UNPKG

hilbert-geohash

Version:

Node.js implementation of Hilbert-curve based geohashing.

56 lines (55 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decode_int = exports.encode_int = void 0; const IntConversion = { // https://stackoverflow.com/questions/6213227/fastest-way-to-convert-a-number-to-radix-64-in-javascript Base64: "0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz", Base16: "0123456789abcdef", Base4: "0123", fromNumber: function (number, base) { let residual = number; let result = ""; while (true) { const digit = Number(BigInt(residual) % BigInt(base)); result = this[("Base" + base)].charAt(digit) + result; residual = BigInt(residual) / BigInt(base); if (residual == 0n) break; } return result; }, toNumber: function (string, base) { var result = 0n; let chars = string.split(""); for (var e = 0; e < chars.length; e++) { result = result * BigInt(base) + BigInt(this[("Base" + base)].indexOf(chars[e])); } return result; }, }; exports.encode_int = (n, bits_per_char = 6) => { if (bits_per_char === 6) { return IntConversion.fromNumber(n, 64); } if (bits_per_char === 4) { return IntConversion.fromNumber(n, 16); } if (bits_per_char === 2) { return IntConversion.fromNumber(n, 4); } return ""; }; exports.decode_int = (n, bits_per_char = 6) => { if (bits_per_char === 6) { return IntConversion.toNumber(n, 64); } if (bits_per_char === 4) { return IntConversion.toNumber(n, 16); } if (bits_per_char === 2) { return IntConversion.toNumber(n, 4); } return 0n; };