UNPKG

docparse-supplier-nge

Version:

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

147 lines (136 loc) 3.64 kB
/** * @param {String} text in the supply section text of the bill */ var num = require('num'); var inspect = require('eyespect').inspector({maxLength:20000000}); module.exports = function(text, callback) { text = text.replace(/\$/g,''); var cost = num(0); var cost_found = false; var cost_supply_variable = get_cost_supply_variable(text); if (cost_supply_variable) { cost_found = true; cost = cost.add(cost_supply_variable); } var cost_supply_two = get_cost_supply_two(text); if (cost_supply_two) { cost_found = true; cost = cost.add(cost_supply_two); } var cost_supply_three = get_cost_supply_three(text); if (cost_supply_three) { cost_found = true; cost = cost.add(cost_supply_three); } var cost_supply_four = get_cost_supply_four(text); if (cost_supply_four) { cost_found = true; cost = cost.add(cost_supply_four); } if (!cost_found) { inspect('********************************************************************************'); console.log(text); var err = 'cost supply not found'; return callback(err, null); } var cost_string = cost.toString(); return callback(null, cost_string); }; /** * Cost Supply variable text looks like "... Billing Period Jul 13, 2011 to Jul 21, 2011 Number of days in period 8 Basic Service Variable 0.0722 x 45600 kWh 3,292.32 Total Supply Services $ 11,414.00 */ var get_cost_supply_variable = function(text) { var pattern = /Basic Service (?:Variable|Fixed).*?(-?\s?[\d\.]+)\s*$/mg; var cost = num(0); var cost_found = false; while(true ) { var matches = pattern.exec(text); if (!matches || matches.length !== 2) { break; } var match = matches[1].trim(); cost = cost.add(match); cost_found = true; } if (!cost_found) { return null; } return cost.toString(); }; /** * Cost Supply Style two text looks like "... ... Electricity Supply 0.0681 x 111200 kWh 7572.72 Total Supply Services $ 7572.72' */ var get_cost_supply_two = function(text) { var pattern = /Electricity Supply.*?(-?\s?[\d\.]+)\s*$/gm; var cost = num(0); var cost_found = false; while(true) { var matches = pattern.exec(text); if (!matches || matches.length !== 2) { break; } var match = matches[1].trim(); cost = cost.add(match); cost_found = true; } if (!cost_found) { return null; } return cost.toString(); }; /** * Cost Supply Style two text looks like "... Energy Service 0.07250398 x 6082 kWh 440.97 Total Supply Services $ 440.97' */ var get_cost_supply_three = function(text) { var pattern = /Energy Service.*?(-?\s?[\d\.]+)\s*$/gm; var cost = num(0); var cost_found = false; while(true) { var matches = pattern.exec(text); if (!matches || matches.length !== 2) { break; } var match = matches[1].trim(); cost = cost.add(match); cost_found = true; } if (!cost_found) { return null; } return cost.toString(); }; /** * Cost Supply Style four text looks like "... Energy Service 0.07250398 x 6082 kWh 440.97 Total Supply Services $ 440.97' */ var get_cost_supply_four = function(text) { var pattern = /Energy Charge.*?(-?\s?[\d\.]+)\s*$/gm; var cost = num(0); var cost_found = false; while(true) { var matches = pattern.exec(text); if (!matches || matches.length !== 2) { break; } var match = matches[1].trim(); cost = cost.add(match); cost_found = true; } if (!cost_found) { return null; } return cost.toString(); };