UNPKG

@geoapify/un-locode

Version:

A Node.js library for querying United Nations Location Code (UN/LOCODE) data

3 lines (2 loc) 6.26 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("path"),require("fs")):"function"==typeof define&&define.amd?define(["exports","path","fs"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).autocomplete={},e.path,e.require$$0)}(this,(function(e,t,r){"use strict";function i(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var i=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,i.get?i:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,t}var n=i(t),o={};let s=r;var u=new class{readFile(e,t){return s.readFileSync(e,t).toString()}writeFile(e,t){s.writeFile(t,e,(function(e){if(e)throw e;console.log("File saved: "+t)}))}},a=new class{trimPropertyName(e){return e.replace(/\s/g,"")}getValueFormatByType(e){return void 0===e||""===e?String():isNaN(e)?"true"===e||"false"===e?JSON.parse(e.toLowerCase()):String(e):Number(e)}hasContent(e){if(e.length>0)for(let t=0;t<e.length;t++)if(e[t])return!0;return!1}},l=new class{validateJson(e){try{JSON.parse(e)}catch(e){throw Error("Parsed csv has generated an invalid json!!!\n"+e)}}};let d=u,h=a,c=l;const f=/\r?\n/;let p=new class{formatValueByType(e){return this.printValueFormatByType=e,this}supportQuotedField(e){return this.isSupportQuotedField=e,this}fieldDelimiter(e){return this.delimiter=e,this}indexHeader(e){if(isNaN(e))throw new Error("The index Header must be a Number!");return this.indexHeader=e,this}parseSubArray(e="*",t=","){this.parseSubArrayDelimiter=e,this.parseSubArraySeparator=t}encoding(e){return this.encoding=e,this}generateJsonFileFromCsv(e,t){let r=this.getJsonFromCsvStringified(e);d.writeFile(r,t)}getJsonFromCsvStringified(e){let t=this.getJsonFromCsv(e),r=JSON.stringify(t,void 0,1);return c.validateJson(r),r}getJsonFromCsv(e){let t=d.readFile(e,this.encoding);return this.csvToJson(t)}csvStringToJson(e){return this.csvToJson(e)}csvToJson(e){this.validateInputConfig();let t,r=e.split(f),i=this.getFieldDelimiter(),n=this.getIndexHeader();for(t=this.isSupportQuotedField?this.split(r[n]):r[n].split(i);!h.hasContent(t)&&n<=r.length;)n+=1,t=r[n].split(i);let o=[];for(let e=n+1;e<r.length;e++){let n;n=this.isSupportQuotedField?this.split(r[e]):r[e].split(i),h.hasContent(n)&&o.push(this.buildJsonResult(t,n))}return o}getFieldDelimiter(){return this.delimiter?this.delimiter:";"}getIndexHeader(){return null===this.indexHeader||isNaN(this.indexHeader)?0:this.indexHeader}buildJsonResult(e,t){let r={};for(let i=0;i<e.length;i++){let n=h.trimPropertyName(e[i]),o=t[i];this.isParseSubArray(o)&&(o=this.buildJsonSubArray(o)),this.printValueFormatByType&&!Array.isArray(o)&&(o=h.getValueFormatByType(t[i])),r[n]=o}return r}buildJsonSubArray(e){let t=e.substring(e.indexOf(this.parseSubArrayDelimiter)+1,e.lastIndexOf(this.parseSubArrayDelimiter));if(t.trim(),e=t.split(this.parseSubArraySeparator),this.printValueFormatByType)for(let t=0;t<e.length;t++)e[t]=h.getValueFormatByType(e[t]);return e}isParseSubArray(e){return!(!this.parseSubArrayDelimiter||!e||0!==e.indexOf(this.parseSubArrayDelimiter)||e.lastIndexOf(this.parseSubArrayDelimiter)!==e.length-1)}validateInputConfig(){if(this.isSupportQuotedField){if('"'===this.getFieldDelimiter())throw new Error('When SupportQuotedFields is enabled you cannot defined the field delimiter as quote -> ["]');if('"'===this.parseSubArraySeparator)throw new Error('When SupportQuotedFields is enabled you cannot defined the field parseSubArraySeparator as quote -> ["]');if('"'===this.parseSubArrayDelimiter)throw new Error('When SupportQuotedFields is enabled you cannot defined the field parseSubArrayDelimiter as quote -> ["]')}}hasQuotes(e){return e.includes('"')}split(e){if(0==e.length)return[];let t=this.getFieldDelimiter(),r=[""];if(this.hasQuotes(e)){let i=e.split(""),n=0,o=!1,s=!1;if(i.forEach(((e,i,u)=>{if(s)return r[n]+=e,void(s=!1);if('"'!=e&&e!=t)r[n]+=e;else if(e==t&&o)r[n]+=e;else{if(e==t)return n++,void(r[n]="");'"'===u[i+1]?s=!0:o=!o}})),o)throw new Error("Row contains mismatched quotes!");return r}return e.split(t)}};o.formatValueByType=function(e=!0){return p.formatValueByType(e),this},o.supportQuotedField=function(e=!1){return p.supportQuotedField(e),this},o.fieldDelimiter=function(e){return p.fieldDelimiter(e),this},o.indexHeader=function(e){return p.indexHeader(e),this},o.parseSubArray=function(e,t){return p.parseSubArray(e,t),this},o.customEncoding=function(e){return p.encoding=e,this},o.utf8Encoding=function(){return p.encoding="utf8",this},o.ucs2Encoding=function(){return p.encoding="ucs2",this},o.utf16leEncoding=function(){return p.encoding="utf16le",this},o.latin1Encoding=function(){return p.encoding="latin1",this},o.asciiEncoding=function(){return p.encoding="ascii",this},o.base64Encoding=function(){return this.csvToJson="base64",this},o.hexEncoding=function(){return this.csvToJson="hex",this},o.generateJsonFileFromCsv=function(e,t){if(!e)throw new Error("inputFileName is not defined!!!");if(!t)throw new Error("outputFileName is not defined!!!");p.generateJsonFileFromCsv(e,t)},o.getJsonFromCsv=function(e){if(!e)throw new Error("inputFileName is not defined!!!");return p.getJsonFromCsv(e)},o.csvStringToJson=function(e){return p.csvStringToJson(e)},o.jsonToCsv=function(e,t){p.generateJsonFileFromCsv(e,t)};const g=new Map;function m(e){if(!e)return[];let t=[];for(let r=0;r<e.length;r++)"-"!==e.charAt(r)&&t.push(e.charAt(r));return t}e.query=function(e,t){g.forEach(((e,t)=>{e.loadDate.getTime()+864e5<Date.now()&&g.delete(t)}));let r=function(e){if(g.has(e))return g.get(e).data;const t=n.join(__dirname,"data",e+".csv");try{const r=o.fieldDelimiter(",").supportQuotedField(!0).getJsonFromCsv(t);return r.forEach((e=>{"true"===e.geocoded?e.geocoded=!0:delete e.geocoded})),function(e,t){g.set(e,{loadDate:new Date,data:t})}(e,r),r}catch(e){return null}}(e);if(!r)return null;let i=function(e,t){return e.find((e=>e.location===t))}(r,t);if(i){const e={country:i.country,location:i.location,locationName:i.nameWoDiacritics,subdivision:i.subdivision,status:i.status,functionCodes:m(i.function)};return i.lat&&i.lon&&(e.coordinates={lat:parseFloat(i.lat),lon:parseFloat(i.lon)}),e.subdivision||delete e.subdivision,e}return null}})); //# sourceMappingURL=index.min.js.map