UNPKG

6.21 kBJavaScriptView Raw
1import { __read } from "tslib";
2export var parseBoolean = function (value) {
3 switch (value) {
4 case "true":
5 return true;
6 case "false":
7 return false;
8 default:
9 throw new Error("Unable to parse boolean value \"".concat(value, "\""));
10 }
11};
12export var expectBoolean = function (value) {
13 if (value === null || value === undefined) {
14 return undefined;
15 }
16 if (typeof value === "boolean") {
17 return value;
18 }
19 throw new TypeError("Expected boolean, got ".concat(typeof value));
20};
21export var expectNumber = function (value) {
22 if (value === null || value === undefined) {
23 return undefined;
24 }
25 if (typeof value === "number") {
26 return value;
27 }
28 throw new TypeError("Expected number, got ".concat(typeof value));
29};
30var MAX_FLOAT = Math.ceil(Math.pow(2, 127) * (2 - Math.pow(2, -23)));
31export var expectFloat32 = function (value) {
32 var expected = expectNumber(value);
33 if (expected !== undefined && !Number.isNaN(expected) && expected !== Infinity && expected !== -Infinity) {
34 if (Math.abs(expected) > MAX_FLOAT) {
35 throw new TypeError("Expected 32-bit float, got ".concat(value));
36 }
37 }
38 return expected;
39};
40export var expectLong = function (value) {
41 if (value === null || value === undefined) {
42 return undefined;
43 }
44 if (Number.isInteger(value) && !Number.isNaN(value)) {
45 return value;
46 }
47 throw new TypeError("Expected integer, got ".concat(typeof value));
48};
49export var expectInt = expectLong;
50export var expectInt32 = function (value) { return expectSizedInt(value, 32); };
51export var expectShort = function (value) { return expectSizedInt(value, 16); };
52export var expectByte = function (value) { return expectSizedInt(value, 8); };
53var expectSizedInt = function (value, size) {
54 var expected = expectLong(value);
55 if (expected !== undefined && castInt(expected, size) !== expected) {
56 throw new TypeError("Expected ".concat(size, "-bit integer, got ").concat(value));
57 }
58 return expected;
59};
60var castInt = function (value, size) {
61 switch (size) {
62 case 32:
63 return Int32Array.of(value)[0];
64 case 16:
65 return Int16Array.of(value)[0];
66 case 8:
67 return Int8Array.of(value)[0];
68 }
69};
70export var expectNonNull = function (value, location) {
71 if (value === null || value === undefined) {
72 if (location) {
73 throw new TypeError("Expected a non-null value for ".concat(location));
74 }
75 throw new TypeError("Expected a non-null value");
76 }
77 return value;
78};
79export var expectObject = function (value) {
80 if (value === null || value === undefined) {
81 return undefined;
82 }
83 if (typeof value === "object" && !Array.isArray(value)) {
84 return value;
85 }
86 throw new TypeError("Expected object, got ".concat(typeof value));
87};
88export var expectString = function (value) {
89 if (value === null || value === undefined) {
90 return undefined;
91 }
92 if (typeof value === "string") {
93 return value;
94 }
95 throw new TypeError("Expected string, got ".concat(typeof value));
96};
97export var expectUnion = function (value) {
98 if (value === null || value === undefined) {
99 return undefined;
100 }
101 var asObject = expectObject(value);
102 var setKeys = Object.entries(asObject)
103 .filter(function (_a) {
104 var _b = __read(_a, 2), _ = _b[0], v = _b[1];
105 return v !== null && v !== undefined;
106 })
107 .map(function (_a) {
108 var _b = __read(_a, 2), k = _b[0], _ = _b[1];
109 return k;
110 });
111 if (setKeys.length === 0) {
112 throw new TypeError("Unions must have exactly one non-null member");
113 }
114 if (setKeys.length > 1) {
115 throw new TypeError("Unions must have exactly one non-null member. Keys ".concat(setKeys, " were not null."));
116 }
117 return asObject;
118};
119export var strictParseDouble = function (value) {
120 if (typeof value == "string") {
121 return expectNumber(parseNumber(value));
122 }
123 return expectNumber(value);
124};
125export var strictParseFloat = strictParseDouble;
126export var strictParseFloat32 = function (value) {
127 if (typeof value == "string") {
128 return expectFloat32(parseNumber(value));
129 }
130 return expectFloat32(value);
131};
132var NUMBER_REGEX = /(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)|(-?Infinity)|(NaN)/g;
133var parseNumber = function (value) {
134 var matches = value.match(NUMBER_REGEX);
135 if (matches === null || matches[0].length !== value.length) {
136 throw new TypeError("Expected real number, got implicit NaN");
137 }
138 return parseFloat(value);
139};
140export var limitedParseDouble = function (value) {
141 if (typeof value == "string") {
142 return parseFloatString(value);
143 }
144 return expectNumber(value);
145};
146export var handleFloat = limitedParseDouble;
147export var limitedParseFloat = limitedParseDouble;
148export var limitedParseFloat32 = function (value) {
149 if (typeof value == "string") {
150 return parseFloatString(value);
151 }
152 return expectFloat32(value);
153};
154var parseFloatString = function (value) {
155 switch (value) {
156 case "NaN":
157 return NaN;
158 case "Infinity":
159 return Infinity;
160 case "-Infinity":
161 return -Infinity;
162 default:
163 throw new Error("Unable to parse float value: ".concat(value));
164 }
165};
166export var strictParseLong = function (value) {
167 if (typeof value === "string") {
168 return expectLong(parseNumber(value));
169 }
170 return expectLong(value);
171};
172export var strictParseInt = strictParseLong;
173export var strictParseInt32 = function (value) {
174 if (typeof value === "string") {
175 return expectInt32(parseNumber(value));
176 }
177 return expectInt32(value);
178};
179export var strictParseShort = function (value) {
180 if (typeof value === "string") {
181 return expectShort(parseNumber(value));
182 }
183 return expectShort(value);
184};
185export var strictParseByte = function (value) {
186 if (typeof value === "string") {
187 return expectByte(parseNumber(value));
188 }
189 return expectByte(value);
190};