@syniol/iso-3166
Version:
ISO-3166 Standard for Country codes
78 lines (77 loc) • 3.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Country = void 0;
var ISO3166 = __importStar(require("./__data__/iso-3166.json"));
var exception_1 = require("./exception");
var Country = (function () {
function Country(name, alpha2Code, alpha3Code, numeric) {
this.name = name;
this.alpha2Code = alpha2Code;
this.alpha3Code = alpha3Code;
this.numeric = numeric;
}
Country.CountryList = function () {
if (Country.countryList.length > 0) {
return Country.countryList;
}
for (var _i = 0, _a = Object.values(ISO3166); _i < _a.length; _i++) {
var country = _a[_i];
Country.countryList.push(new Country(country.name, country['alpha-2'], country['alpha-3'], Number(country['numeric'])));
}
return Country.countryList;
};
Country.CreateFromName = function (name) {
var countries = Object.values(ISO3166).filter(function (country) { var _a; return (_a = country.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(name === null || name === void 0 ? void 0 : name.toLowerCase()); });
if (countries.length < 1) {
throw exception_1.InvalidCountryNameException;
}
var country = countries[0];
return new Country(country.name, country['alpha-2'], country['alpha-3'], Number(country['numeric']));
};
Country.CreateFromAlpha2Code = function (alpha2Code) {
var countries = Object.values(ISO3166).filter(function (country) { var _a; return ((_a = country['alpha-2']) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === (alpha2Code === null || alpha2Code === void 0 ? void 0 : alpha2Code.toLowerCase()); });
if (countries.length < 1) {
throw exception_1.InvalidAlpha2CodeNameException;
}
var country = countries[0];
return new Country(country.name, country['alpha-2'], country['alpha-3'], Number(country['numeric']));
};
Country.CreateFromAlpha3Code = function (alpha3Code) {
var countries = Object.values(ISO3166).filter(function (country) { var _a; return ((_a = country['alpha-3']) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === (alpha3Code === null || alpha3Code === void 0 ? void 0 : alpha3Code.toLowerCase()); });
if (countries.length < 1) {
throw exception_1.InvalidAlpha3CodeNameException;
}
var country = countries[0];
return new Country(country.name, country['alpha-2'], country['alpha-3'], Number(country['numeric']));
};
Country.CreateFromNumeric = function (numeric) {
var countries = Object.values(ISO3166).filter(function (country) { return Number(country['numeric']) === numeric; });
if (countries.length < 1) {
throw exception_1.InvalidNumericCodeNameException;
}
var country = countries[0];
return new Country(country.name, country['alpha-2'], country['alpha-3'], Number(country['numeric']));
};
Country.countryList = [];
return Country;
}());
exports.Country = Country;