UNPKG

typia

Version:

Superfast runtime validators with only one line

35 lines 990 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.$zigzag_encode = $zigzag_encode; exports.$zigzag_decode = $zigzag_decode; function $zigzag_encode(value) { // TODO: optimize (branchless solution exists) if (typeof value === "bigint") { if (value < BigInt(0)) { value = -value; return value * BigInt(2) - BigInt(1); } return value * BigInt(2); } if (value < 0) { value = -value; return value * 2 - 1; } return value * 2; } function $zigzag_decode(value) { // TODO: optimize (branchless solution exists) if (typeof value === "bigint") { value = BigInt.asUintN(64, value); if (value & BigInt(1)) { return -(value + BigInt(1)) / BigInt(2); } return value / BigInt(2); } value = value >>> 0; if (value & 1) { return -(value + 1) / 2; } return value / 2; } //# sourceMappingURL=$zigzag.js.map