82 lines
1.9 KiB
JavaScript
82 lines
1.9 KiB
JavaScript
/**
|
|
* Check message can use or noot
|
|
* @param {string} text SMS message text
|
|
* @return {boolean} can use or not
|
|
*/
|
|
export const canUseSMS = (text) => {
|
|
return text.includes('گزارش');
|
|
};
|
|
|
|
/**
|
|
* convert smsed text data to code and readable data
|
|
* @param {string} text SMS message text
|
|
* @return {{day: string, date: string, data: { status: boolean, key: string, value: string | undefined }[]}} output data
|
|
*/
|
|
export const useSMSData = (text) => {
|
|
let output = {
|
|
day: '',
|
|
date: '',
|
|
data: [],
|
|
};
|
|
|
|
if (!text || typeof text !== 'string') {
|
|
return output;
|
|
}
|
|
|
|
try {
|
|
const splitBreakLines = text.split('\n');
|
|
|
|
output.day = splitBreakLines[0].trim();
|
|
output.date = splitBreakLines[1].trim();
|
|
|
|
for (let i = 4; i < splitBreakLines.length; i++) {
|
|
output.data.push(translateDataRow(splitBreakLines[i].trim()));
|
|
}
|
|
|
|
return output;
|
|
} catch (error) {
|
|
return output;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* translate data row
|
|
* @param {string} text Data row
|
|
* @return {{status: boolean, key: string, value: string | undefined}} output
|
|
*/
|
|
const translateDataRow = (text) => {
|
|
const output = {
|
|
status: false,
|
|
key: '',
|
|
value: '',
|
|
};
|
|
|
|
if (['+', '-'].includes(text.slice(0, 1))) {
|
|
output.status = text.slice(0, 1) === '+';
|
|
text = text.replace(text.slice(0, 1), '');
|
|
} else {
|
|
output.status = null;
|
|
}
|
|
|
|
if (text.includes(':')) {
|
|
const splitKeyValue = text.split(':');
|
|
|
|
output.key = splitKeyValue[0];
|
|
output.value = splitKeyValue[1];
|
|
} else {
|
|
output.key = text;
|
|
}
|
|
|
|
return output;
|
|
};
|
|
|
|
/**
|
|
* Convert devices list to string for shared performance
|
|
* @param {{name: string, phone: string}[]} devices List of devices
|
|
*/
|
|
export const useDevicesAsString = (devices) => {
|
|
let result = devices.reduce((pre, curr) => (pre += `${curr.name}:${curr.phone},`), '');
|
|
|
|
return result.slice(0, result.length - 1);
|
|
};
|