@@ -4,28 +4,45 @@ module.exports = function stringToParts(str) {
4
4
const result = [ ] ;
5
5
6
6
let curPropertyName = '' ;
7
- let inSquareBrackets = false ;
7
+ let state = 'DEFAULT' ;
8
8
for ( let i = 0 ; i < str . length ; ++ i ) {
9
- if ( inSquareBrackets && ! / \d / . test ( str [ i ] ) && str [ i ] !== ']' ) {
10
- throw new Error ( 'Can only use numbers in square bracket path notation, got ' +
11
- 'character "' + str [ i ] + '" in path "' + str + '"' ) ;
9
+ // Fall back to treating as property name rather than bracket notation if
10
+ // square brackets contains something other than a number.
11
+ if ( state === 'IN_SQUARE_BRACKETS' && ! / \d / . test ( str [ i ] ) && str [ i ] !== ']' ) {
12
+ state = 'DEFAULT' ;
13
+ curPropertyName = result [ result . length - 1 ] + '[' + curPropertyName ;
14
+ result . splice ( result . length - 1 , 1 ) ;
12
15
}
13
16
14
- if ( str [ i ] === '.' || str [ i ] === '[' || str [ i ] === ']' ) {
15
- if ( str [ i ] === '[' ) {
16
- inSquareBrackets = true ;
17
- } else if ( str [ i ] === ']' ) {
18
- inSquareBrackets = false ;
17
+ if ( str [ i ] === '[' ) {
18
+ if ( state !== 'IMMEDIATELY_AFTER_SQUARE_BRACKETS' ) {
19
+ result . push ( curPropertyName ) ;
20
+ curPropertyName = '' ;
21
+ }
22
+ state = 'IN_SQUARE_BRACKETS' ;
23
+ } else if ( str [ i ] === ']' ) {
24
+ if ( state === 'IN_SQUARE_BRACKETS' ) {
25
+ state = 'IMMEDIATELY_AFTER_SQUARE_BRACKETS' ;
26
+ result . push ( curPropertyName ) ;
27
+ curPropertyName = '' ;
28
+ } else {
29
+ state = 'DEFAULT' ;
30
+ curPropertyName += str [ i ] ;
19
31
}
20
- if ( curPropertyName . length > 0 ) {
32
+ } else if ( str [ i ] === '.' ) {
33
+ if ( state !== 'IMMEDIATELY_AFTER_SQUARE_BRACKETS' ) {
21
34
result . push ( curPropertyName ) ;
35
+ curPropertyName = '' ;
22
36
}
23
- curPropertyName = '' ;
37
+ state = 'DEFAULT ' ;
24
38
} else {
25
39
curPropertyName += str [ i ] ;
26
40
}
27
41
}
28
- result . push ( curPropertyName ) ;
42
+
43
+ if ( state !== 'IMMEDIATELY_AFTER_SQUARE_BRACKETS' ) {
44
+ result . push ( curPropertyName ) ;
45
+ }
29
46
30
47
return result ;
31
48
} ;
0 commit comments