UNPKG

@nmscd/coordinate-conversion

Version:
271 lines (270 loc) 8.7 kB
const o = { generic: "Validation failed", atleast1InputIsRequired: "You need to pass in at least 1 input", cannotBeNull: (e) => `${e} cannot be null`, unexpectedValue: (e, t, n) => `${e} is an unexpected value (${t}), expected values ${n.join(", ")}`, minLength: (e) => `Minimum length required is ${e}`, maxLength: (e) => `Text is too long! Maximum length allowed is ${e}`, minValue: (e, t) => `Minimum value for ${e} is ${t}`, maxValue: (e, t) => `Maximum value for ${e} allowed is ${t}`, unexpectedPattern: (e, t) => `${e} has a value (${t}) which does not match the expected pattern`, minNumItems: (e, t) => `Minimum number of ${e} that need to be provided is ${t}`, maxNumItems: (e, t) => `Maximum number of ${e} allowed to be provided is ${t}` }, g = (e) => () => { var n; const t = e.inputValidator(e.input); if (t.isValid === !1) return { isSuccess: !1, value: {}, errorMessage: t.errorMessage ?? o.generic }; try { return { isSuccess: !0, errorMessage: "", value: e.converter(e.input) }; } catch (r) { return { isSuccess: !1, value: {}, errorMessage: ((n = r == null ? void 0 : r.toString) == null ? void 0 : n.call(r)) ?? "Unknown exception occurred during conversion" }; } }, S = 19, C = 16, G = 12, v = -2048, b = 2048, w = 0, D = 4095, L = 0, R = 15; function F(e) { if (e.length !== S) return e; const t = 2049, n = 2047, r = 129, a = 127, s = parseInt(e.substring(0, 4), 16), i = parseInt(e.substring(5, 9), 16), c = parseInt(e.substring(10, 14), 16), f = parseInt(e.substring(15, 19), 16); let l = 0, u = 0, d = 0; s < n ? l = s + t : l = s - n, c < n ? d = c + t : d = c - n, i < a ? u = i + r : u = i - a; const y = [ "0", f.toString(16).toUpperCase().padStart(3, "0"), u.toString(16).toUpperCase().padStart(2, "0"), d.toString(16).toUpperCase().padStart(3, "0"), l.toString(16).toUpperCase().padStart(3, "0") ].join(""); return y.length === G ? y : ""; } function O(e) { const t = e.substring(9, 12), n = e.substring(4, 6), r = e.substring(6, 9), a = e.substring(1, 4), s = e.substring(0, 1); return [t, n, r, a, s]; } function Y(e) { if (e.length !== G) return {}; const n = O(e).map((d) => parseInt(d, 16)), r = n[0], a = n[1], s = n[2], i = n[3], c = n[4]; let f, l, u; return r > 2047 ? f = r - 4096 : f = r, s > 2047 ? u = s - 4096 : u = s, a > 127 ? l = a - 256 : l = a, { VoxelX: f, VoxelY: l, VoxelZ: u, SolarSystemIndex: i, PlanetIndex: c }; } function q(e) { if (e.length !== S) return {}; const t = F(e); return Y(t); } const A = (e) => { let t = ""; return e.code != null ? e.code.length === S ? t = e.code : e.code.length === C && (t = e.code.split("").flatMap((n, r) => r !== C - 1 && r % 4 === 3 ? [n, ":"] : [n]).join("")) : e.groups != null && (t = e.groups.join(":")), t; }; var I = /* @__PURE__ */ ((e) => (e[e.Glyphs = 0] = "Glyphs", e[e.GalacticCoordinates = 1] = "GalacticCoordinates", e[e.VoxelCoordinates = 2] = "VoxelCoordinates", e))(I || {}); const x = (e) => (t) => t !== null ? { isValid: !0 } : { isValid: !1, errorMessage: e ?? "Field shouldn't be empty" }, _ = (...e) => (t) => { for (const n of e) { const r = n(t); if (r.isValid === !1) return r; } return { isValid: !0 }; }, j = (...e) => (t) => { const n = (t == null ? void 0 : t.length) ?? 0; return e.includes(n) ? { isValid: !0 } : { isValid: !1, errorMessage: o.unexpectedValue("Length", n, e) }; }, p = (e, t) => (n) => { const r = n ?? "", a = e.split(""); for (const s of r) if (a.includes(s) === !1) return { isValid: !1, errorMessage: o.unexpectedValue("Character", s, a) }; return { isValid: !0 }; }, T = (e) => _( x(o.cannotBeNull(I.GalacticCoordinates.toString())), // j(C, S), p(":1234567890abcdefABCDEF") )(e), M = (e) => e.code == null && e.groups == null ? { isValid: !1, errorMessage: o.atleast1InputIsRequired } : e.code != null ? T(e.code) : { isValid: !0 }, W = (e) => ({ toGlyph: g({ input: e, inputValidator: M, converter: (t) => { const n = A(t), r = F(n); return { code: r, hexArray: r.split("") }; } }), toVoxel: g({ input: e, inputValidator: M, converter: (t) => { const n = A(t); return q(n); } }) }); function z(e) { if (e.length !== G) return ""; const t = 2049, n = 2047, r = 129, a = 127, s = parseInt(e.substring(9, 12), 16), i = parseInt(e.substring(4, 6), 16), c = parseInt(e.substring(6, 9), 16), f = e.substring(1, 4); let l = 0, u = 0, d = 0; s >= t ? l = s - t : l = s + n, c >= t ? d = c - t : d = c + n, i >= r ? u = i - r : u = i + a; const y = [l, u, d].map((U) => U.toString(16).toUpperCase().padStart(4, "0")); return y[3] = f.padStart(4, "0").toUpperCase(), y.join(":"); } const N = (e) => { var n; let t = ""; return e.code != null ? t = e.code : e.hexArray != null ? t = e.hexArray.join("") : e.numberArray != null && (t = e.numberArray.map((r) => r.toString(16)).join("")), (n = t == null ? void 0 : t.toLocaleLowerCase) == null ? void 0 : n.call(t); }, X = (e, t = []) => e == null ? t : Array.isArray(e) == !0 ? [...e] : e != null ? [e] : t, Z = (e, t) => (n) => X(n).length >= e ? { isValid: !0 } : { isValid: !1, errorMessage: o.minNumItems("item", e) }, E = (e, t) => (n) => X(n).length < e ? { isValid: !0 } : { isValid: !1, errorMessage: o.maxNumItems("item", e) }, k = (e) => _( x(o.cannotBeNull(I.Glyphs.toString())), // p("1234567890abcdefABCDEF"), j(12) )(e), J = (e) => _( x(o.cannotBeNull(I.Glyphs.toString())), // (n) => p("1234567890abcdefABCDEF")(n.join("")), Z(12), E(12) )(e), K = (e) => _( x(o.cannotBeNull(I.Glyphs.toString())), // (n) => p("1234567890abcdefABCDEF")(n.map((r) => r.toString(16)).join("")), Z(12), E(12) )(e), $ = (e) => e.code == null && e.hexArray == null && e.numberArray == null ? { isValid: !1, errorMessage: o.atleast1InputIsRequired } : e.code != null ? k(e.code) : e.hexArray != null ? J(e.hexArray) : e.numberArray != null ? K(e.numberArray) : { isValid: !0 }, P = (e) => ({ toGalacticCoordinates: g({ input: e, inputValidator: $, converter: (t) => { const n = N(t), r = z(n); return { code: r, groups: r.split(":") }; } }), toVoxel: g({ input: e, inputValidator: $, converter: (t) => { const n = N(t); return Y(n); } }) }); function H({ VoxelX: e, VoxelY: t, VoxelZ: n, SolarSystemIndex: r, PlanetIndex: a }) { let s, i, c; e < 0 ? s = e + 4096 : s = e, n < 0 ? c = n + 4096 : c = n, t < 0 ? i = t + 256 : i = t; const l = [r, i, c, s].map((m) => m.toString(16).toUpperCase()), u = l.splice(1, 1)[0].padStart(2, "0"), d = l.map((m) => m.padStart(3, "0")); return d.splice(1, 0, u), `${a}${d.join("")}`; } function Q(e) { const t = H(e); return z(t); } const V = (e, t) => (n) => (n ?? 0) >= e ? { isValid: !0 } : { isValid: !1, errorMessage: o.minValue(t ?? "property", e) }, h = (e, t) => (n) => (n ?? 0) < e ? { isValid: !0 } : { isValid: !1, errorMessage: o.maxValue(t ?? "property", e) }, B = (e) => { const t = x(o.cannotBeNull("voxelInput"))(e); if (t.isValid === !1) return t; const n = [ () => x(o.cannotBeNull("VoxelX"))(e.VoxelX), () => V(v, "VoxelX")(e.VoxelX), () => h(b, "VoxelX")(e.VoxelX), // () => x(o.cannotBeNull("VoxelY"))(e.VoxelY), () => V(v, "VoxelY")(e.VoxelY), () => h(b, "VoxelY")(e.VoxelY), // () => x(o.cannotBeNull("VoxelZ"))(e.VoxelZ), () => V(v, "VoxelZ")(e.VoxelZ), () => h(b, "VoxelZ")(e.VoxelZ), // () => x(o.cannotBeNull("SolarSystemIndex"))(e.SolarSystemIndex), () => V(w, "SolarSystemIndex")(e.SolarSystemIndex), () => h(D, "SolarSystemIndex")(e.SolarSystemIndex), // () => x(o.cannotBeNull("PlanetIndex"))(e.PlanetIndex), () => V(L, "PlanetIndex")(e.PlanetIndex), () => h(R, "PlanetIndex")(e.PlanetIndex) ]; for (const r of n) { const a = r(); if (a.isValid !== !0) return a; } return { isValid: !0 }; }, ee = (e) => ({ toGlyph: g({ input: e, inputValidator: B, converter: (t) => { const n = H(t); return { code: n, hexArray: n.split("") }; } }), toGalacticCoordinates: g({ input: e, inputValidator: B, converter: (t) => { const n = Q(t); return { code: n, groups: n.split(":") }; } }) }), te = () => "1.2.4"; export { W as GalacticCoordinate, P as PortalCode, ee as VoxelCoordinate, te as getPackageVersion };