UNPKG

quad-hexer

Version:

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

98 lines (97 loc) 4.1 kB
const u = (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(""), n = ["x"], i = Math.floor(r.length / 4); return r.length % 4 === 0 ? n.push( BigInt("0b" + r).toString(16).padStart(i, "0") ) : (r.length >= 4 && n.push( BigInt("0b" + r.slice(0, -2)).toString(16).padStart(i, "0") ), n.push("#"), n.push(BigInt("0b" + r.slice(-2)).toString(4))), n.join(""); }, S = (t) => { if (!t || typeof t != "string") throw new Error("Invalid hex quadkey type: " + typeof t); const e = /^x([0-9a-f]{0,16})(?:#([0-3]))?$/; if (!e.test(t)) throw new Error("Invalid hex quadkey format: " + t); const r = t.match(e); if (!r[1] && !r[2]) throw new Error("Invalid hex quadkey format: " + t); const n = []; if (r[1]) { const i = r[1].length * 2; n.push( BigInt("0x" + r[1]).toString(4).padStart(i, "0") ); } return r[2] && n.push(r[2]), n.join(""); }, w = (t) => { if (typeof t != "string") throw new Error("Invalid S2 hilbert quadkey type: " + typeof t); const e = /^([0-5]{1})\/([0-3]{1,30})$/; if (!e.test(t)) throw new Error("Invalid S2 hilbert quadkey format: " + t); const r = t.match(e), n = []; return n.push(r[1]), n.push(u(r[2])), n.join(""); }, I = (t) => { if (typeof t != "string") throw new Error("Invalid hex S2 hilbert quadkey type: " + typeof t); const e = /^([0-5]{1})(x[0-9a-f]{0,15}(?:#[0-3])?)$/; if (!e.test(t)) throw new Error("Invalid hex S2 hilbert quadkey format: " + t); const r = t.match(e), n = []; if (n.push(r[1]), n.push("/"), r[2]) try { n.push(S(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 n.join(""); }, v = (t) => { if (typeof t != "string") throw new Error("Invalid spatial id tilehash type: " + typeof t); const e = /^(-?)([1-8]{1,30})$/; if (!e.test(t)) throw new Error("Invalid spatial id tilehash format: " + t); const r = t.match(e), n = r[1] === "-", a = r[2].split("").map((s) => (parseInt(s) - 1).toString()).join(""), c = a.length, p = BigInt("0o" + a).toString(2).padStart(c * 3, "0"), l = [], g = []; for (let s = 0; s < c * 3; s += 3) l.push(p.slice(s, s + 1)), g.push(p.slice(s + 1, s + 3)); const y = l.join(""), h = g.join(""), o = parseInt(y, 2), m = BigInt("0b" + h).toString(4).padStart(c, "0"), f = []; return f.push(u(m)), o !== 0 && (f.push(n ? "-" : "+"), f.push(o.toString(16))), f.join(""); }, x = (t) => { if (typeof t != "string") throw new Error("Invalid hex spatial id tilehash type: " + typeof t); const e = /^(x[0-9a-f]{0,15}(?:#[0-3])?)(?:([+-][0-9a-f]+))?$/; if (!e.test(t)) throw new Error("Invalid hex spatial id tilehash format: " + t); const r = t.match(e); let n; try { n = S(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 = n.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(n[o], 4).toString(2).padStart(2, "0")); const p = d.join(""), g = BigInt("0b" + p).toString(8).padStart(a, "0").split("").map((o) => (parseInt(o) + 1).toString()).join(""), y = i < 0, h = []; return y && h.push("-"), h.push(g), h.join(""); }, q = { encodeQuadkey: u, decodeHexQuadkey: S, encodeS2HilbertQuadkey: w, decodeHexS2HilbertQuadkey: I, encodeSpatialIdTilehash: v, decodeHexSpatialIdTilehash: x }; export { S as decodeHexQuadkey, I as decodeHexS2HilbertQuadkey, x as decodeHexSpatialIdTilehash, u as encodeQuadkey, w as encodeS2HilbertQuadkey, v as encodeSpatialIdTilehash, q as quadHexer }; //# sourceMappingURL=quad-hexer.mjs.map