@geoapify/un-locode
Version:
A Node.js library for querying United Nations Location Code (UN/LOCODE) data
3 lines (2 loc) • 5.75 kB
JavaScript
import*as e from"path";import t from"fs";var r={};let i=t;var n=new class{readFile(e,t){return i.readFileSync(e,t).toString()}writeFile(e,t){i.writeFile(t,e,(function(e){if(e)throw e;console.log("File saved: "+t)}))}},s=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}},o=new class{validateJson(e){try{JSON.parse(e)}catch(e){throw Error("Parsed csv has generated an invalid json!!!\n"+e)}}};let a=n,u=s,l=o;const d=/\r?\n/;let h=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);a.writeFile(r,t)}getJsonFromCsvStringified(e){let t=this.getJsonFromCsv(e),r=JSON.stringify(t,void 0,1);return l.validateJson(r),r}getJsonFromCsv(e){let t=a.readFile(e,this.encoding);return this.csvToJson(t)}csvStringToJson(e){return this.csvToJson(e)}csvToJson(e){this.validateInputConfig();let t,r=e.split(d),i=this.getFieldDelimiter(),n=this.getIndexHeader();for(t=this.isSupportQuotedField?this.split(r[n]):r[n].split(i);!u.hasContent(t)&&n<=r.length;)n+=1,t=r[n].split(i);let s=[];for(let e=n+1;e<r.length;e++){let n;n=this.isSupportQuotedField?this.split(r[e]):r[e].split(i),u.hasContent(n)&&s.push(this.buildJsonResult(t,n))}return s}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=u.trimPropertyName(e[i]),s=t[i];this.isParseSubArray(s)&&(s=this.buildJsonSubArray(s)),this.printValueFormatByType&&!Array.isArray(s)&&(s=u.getValueFormatByType(t[i])),r[n]=s}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]=u.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,s=!1,o=!1;if(i.forEach(((e,i,a)=>{if(o)return r[n]+=e,void(o=!1);if('"'!=e&&e!=t)r[n]+=e;else if(e==t&&s)r[n]+=e;else{if(e==t)return n++,void(r[n]="");'"'===a[i+1]?o=!0:s=!s}})),s)throw new Error("Row contains mismatched quotes!");return r}return e.split(t)}};r.formatValueByType=function(e=!0){return h.formatValueByType(e),this},r.supportQuotedField=function(e=!1){return h.supportQuotedField(e),this},r.fieldDelimiter=function(e){return h.fieldDelimiter(e),this},r.indexHeader=function(e){return h.indexHeader(e),this},r.parseSubArray=function(e,t){return h.parseSubArray(e,t),this},r.customEncoding=function(e){return h.encoding=e,this},r.utf8Encoding=function(){return h.encoding="utf8",this},r.ucs2Encoding=function(){return h.encoding="ucs2",this},r.utf16leEncoding=function(){return h.encoding="utf16le",this},r.latin1Encoding=function(){return h.encoding="latin1",this},r.asciiEncoding=function(){return h.encoding="ascii",this},r.base64Encoding=function(){return this.csvToJson="base64",this},r.hexEncoding=function(){return this.csvToJson="hex",this},r.generateJsonFileFromCsv=function(e,t){if(!e)throw new Error("inputFileName is not defined!!!");if(!t)throw new Error("outputFileName is not defined!!!");h.generateJsonFileFromCsv(e,t)},r.getJsonFromCsv=function(e){if(!e)throw new Error("inputFileName is not defined!!!");return h.getJsonFromCsv(e)},r.csvStringToJson=function(e){return h.csvStringToJson(e)},r.jsonToCsv=function(e,t){h.generateJsonFileFromCsv(e,t)};const c=new Map;function f(t,i){c.forEach(((e,t)=>{e.loadDate.getTime()+864e5<Date.now()&&c.delete(t)}));let n=function(t){if(c.has(t))return c.get(t).data;const i=e.join(__dirname,"data",t+".csv");try{const e=r.fieldDelimiter(",").supportQuotedField(!0).getJsonFromCsv(i);return e.forEach((e=>{"true"===e.geocoded?e.geocoded=!0:delete e.geocoded})),function(e,t){c.set(e,{loadDate:new Date,data:t})}(t,e),e}catch(e){return null}}(t);if(!n)return null;let s=function(e,t){return e.find((e=>e.location===t))}(n,i);if(s){const e={country:s.country,location:s.location,locationName:s.nameWoDiacritics,subdivision:s.subdivision,status:s.status,functionCodes:p(s.function)};return s.lat&&s.lon&&(e.coordinates={lat:parseFloat(s.lat),lon:parseFloat(s.lon)}),e.subdivision||delete e.subdivision,e}return null}function p(e){if(!e)return[];let t=[];for(let r=0;r<e.length;r++)"-"!==e.charAt(r)&&t.push(e.charAt(r));return t}export{f as query};
//# sourceMappingURL=index.min.esm.js.map