UNPKG

docparse-supplier-nge

Version:

process ngrid electric utility bill data for use in the docparse system

51 lines (49 loc) 1.24 kB
/** * @param {String} text the entire supply section of the bill * @see {Function} get_cost_other_total */ var async = require('async'); var num = require('num'); module.exports = function(text, callback) { var anchor; var anchors = [ 'Basic Service Fixed', 'Basic Service Variable', 'Electricity Supply', 'Energy Service', 'Energy Charge' ]; for (var i in anchors) { var pattern = anchors[i]; var regex = new RegExp(pattern); if (text.match(regex)) { anchor = pattern; } } var replace_pattern = new RegExp('[\\s\\S]*?'+anchor +'.*$','m'); text = text.replace(replace_pattern, ''); var costs = []; var total = num(0); var lines = text.match(/[^\r\n]+/g); async.forEach( lines, function (line, cb) { var line_text = line; get_other_cost_line_item(line_text, function (err, reply) { if (err) { return cb(err); } if (!reply) { return cb(); } costs.push(reply); total = total.add(num(reply.total)); cb(); }); }, function(err) { if (err) { return callback(err); } var output = { total: total.toString(), costs: costs }; return callback(null, output); } ); }