country-state-city-js
Version:
A fast & light weight JS library to get countries geographical information including states, cities, iso codes, etc.
49 lines • 2.1 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateLite = exports.State = void 0;
var utils_1 = require("../utils");
exports.State = stateHandler();
exports.StateLite = stateHandler('lite');
function stateHandler(type) {
var path = type === 'lite' ? utils_1.PATH_LITE : utils_1.PATH;
var fn = function (countryIso2, stateIso, populate) {
if (!countryIso2) {
throw new TypeError("Country iso2 type missing");
}
try {
var stateRecords = require(path + "/states/" + countryIso2 + "/index.json");
if (!stateIso || typeof stateIso === 'object') {
var states = [];
for (var _i = 0, stateRecords_1 = stateRecords; _i < stateRecords_1.length; _i++) {
var _a = stateRecords_1[_i];
var state_1 = __rest(_a, []);
(0, utils_1.populateCities)(countryIso2, state_1, typeof stateIso === 'object' ? stateIso.cities : false, type);
states.push(state_1);
}
return states;
}
var state = stateRecords.find(function (_a) {
var iso = _a.iso;
return iso === stateIso;
});
(populate === null || populate === void 0 ? void 0 : populate.cities) && (0, utils_1.populateCities)(countryIso2, state, true, type);
return state !== null && state !== void 0 ? state : null;
}
catch (e) {
return null;
}
};
return fn;
}
//# sourceMappingURL=state.js.map