UNPKG

state-advice

Version:

This will auto-correct the indian state

147 lines (143 loc) 4.56 kB
"use strict"; const pincodeData = require("./dataset/pincodeData"); const countries = require("./dataset/countries"); const cities = require("./dataset/cities"); const states = require("./dataset/states"); const matchesFinder = require("./MatchesFinder"); function pincodeDataLookup(key) { if (!isNaN(key)) { if (typeof key === "string") { return pincodeData.filter(function(e) { return e.pincode === +key; }); } else if (typeof key === "number") { return pincodeData.filter(function(e) { return e.pincode === key; }); } } else { let records = []; pincodeData.forEach(function(elem) { if ( elem.districtName.toUpperCase() === key.toUpperCase() || elem.taluk.toUpperCase() === key.toUpperCase() ) { let exist = records.find((record, i) => { if (record.stateName === elem.stateName) { if (!Array.isArray(records[i].pincode)) { records[i].pincode = [records[i].pincode]; } else { records[i].pincode.push(elem.pincode); } return true; } }); if (!exist) { elem.pincode = [elem.pincode]; records.push(elem); } } }); return records; } } function autoCorrect(addressObj, totalmatchestocompare = 1) { if ( typeof addressObj !== "object" || !addressObj.state || addressObj.state.length < 2 || countries.indexOf(addressObj.state.toUpperCase()) >= 0 ) { console.log( "Acceptable input:\n1.Object(state[,pincode])\n2.String of length >=2\n3.Not a country" ); return null; } if (addressObj.state.length === 2) { let match = states.abbreviations[addressObj.state.toUpperCase()]; return match ? match : null; } if (states.names.indexOf(addressObj.state.toUpperCase()) > -1) { return addressObj.state.toUpperCase(); } addressObj.pincode && typeof addressObj.pincode === "string" && (addressObj.pincode = +addressObj.pincode); // For case where city is provided in place of state let cityMatch = pincodeDataLookup(addressObj.state); if (cityMatch.length) { if (addressObj.pincode) { let match = cityMatch.find(record => { return record.pincode.indexOf(addressObj.pincode) > -1; }); return match ? match.stateName : null; } else { return cityMatch[0].stateName; } } if ( typeof addressObj.state === "string" && typeof totalmatchestocompare === "number" && totalmatchestocompare >= 0 ) { let cityLookupMatch = pincodeDataLookup(addressObj.state); if (cityLookupMatch.length) { if (addressObj.pincode) { let match = cityLookupMatch.find(record => { return record.pincode.indexOf(addressObj.pincode) > -1; }); return match ? match.stateName : null; } else { return cityLookupMatch[0].stateName; } } let cityCorrection = matchesFinder( addressObj.state, cities, totalmatchestocompare ); let stateCorrection = matchesFinder( addressObj.state, states.names, totalmatchestocompare ); // console.log(cityCorrection, stateCorrection); if (!cityCorrection && !stateCorrection) { return null; } else if (cityCorrection && !stateCorrection) { cityLookupMatch = pincodeDataLookup(cityCorrection.name); if (cityLookupMatch.length) { if (addressObj.pincode) { let match = cityLookupMatch.find(record => { return record.pincode.indexOf(addressObj.pincode) > -1; }); return match ? match.stateName : null; } else { return cityLookupMatch[0].stateName; } } else { return null; } } else if (!cityCorrection && stateCorrection) { return stateCorrection.name; } else if (cityCorrection && stateCorrection) { if (cityCorrection.percentage > stateCorrection.percentage) { cityLookupMatch = pincodeDataLookup(cityCorrection.name); if (cityLookupMatch.length) { if (addressObj.pincode) { let match = cityLookupMatch.find(record => { return record.pincode.indexOf(addressObj.pincode) > -1; }); return match ? match.stateName : null; } else { return cityLookupMatch[0].stateName; } } else { return null; } } return stateCorrection.name || null; } } } exports.autoCorrect = autoCorrect;