quad-hexer
Version:
A library for converting between quad and hex representations of geocodes.
3 lines (2 loc) • 3.68 kB
JavaScript
(function(a,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(a=typeof globalThis<"u"?globalThis:a||self,s(a.quadHexer={}))})(this,function(a){"use strict";const s=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 e=t.split("").map(c=>parseInt(c,4).toString(2).padStart(2,"0")).join(""),n=["x"],i=Math.floor(e.length/4);return e.length%4===0?n.push(BigInt("0b"+e).toString(16).padStart(i,"0")):(e.length>=4&&n.push(BigInt("0b"+e.slice(0,-2)).toString(16).padStart(i,"0")),n.push("#"),n.push(BigInt("0b"+e.slice(-2)).toString(4))),n.join("")},l=t=>{if(!t||typeof t!="string")throw new Error("Invalid hex quadkey type: "+typeof t);const r=/^x([0-9a-f]{0,16})(?:#([0-3]))?$/;if(!r.test(t))throw new Error("Invalid hex quadkey format: "+t);const e=t.match(r);if(!e[1]&&!e[2])throw new Error("Invalid hex quadkey format: "+t);const n=[];if(e[1]){const i=e[1].length*2;n.push(BigInt("0x"+e[1]).toString(4).padStart(i,"0"))}return e[2]&&n.push(e[2]),n.join("")},w=t=>{if(typeof t!="string")throw new Error("Invalid S2 hilbert quadkey type: "+typeof t);const r=/^([0-5]{1})\/([0-3]{1,30})$/;if(!r.test(t))throw new Error("Invalid S2 hilbert quadkey format: "+t);const e=t.match(r),n=[];return n.push(e[1]),n.push(s(e[2])),n.join("")},I=t=>{if(typeof t!="string")throw new Error("Invalid hex S2 hilbert quadkey type: "+typeof t);const r=/^([0-5]{1})(x[0-9a-f]{0,15}(?:#[0-3])?)$/;if(!r.test(t))throw new Error("Invalid hex S2 hilbert quadkey format: "+t);const e=t.match(r),n=[];if(n.push(e[1]),n.push("/"),e[2])try{n.push(l(e[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 n.join("")},b=t=>{if(typeof t!="string")throw new Error("Invalid spatial id tilehash type: "+typeof t);const r=/^(-?)([1-8]{1,30})$/;if(!r.test(t))throw new Error("Invalid spatial id tilehash format: "+t);const e=t.match(r),n=e[1]==="-",c=e[2].split("").map(d=>(parseInt(d)-1).toString()).join(""),h=c.length,g=BigInt("0o"+c).toString(2).padStart(h*3,"0"),S=[],u=[];for(let d=0;d<h*3;d+=3)S.push(g.slice(d,d+1)),u.push(g.slice(d+1,d+3));const m=S.join(""),f=u.join(""),o=parseInt(m,2),x=BigInt("0b"+f).toString(4).padStart(h,"0"),y=[];return y.push(s(x)),o!==0&&(y.push(n?"-":"+"),y.push(o.toString(16))),y.join("")},v=t=>{if(typeof t!="string")throw new Error("Invalid hex spatial id tilehash type: "+typeof t);const r=/^(x[0-9a-f]{0,15}(?:#[0-3])?)(?:([+-][0-9a-f]+))?$/;if(!r.test(t))throw new Error("Invalid hex spatial id tilehash format: "+t);const e=t.match(r);let n;try{n=l(e[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=e[2]?parseInt(e[2],16):0,c=n.length,h=Math.abs(i).toString(2).padStart(c,"0"),p=[];for(let o=0;o<c;o++)p.push(h.slice(o,o+1)),p.push(parseInt(n[o],4).toString(2).padStart(2,"0"));const g=p.join(""),u=BigInt("0b"+g).toString(8).padStart(c,"0").split("").map(o=>(parseInt(o)+1).toString()).join(""),m=i<0,f=[];return m&&f.push("-"),f.push(u),f.join("")},q={encodeQuadkey:s,decodeHexQuadkey:l,encodeS2HilbertQuadkey:w,decodeHexS2HilbertQuadkey:I,encodeSpatialIdTilehash:b,decodeHexSpatialIdTilehash:v};a.decodeHexQuadkey=l,a.decodeHexS2HilbertQuadkey=I,a.decodeHexSpatialIdTilehash=v,a.encodeQuadkey=s,a.encodeS2HilbertQuadkey=w,a.encodeSpatialIdTilehash=b,a.quadHexer=q,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=quad-hexer.js.map