UNPKG

@mr-mkz/atlas

Version:

Atlas is a library that gives you region, subregion, country, state and city names with some special information if you want.

3 lines (2 loc) 8.34 kB
var e=require("fs"),t=require("path"),r=require("url");function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function o(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function i(e){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},i(e)}function a(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(a=function(){return!!e})()}function u(e,t){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},u(e,t)}function s(e){var t="function"==typeof Map?new Map:void 0;return s=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(a())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,t);var o=new(e.bind.apply(e,n));return r&&u(o,r.prototype),o}(e,arguments,i(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),u(r,e)},s(e)}var c=/*#__PURE__*/function(e){function t(t,r,n,o){var i;return(i=e.call(this,t)||this).name="AtlasError -> "+r,n&&(i.tips=n),o&&(i.data=o),i}var r,n;return n=e,(r=t).prototype=Object.create(n.prototype),r.prototype.constructor=r,u(r,n),t}(/*#__PURE__*/s(Error));function l(e,t,r,n){throw new c(e,t,r,n)}function f(e){try{var n=r.fileURLToPath("undefined"==typeof document?new(require("url").URL)("file:"+n).href:document.currentScript&&document.currentScript.src||new URL("index.cjs",document.baseURI).href),o=t.dirname(n);return t.join(o,"..","assets",e)}catch(e){console.error("Error reading file:",e)}}var m=new(/*#__PURE__*/function(){function t(){}var r=t.prototype;return r.getRegions=function(){try{return Promise.resolve(e.promises.readFile(f("regions.json"),"utf8")).then(JSON.parse)}catch(e){return Promise.reject(e)}},r.getRegionByName=function(t){try{return Promise.resolve(function(){if(isNaN(Number(t)))return Promise.resolve(e.promises.readFile(f("regions.json"),"utf8")).then(function(e){for(var r,n=o(JSON.parse(e));!(r=n()).done;){var i=r.value;if(i.name.toLowerCase()===t.toLowerCase())return i}l("region "+t+" not found","RESULT_NOT_FOUND")});l("region name must be string","INPUT_TYPE_ERR")}())}catch(e){return Promise.reject(e)}},r.getSubRegion=function(t){try{var r=[];return Promise.resolve(e.promises.readFile(f("subregions.json"),"utf8")).then(function(e){for(var n,i=o(JSON.parse(e));!(n=i()).done;){var a=n.value;a.region_id==t&&r.push(a)}if(0!==r.length)return r;l("Subregion with region id "+t+" not found","RESULT_NOT_FOUND")})}catch(e){return Promise.reject(e)}},r.getCountries=function(t){var r=t.currency,n=t.dialCode,i=t.native,a=t.nationality,u=t.region,s=t.subregion,c=t.translations,l=t.timezones,m=t.geolocation,d=t.emojies,v=t.domain;try{var g=[];return Promise.resolve(e.promises.readFile(f("countries_states_cities.json"),"utf8")).then(function(e){for(var t,f=o(JSON.parse(e));!(t=f()).done;){var y=t.value,p={};p.id=y.id,p.name=y.name,p.iso2=y.iso2,p.iso3=y.iso3,p.capital=y.capital,n&&(p.phone_code=y.phone_code),r&&(p.currency=y.currency,p.currency_name=y.currency_name,p.currency_symbol=y.currency_symbol),v&&(p.domain=y.tld),i&&(p.native=y.native),a&&(p.nationality=y.nationality),u&&(p.region=y.region,p.region_id=y.region_id),s&&(p.subregion=y.subregion,p.subregion_id=y.subregion_id),c&&(p.translations=y.translations),l&&(p.timezones=y.timezones),m&&(p.latitude=y.latitude,p.longitude=y.longitude),d&&(p.emoji=y.emoji,p.emojiU=y.emojiU),g.push(p)}return g})}catch(e){return Promise.reject(e)}},r.getStates=function(t){var r=t.countryName,n=t.iso3,i=t.iso2,a=t.geolocation;try{return Promise.resolve(function(){if(void 0!==r&&null!=r&&isNaN(Number(r))||void 0!==n&&isNaN(Number(n))||void 0!==i&&isNaN(Number(i))){var t=[];return Promise.resolve(e.promises.readFile(f("countries_states_cities.json"),"utf8")).then(function(e){for(var u,s=o(JSON.parse(e));!(u=s()).done;){var c=u.value;if(void 0!==r&&null!=r&&c.name.toLowerCase()===r.toLowerCase()||void 0!==n&&c.iso3.toLowerCase()===n.toLowerCase()||void 0!==i&&c.iso2.toLowerCase()===i.toLowerCase())for(var f,m=o(c.states);!(f=m()).done;){var d=f.value,v={};v.id=d.id,v.name=d.name,v.state_code=d.state_code,a&&(v.latitude=d.latitude,v.longitude=d.longitude),t.push(v)}}if(0!==t.length)return t;l("Country "+r+" not found","RESULT_NOT_FOUND",{country_name:"check entered country name: "+r,iso3:"check entered iso3: "+n,iso2:"check entered iso2: "+i})})}l("region name must be string","INPUT_TYPE_ERR")}())}catch(e){return Promise.reject(e)}},r.getCities=function(t){var r=t.countryName,n=t.stateName,i=t.geolocation;try{var a=[];return Promise.resolve(function(){if(isNaN(Number(r))&&isNaN(Number(n)))return Promise.resolve(e.promises.readFile(f("countries_states_cities.json"),"utf8")).then(function(e){for(var t,u=!1,s=!1,c=o(JSON.parse(e));!(t=c()).done;){var f=t.value;if(f.name.toLowerCase()===r.toLowerCase()){u=!0;for(var m,d=o(f.states);!(m=d()).done;){var v=m.value;if(v.name.toLowerCase()===n.toLowerCase()){s=!0;for(var g,y=o(v.cities);!(g=y()).done;){var p=g.value,h={};h.id=p.id,h.name=p.name,i&&(h.latitude=p.latitude,h.longitude=p.longitude),a.push(h)}}}}}if(a.length>0)return a;u?s?l("There is no city for "+r+","+n,"RESULT_NOT_FOUND"):l("State "+n+" not found","RESULT_NOT_FOUND",{state:"check entered state name: "+n}):l("Country "+r+" not found","RESULT_NOT_FOUND",{country:"check entered country name: "+r})});l("country name and state name must be string","INPUT_TYPE_ERR")}())}catch(e){return Promise.reject(e)}},t}());exports.getAllCountries=function(e){var t=e.currency,r=e.dialCode,n=e.native,o=e.nationality,i=e.region,a=e.subregion,u=e.translations,s=e.timezones,c=e.geolocation,l=e.emojies,f=e.domain;try{return Promise.resolve(m.getCountries({currency:t,dialCode:r,native:n,nationality:o,region:i,subregion:a,translations:u,timezones:s,geolocation:c,emojies:l,domain:f}))}catch(e){return Promise.reject(e)}},exports.getAllRegions=function(){try{return Promise.resolve(m.getRegions())}catch(e){return Promise.reject(e)}},exports.getCountryStates=function(e){var t=e.country,r=e.iso3,n=e.iso2,o=e.geolocation;try{return Promise.resolve(function(){if(t&&""!==t||r&&""!==r||n&&""!==n)return null!=t&&null!=t&&(t=t.toString()),null!=r&&void 0!==r&&(r=r.toString()),null!=n&&null!=n&&(n=n.toString()),Promise.resolve(m.getStates({country:t,iso3:r,iso2:n,geolocation:o}));l("country name, iso3 or iso2 is missing, at least one of them is required!","MISSING_REQUIRED_VALUE")}())}catch(e){return Promise.reject(e)}},exports.getStateCities=function(e){var t=e.country,r=e.state,n=e.geolocation;try{return Promise.resolve(function(){if(t&&void 0!==t&&""!==t)return function(){if(r&&void 0!==r&&""!==r)return isNaN(t)||(t=t.toString()),isNaN(r)||(r=r.toString()),Promise.resolve(m.getCities({country:t,state:r,geolocation:n}));l("state name is required!","MISSING_REQUIRED_VALUE")}();l("country name is required!","MISSING_REQUIRED_VALUE")}())}catch(e){return Promise.reject(e)}},exports.getSubRegions=function(e){try{return Promise.resolve(function(){if(e&&void 0!==e&&""!==e)return Promise.resolve(m.getRegionByName(e)).then(function(e){return function(){if(void 0!==e)return Promise.resolve(m.getSubRegion(e.id))}()});l("region name is required!","MISSING_REQUIRED_VALUE")}())}catch(e){return Promise.reject(e)}}; //# sourceMappingURL=index.cjs.map