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