Skip to content

Commit d5925eb

Browse files
committed
simplify
1 parent 1a2fc54 commit d5925eb

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/parse.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@ import {
1414
export function parse(serialized) {
1515
const parsed = JSON.parse(serialized);
1616

17-
if (typeof parsed === 'number') {
18-
const constant = hydrateConstant(parsed);
19-
if (constant === NOT_A_CONSTANT) throw new SyntaxError(`Unexpected number ${parsed}`);
20-
return constant;
21-
}
17+
if (typeof parsed === 'number') return hydrate(parsed, true);
2218

23-
if (!Array.isArray(parsed)) throw new SyntaxError(`Expected array, got ${parsed === null ? 'null' : typeof parsed}`);
19+
if (!Array.isArray(parsed)) {
20+
throw new SyntaxError(
21+
`Expected array, got ${parsed === null ? 'null' : typeof parsed}`
22+
);
23+
}
2424

2525
const values = /** @type {any[]} */ (parsed);
2626
if (values.length === 0) throw new SyntaxError(`Unexpected empty array`);
2727

2828
const hydrated = Array(values.length);
2929

3030
/** @param {number} index */
31-
function hydrate(index) {
32-
const constant = hydrateConstant(index);
33-
if (constant !== NOT_A_CONSTANT) return constant;
31+
function hydrate(index, standalone = false) {
32+
if (index === UNDEFINED) return undefined;
33+
if (index === NAN) return NaN;
34+
if (index === POSITIVE_INFINITY) return Infinity;
35+
if (index === NEGATIVE_INFINITY) return -Infinity;
36+
if (index === NEGATIVE_ZERO) return -0;
37+
38+
if (standalone) throw new SyntaxError(`Unexpected number ${index}`);
3439

3540
if (index in hydrated) return hydrated[index];
3641

@@ -110,15 +115,3 @@ export function parse(serialized) {
110115

111116
return hydrate(0);
112117
}
113-
114-
const NOT_A_CONSTANT = Symbol('not a constant');
115-
116-
/** @param {number} constant */
117-
function hydrateConstant(constant) {
118-
if (constant === UNDEFINED) return undefined;
119-
if (constant === NAN) return NaN;
120-
if (constant === POSITIVE_INFINITY) return Infinity;
121-
if (constant === NEGATIVE_INFINITY) return -Infinity;
122-
if (constant === NEGATIVE_ZERO) return -0;
123-
return NOT_A_CONSTANT;
124-
}

0 commit comments

Comments
 (0)