@@ -14,23 +14,28 @@ import {
14
14
export function parse ( serialized ) {
15
15
const parsed = JSON . parse ( serialized ) ;
16
16
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 ) ;
22
18
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
+ }
24
24
25
25
const values = /** @type {any[] } */ ( parsed ) ;
26
26
if ( values . length === 0 ) throw new SyntaxError ( `Unexpected empty array` ) ;
27
27
28
28
const hydrated = Array ( values . length ) ;
29
29
30
30
/** @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 } ` ) ;
34
39
35
40
if ( index in hydrated ) return hydrated [ index ] ;
36
41
@@ -110,15 +115,3 @@ export function parse(serialized) {
110
115
111
116
return hydrate ( 0 ) ;
112
117
}
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