sms-parser.js
1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"use strict";
const moment = require('moment');
function parseHeader(header) {
if (typeof header !== 'string') {
return;
}
const values = header.split(',');
return {
sender: typeof values[1] === 'string' ? values[1].replace(/"/g, '') : null,
created: typeof values[3] === 'string' && typeof values[4] === 'string' ? moment(values[3] + ' ' + values[4], 'YY/MM/DD HH:mm:ss+SS').format('YYYY-MM-DD HH:mm:ss') : null
}
}
function parseModemResponse(data) {
const sms = {
sender: null,
created: null,
msg: null
}
const lines = data.split(/\r\n/);
if (lines < 2) {
return;
}
const header_string = lines[0] ? lines[0] : null;
const header = parseHeader(header_string);
if (!header) {
return;
}
return {
sender: header.sender,
created: header.created,
msg: lines.slice(1).join("\n")
}
}
exports.parseHeader = parseHeader;
exports.parseModemResponse = parseModemResponse;