UNPKG

quad-hexer

Version:

A library for converting between quad and hex representations of geocodes.

3 lines (2 loc) 3.51 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=t=>{if(typeof t!="string")throw new Error("Invalid quadkey type: "+typeof t);if(!/^[0-3]{1,32}$/.test(t))throw new Error("Invalid quadkey format: "+t);const r=t.split("").map(a=>parseInt(a,4).toString(2).padStart(2,"0")).join(""),e=["x"],i=Math.floor(r.length/4);return r.length%4===0?e.push(BigInt("0b"+r).toString(16).padStart(i,"0")):(r.length>=4&&e.push(BigInt("0b"+r.slice(0,-2)).toString(16).padStart(i,"0")),e.push("#"),e.push(BigInt("0b"+r.slice(-2)).toString(4))),e.join("")},u=t=>{if(!t||typeof t!="string")throw new Error("Invalid hex quadkey type: "+typeof t);const n=/^x([0-9a-f]{0,16})(?:#([0-3]))?$/;if(!n.test(t))throw new Error("Invalid hex quadkey format: "+t);const r=t.match(n);if(!r[1]&&!r[2])throw new Error("Invalid hex quadkey format: "+t);const e=[];if(r[1]){const i=r[1].length*2;e.push(BigInt("0x"+r[1]).toString(4).padStart(i,"0"))}return r[2]&&e.push(r[2]),e.join("")},m=t=>{if(typeof t!="string")throw new Error("Invalid S2 hilbert quadkey type: "+typeof t);const n=/^([0-5]{1})\/([0-3]{1,30})$/;if(!n.test(t))throw new Error("Invalid S2 hilbert quadkey format: "+t);const r=t.match(n),e=[];return e.push(r[1]),e.push(f(r[2])),e.join("")},w=t=>{if(typeof t!="string")throw new Error("Invalid hex S2 hilbert quadkey type: "+typeof t);const n=/^([0-5]{1})(x[0-9a-f]{0,15}(?:#[0-3])?)$/;if(!n.test(t))throw new Error("Invalid hex S2 hilbert quadkey format: "+t);const r=t.match(n),e=[];if(e.push(r[1]),e.push("/"),r[2])try{e.push(u(r[2]))}catch(i){throw i instanceof Error&&i.message&&i.message.includes("Invalid hex quadkey format")?new Error("Invalid hex S2 hilbert quadkey format: "+t):i}return e.join("")},I=t=>{if(typeof t!="string")throw new Error("Invalid spatial id tilehash type: "+typeof t);const n=/^(-?)([1-8]{1,30})$/;if(!n.test(t))throw new Error("Invalid spatial id tilehash format: "+t);const r=t.match(n),e=r[1]==="-",a=r[2].split("").map(s=>(parseInt(s)-1).toString()).join(""),c=a.length,l=BigInt("0o"+a).toString(2).padStart(c*3,"0"),y=[],p=[];for(let s=0;s<c*3;s+=3)y.push(l.slice(s,s+1)),p.push(l.slice(s+1,s+3));const S=y.join(""),h=p.join(""),o=parseInt(S,2),x=BigInt("0b"+h).toString(4).padStart(c,"0"),g=[];return g.push(f(x)),o!==0&&(g.push(e?"-":"+"),g.push(o.toString(16))),g.join("")},b=t=>{if(typeof t!="string")throw new Error("Invalid hex spatial id tilehash type: "+typeof t);const n=/^(x[0-9a-f]{0,15}(?:#[0-3])?)(?:([+-][0-9a-f]+))?$/;if(!n.test(t))throw new Error("Invalid hex spatial id tilehash format: "+t);const r=t.match(n);let e;try{e=u(r[1])}catch(o){throw o instanceof Error&&o.message&&o.message.includes("Invalid hex quadkey format")?new Error("Invalid hex spatial id tilehash format: "+t):o}const i=r[2]?parseInt(r[2],16):0,a=e.length,c=Math.abs(i).toString(2).padStart(a,"0"),d=[];for(let o=0;o<a;o++)d.push(c.slice(o,o+1)),d.push(parseInt(e[o],4).toString(2).padStart(2,"0"));const l=d.join(""),p=BigInt("0b"+l).toString(8).padStart(a,"0").split("").map(o=>(parseInt(o)+1).toString()).join(""),S=i<0,h=[];return S&&h.push("-"),h.push(p),h.join("")},v={encodeQuadkey:f,decodeHexQuadkey:u,encodeS2HilbertQuadkey:m,decodeHexS2HilbertQuadkey:w,encodeSpatialIdTilehash:I,decodeHexSpatialIdTilehash:b};exports.decodeHexQuadkey=u;exports.decodeHexS2HilbertQuadkey=w;exports.decodeHexSpatialIdTilehash=b;exports.encodeQuadkey=f;exports.encodeS2HilbertQuadkey=m;exports.encodeSpatialIdTilehash=I;exports.quadHexer=v; //# sourceMappingURL=quad-hexer.cjs.map