sms-parser.js 1 KB
"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;