Blame view
lib/sms.js
1015 Bytes
49eaf19a3
|
1 2 3 4 5 6 7 8 9 |
'use strict'; const moment = require('moment'); function extract(raw) { if (!raw) return null; const lines = raw.trim().split(/[ \r]+/m); if (!lines) return null; |
49eaf19a3
|
10 11 |
const result = { metadata: { |
edb8b6e81
|
12 13 14 15 |
status: null, from: null, ts: null, raw: null, |
49eaf19a3
|
16 |
}, |
edb8b6e81
|
17 |
message: null, |
49eaf19a3
|
18 19 |
raw, }; |
edb8b6e81
|
20 21 22 23 |
const metadata = lines[0].split(',').map(el => el.replace(/"/g, '')) || []; [result.metadata.raw] = lines; result.metadata.status = typeof metadata[0] === 'string' ? metadata[0].replace(/^.+: /, '') : ''; |
6abb33e39
|
24 |
// result.metadata.from = typeof metadata[1] === 'string' ? metadata[1].replace(/^\+/, '') : ''; |
233a5ff50
|
25 |
[, result.metadata.from] = metadata; |
edb8b6e81
|
26 27 28 29 30 31 |
const tsFromRaw = `${metadata[3]} ${(metadata[4] || '').replace(/\+\d+/, '')}`; result.metadata.ts = moment(tsFromRaw, 'YY/MM/DD HH:mm:ss').format('YYYY-MM-DD HH:mm:ss'); result.message = lines.slice(1).join(' ').trim(); |
49eaf19a3
|
32 33 34 35 |
return result; } exports.extract = extract; |