254 lines
8.8 KiB
JavaScript
254 lines
8.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.logger = exports.strictParseByte = exports.strictParseShort = exports.strictParseInt32 = exports.strictParseInt = exports.strictParseLong = exports.limitedParseFloat32 = exports.limitedParseFloat = exports.handleFloat = exports.limitedParseDouble = exports.strictParseFloat32 = exports.strictParseFloat = exports.strictParseDouble = exports.expectUnion = exports.expectString = exports.expectObject = exports.expectNonNull = exports.expectByte = exports.expectShort = exports.expectInt32 = exports.expectInt = exports.expectLong = exports.expectFloat32 = exports.expectNumber = exports.expectBoolean = exports.parseBoolean = void 0;
|
|
const parseBoolean = (value) => {
|
|
switch (value) {
|
|
case "true":
|
|
return true;
|
|
case "false":
|
|
return false;
|
|
default:
|
|
throw new Error(`Unable to parse boolean value "${value}"`);
|
|
}
|
|
};
|
|
exports.parseBoolean = parseBoolean;
|
|
const expectBoolean = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (typeof value === "number") {
|
|
if (value === 0 || value === 1) {
|
|
exports.logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`));
|
|
}
|
|
if (value === 0) {
|
|
return false;
|
|
}
|
|
if (value === 1) {
|
|
return true;
|
|
}
|
|
}
|
|
if (typeof value === "string") {
|
|
const lower = value.toLowerCase();
|
|
if (lower === "false" || lower === "true") {
|
|
exports.logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`));
|
|
}
|
|
if (lower === "false") {
|
|
return false;
|
|
}
|
|
if (lower === "true") {
|
|
return true;
|
|
}
|
|
}
|
|
if (typeof value === "boolean") {
|
|
return value;
|
|
}
|
|
throw new TypeError(`Expected boolean, got ${typeof value}: ${value}`);
|
|
};
|
|
exports.expectBoolean = expectBoolean;
|
|
const expectNumber = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (typeof value === "string") {
|
|
const parsed = parseFloat(value);
|
|
if (!Number.isNaN(parsed)) {
|
|
if (String(parsed) !== String(value)) {
|
|
exports.logger.warn(stackTraceWarning(`Expected number but observed string: ${value}`));
|
|
}
|
|
return parsed;
|
|
}
|
|
}
|
|
if (typeof value === "number") {
|
|
return value;
|
|
}
|
|
throw new TypeError(`Expected number, got ${typeof value}: ${value}`);
|
|
};
|
|
exports.expectNumber = expectNumber;
|
|
const MAX_FLOAT = Math.ceil(2 ** 127 * (2 - 2 ** -23));
|
|
const expectFloat32 = (value) => {
|
|
const expected = (0, exports.expectNumber)(value);
|
|
if (expected !== undefined && !Number.isNaN(expected) && expected !== Infinity && expected !== -Infinity) {
|
|
if (Math.abs(expected) > MAX_FLOAT) {
|
|
throw new TypeError(`Expected 32-bit float, got ${value}`);
|
|
}
|
|
}
|
|
return expected;
|
|
};
|
|
exports.expectFloat32 = expectFloat32;
|
|
const expectLong = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (Number.isInteger(value) && !Number.isNaN(value)) {
|
|
return value;
|
|
}
|
|
throw new TypeError(`Expected integer, got ${typeof value}: ${value}`);
|
|
};
|
|
exports.expectLong = expectLong;
|
|
exports.expectInt = exports.expectLong;
|
|
const expectInt32 = (value) => expectSizedInt(value, 32);
|
|
exports.expectInt32 = expectInt32;
|
|
const expectShort = (value) => expectSizedInt(value, 16);
|
|
exports.expectShort = expectShort;
|
|
const expectByte = (value) => expectSizedInt(value, 8);
|
|
exports.expectByte = expectByte;
|
|
const expectSizedInt = (value, size) => {
|
|
const expected = (0, exports.expectLong)(value);
|
|
if (expected !== undefined && castInt(expected, size) !== expected) {
|
|
throw new TypeError(`Expected ${size}-bit integer, got ${value}`);
|
|
}
|
|
return expected;
|
|
};
|
|
const castInt = (value, size) => {
|
|
switch (size) {
|
|
case 32:
|
|
return Int32Array.of(value)[0];
|
|
case 16:
|
|
return Int16Array.of(value)[0];
|
|
case 8:
|
|
return Int8Array.of(value)[0];
|
|
}
|
|
};
|
|
const expectNonNull = (value, location) => {
|
|
if (value === null || value === undefined) {
|
|
if (location) {
|
|
throw new TypeError(`Expected a non-null value for ${location}`);
|
|
}
|
|
throw new TypeError("Expected a non-null value");
|
|
}
|
|
return value;
|
|
};
|
|
exports.expectNonNull = expectNonNull;
|
|
const expectObject = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (typeof value === "object" && !Array.isArray(value)) {
|
|
return value;
|
|
}
|
|
const receivedType = Array.isArray(value) ? "array" : typeof value;
|
|
throw new TypeError(`Expected object, got ${receivedType}: ${value}`);
|
|
};
|
|
exports.expectObject = expectObject;
|
|
const expectString = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (typeof value === "string") {
|
|
return value;
|
|
}
|
|
if (["boolean", "number", "bigint"].includes(typeof value)) {
|
|
exports.logger.warn(stackTraceWarning(`Expected string, got ${typeof value}: ${value}`));
|
|
return String(value);
|
|
}
|
|
throw new TypeError(`Expected string, got ${typeof value}: ${value}`);
|
|
};
|
|
exports.expectString = expectString;
|
|
const expectUnion = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return undefined;
|
|
}
|
|
const asObject = (0, exports.expectObject)(value);
|
|
const setKeys = Object.entries(asObject)
|
|
.filter(([, v]) => v != null)
|
|
.map(([k]) => k);
|
|
if (setKeys.length === 0) {
|
|
throw new TypeError(`Unions must have exactly one non-null member. None were found.`);
|
|
}
|
|
if (setKeys.length > 1) {
|
|
throw new TypeError(`Unions must have exactly one non-null member. Keys ${setKeys} were not null.`);
|
|
}
|
|
return asObject;
|
|
};
|
|
exports.expectUnion = expectUnion;
|
|
const strictParseDouble = (value) => {
|
|
if (typeof value == "string") {
|
|
return (0, exports.expectNumber)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectNumber)(value);
|
|
};
|
|
exports.strictParseDouble = strictParseDouble;
|
|
exports.strictParseFloat = exports.strictParseDouble;
|
|
const strictParseFloat32 = (value) => {
|
|
if (typeof value == "string") {
|
|
return (0, exports.expectFloat32)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectFloat32)(value);
|
|
};
|
|
exports.strictParseFloat32 = strictParseFloat32;
|
|
const NUMBER_REGEX = /(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)|(-?Infinity)|(NaN)/g;
|
|
const parseNumber = (value) => {
|
|
const matches = value.match(NUMBER_REGEX);
|
|
if (matches === null || matches[0].length !== value.length) {
|
|
throw new TypeError(`Expected real number, got implicit NaN`);
|
|
}
|
|
return parseFloat(value);
|
|
};
|
|
const limitedParseDouble = (value) => {
|
|
if (typeof value == "string") {
|
|
return parseFloatString(value);
|
|
}
|
|
return (0, exports.expectNumber)(value);
|
|
};
|
|
exports.limitedParseDouble = limitedParseDouble;
|
|
exports.handleFloat = exports.limitedParseDouble;
|
|
exports.limitedParseFloat = exports.limitedParseDouble;
|
|
const limitedParseFloat32 = (value) => {
|
|
if (typeof value == "string") {
|
|
return parseFloatString(value);
|
|
}
|
|
return (0, exports.expectFloat32)(value);
|
|
};
|
|
exports.limitedParseFloat32 = limitedParseFloat32;
|
|
const parseFloatString = (value) => {
|
|
switch (value) {
|
|
case "NaN":
|
|
return NaN;
|
|
case "Infinity":
|
|
return Infinity;
|
|
case "-Infinity":
|
|
return -Infinity;
|
|
default:
|
|
throw new Error(`Unable to parse float value: ${value}`);
|
|
}
|
|
};
|
|
const strictParseLong = (value) => {
|
|
if (typeof value === "string") {
|
|
return (0, exports.expectLong)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectLong)(value);
|
|
};
|
|
exports.strictParseLong = strictParseLong;
|
|
exports.strictParseInt = exports.strictParseLong;
|
|
const strictParseInt32 = (value) => {
|
|
if (typeof value === "string") {
|
|
return (0, exports.expectInt32)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectInt32)(value);
|
|
};
|
|
exports.strictParseInt32 = strictParseInt32;
|
|
const strictParseShort = (value) => {
|
|
if (typeof value === "string") {
|
|
return (0, exports.expectShort)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectShort)(value);
|
|
};
|
|
exports.strictParseShort = strictParseShort;
|
|
const strictParseByte = (value) => {
|
|
if (typeof value === "string") {
|
|
return (0, exports.expectByte)(parseNumber(value));
|
|
}
|
|
return (0, exports.expectByte)(value);
|
|
};
|
|
exports.strictParseByte = strictParseByte;
|
|
const stackTraceWarning = (message) => {
|
|
return String(new TypeError(message).stack || message)
|
|
.split("\n")
|
|
.slice(0, 5)
|
|
.filter((s) => !s.includes("stackTraceWarning"))
|
|
.join("\n");
|
|
};
|
|
exports.logger = {
|
|
warn: console.warn,
|
|
};
|