@@ -20,6 +20,14 @@ const NULL_STRING = 'NULL'
20
20
function makeParseArrayWithTransform ( transform ) {
21
21
const haveTransform = transform != null
22
22
return function parseArray ( str ) {
23
+ const rbraceIndex = str . length - 1
24
+ if ( rbraceIndex === 1 ) {
25
+ return [ ]
26
+ }
27
+ if ( str [ rbraceIndex ] !== RBRACE ) {
28
+ throw new Error ( 'Invalid array text - must end with }' )
29
+ }
30
+
23
31
// If starts with `[`, it is specifying the index boundas. Skip past first `=`.
24
32
let position = 0
25
33
if ( str [ position ] === LBRACKET ) {
@@ -29,17 +37,12 @@ function makeParseArrayWithTransform (transform) {
29
37
if ( str [ position ++ ] !== LBRACE ) {
30
38
throw new Error ( 'Invalid array text - must start with {' )
31
39
}
32
- const rbraceIndex = str . length - 1
33
- if ( str [ rbraceIndex ] !== RBRACE ) {
34
- throw new Error ( 'Invalid array text - must end with }' )
35
- }
36
40
const output = [ ]
37
41
let current = output
38
42
const stack = [ ]
39
43
40
44
let currentStringStart = position
41
- const currentStringParts = [ ]
42
- let hasStringParts = false
45
+ let currentString = ''
43
46
let expectValue = true
44
47
45
48
for ( ; position < rbraceIndex ; ++ position ) {
@@ -56,8 +59,7 @@ function makeParseArrayWithTransform (transform) {
56
59
while ( backSlash !== - 1 && backSlash < dquot ) {
57
60
position = backSlash
58
61
const part = str . slice ( currentStringStart , position )
59
- currentStringParts . push ( part )
60
- hasStringParts = true
62
+ currentString += part
61
63
currentStringStart = ++ position
62
64
if ( dquot === position ++ ) {
63
65
// This was an escaped doublequote; find the next one!
@@ -68,14 +70,9 @@ function makeParseArrayWithTransform (transform) {
68
70
}
69
71
position = dquot
70
72
const part = str . slice ( currentStringStart , position )
71
- if ( hasStringParts ) {
72
- const final = currentStringParts . join ( '' ) + part
73
- current . push ( haveTransform ? transform ( final ) : final )
74
- currentStringParts . length = 0
75
- hasStringParts = false
76
- } else {
77
- current . push ( haveTransform ? transform ( part ) : part )
78
- }
73
+ currentString += part
74
+ current . push ( haveTransform ? transform ( currentString ) : currentString )
75
+ currentString = ''
79
76
expectValue = false
80
77
} else if ( char === LBRACE ) {
81
78
const newArray = [ ]
0 commit comments