sms.js
1015 Bytes
'use strict';
const moment = require('moment');
function extract(raw) {
if (!raw) return null;
const lines = raw.trim().split(/[\n\r]+/m);
if (!lines) return null;
const result = {
metadata: {
status: null,
from: null,
ts: null,
raw: null,
},
message: null,
raw,
};
const metadata = lines[0].split(',').map(el => el.replace(/"/g, '')) || [];
[result.metadata.raw] = lines;
result.metadata.status = typeof metadata[0] === 'string' ? metadata[0].replace(/^.+: /, '') : '';
// result.metadata.from = typeof metadata[1] === 'string' ? metadata[1].replace(/^\+/, '') : '';
[, result.metadata.from] = metadata;
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('\n').trim();
return result;
}
exports.extract = extract;