light-characteristics
Version:
🛥️🔦 A parser for light characteristics used on nautical charts
3 lines (2 loc) • 3.44 kB
JavaScript
function S(e){let i="";if(e.CATLIT==="directional"&&(i+="Dir"),e.CATLIT==="aero"&&(i+="Aero"),e.MLTYLT&&(i+=e.MLTYLT),e.LITCHR)if(e.SIGGRP)if(e.LITCHR.includes("+")){let[r,o]=e.LITCHR.split("+");i+=`${r}(${e.SIGGRP})+${o}`}else i+=`${e.LITCHR}(${e.SIGGRP})`;else i+=e.LITCHR;return i&&i.at(-1)!==")"&&(i+="."),e.COLOUR.length&&(i+=e.COLOUR.join("")),i.at(-1)!=="."&&(i+="."),e.SIGPER&&(i+=`${e.SIGPER}s`),e.HEIGHT&&(i+=`${e.HEIGHT}m`),e.VALMXR&&(i+=`${e.VALMXR}M`),i.at(-1)==="."&&(i=i.slice(0,-1)),e.CATLIT==="vertical"&&(i+="(vert)"),e.CATLIT==="horizontal"&&(i+="(hor)"),e.CATLIT==="front"&&(i+="(Front)"),e.CATLIT==="rear"&&(i+="(Rear)"),e.CATLIT==="upper"&&(i+="(Upper)"),e.CATLIT==="lower"&&(i+="(Lower)"),i}var I={W:"white",B:"black",R:"red",G:"green",Bu:"blue",Y:"yellow",Gy:"grey",Br:"brown",A:"amber",V:"violet",O:"orange",M:"mangeta",P:"pink"},s={directional:"directional function",leading:"leading light",aero:"aero light",air_obstruction:"air obstruction light",fog_detector:"fog detector light",floodlight:"flood light",strip_light:"strip light",subsidiary:"subsidiary light",spotlight:"spotlight",front:"front",rear:"rear",lower:"lower",upper:"upper",moire:"moir\xE9 effect",emergency:"emergency",bearing:"bearing light",horizontal:"horizontally disposed",vertical:"vertically disposed"},C={F:"fixed",Fl:"flashing",LFl:"long-flashing",Q:"quick-flashing",VQ:"very quick-flashing",UQ:"ultra quick-flashing",Iso:"isophased",Oc:"occulting",IQ:"interrupted quick-flashing",IVQ:"interrupted very quick- flashing",IUQ:"interrupted ultra quick- flashing",Mo:"morse",FFl:"fixed/flash",FlLFl:"flash/long-flash",OcFl:"occulting/flash",FLFl:"fixed/long-flash","Al.Oc":"occulting alternating","Al.LFl":"long-flash alternating","Al.Fl":"flash alternating","Al.Gr":"group alternating","Q+LFl":"quick-flash plus long- flash","VQ+LFl":"very quick-flash plus long- flash","UQ+LFl":"ultra quick-flash plus long- flash",Al:"alternating","Al.FFl":"fixed and alternating flashing"};var p=Object.keys(C).sort((e,i)=>i.length-e.length),G=Object.keys(I).sort((e,i)=>i.length-e.length),R=e=>G.find(i=>e.startsWith(i)),T=e=>e in s,d=e=>e in C;var A=/(?<SIGPER>\d[\d.]*s)?(?<HEIGHT>\d[\d.]*m)?(?<VALMXR>\d[\d.]*(M|Nm))?$/,h={vertical:"vert",horizontal:"hor",directional:"dir"};function w(e){let i=e,r={COLOUR:[]},o=Object.keys(s).find(t=>i.toLowerCase().startsWith(h[t]||t));if(o&&T(o)){r.CATLIT=o;let t=h[o]||o;i=i.slice(t.length)}let l=Object.keys(s).find(t=>i.toLowerCase().endsWith(`(${h[t]||t})`));if(l&&T(l)){r.CATLIT=l;let t=h[l]||l;i=i.slice(0,-t.length-2)}let g=i.match(/^\d+/g)?.[0];g&&(r.MLTYLT=+g,i=i.slice(g.length));let a=i.match(A);if(a?.groups&&a?.[0]){let{SIGPER:t,HEIGHT:f,VALMXR:u}=a.groups;t&&(r.SIGPER=parseFloat(t)),f&&(r.HEIGHT=parseFloat(f)),u&&(r.VALMXR=parseFloat(u)),i=i.slice(0,-a[0].length)}let n=i.match(/\((?<SIGGRP>[\w+]+)\)/);if(n?.groups?.SIGGRP&&n.index!==void 0){r.SIGGRP=n.groups.SIGGRP;let t=n[0],f=i[n.index+t.length];i=i.replace(t,f==="+"?"":".")}let c=p.find(t=>i.startsWith(t));c&&d(c)&&(r.LITCHR=c,i=i.replace(c,".")),i=i.replaceAll(".","").replaceAll(" ","");let L;for(;L=R(i);)r.COLOUR.push(L),i=i.slice(L.length);if(!r.LITCHR)throw new SyntaxError("No light characteristic found");if(i)throw new SyntaxError(`Unexpected text remaining: \u201C${i}\u201D`);return r}export{I as COLOURS,s as LIGHT_CATEGORIES,C as LIGHT_CHARACTERISTICS,w as decodeLight,S as encodeLight};
//# sourceMappingURL=index.js.map