1
1
import { parse as parser } from 'fast-querystring'
2
2
3
- const BAD_KEYS = new Set ( [
4
- 'length' ,
5
- ...Object . getOwnPropertyNames ( Object . prototype ) ,
6
- ] )
3
+ const BAD_KEYS = new Set ( Object . getOwnPropertyNames ( Object . prototype ) )
7
4
8
5
const isPosInt = ( str ) => {
9
6
const n = Number ( str )
@@ -13,18 +10,21 @@ const isPosInt = (str) => {
13
10
const resolvePath = ( o , path , v = null , d ) => {
14
11
let next = path
15
12
let cur = o
13
+ let curIsArray
16
14
for ( let l , r , k ; d > 0 || ! next ; d -- ) {
17
15
l = next . indexOf ( '[' )
18
16
r = next . indexOf ( ']' )
19
17
if ( l === - 1 || r === - 1 || l > r ) return { [ next ] : v }
20
- k = next . slice ( l + 1 , r ) || cur ?. length || 0
18
+ curIsArray = cur instanceof Array
19
+ k = next . slice ( l + 1 , r ) || ( curIsArray ? cur . length : 0 )
21
20
next = next . slice ( r + 1 )
22
21
if ( isPosInt ( k ) ) {
23
22
if ( ! cur ) {
24
23
cur = [ ]
25
24
o = cur
25
+ curIsArray = true
26
26
}
27
- if ( ! ( cur instanceof Array ) ) {
27
+ if ( ! curIsArray ) {
28
28
if ( ! k ) k = Object . keys ( cur ) . length
29
29
cur = cur [ k ] = { }
30
30
continue
@@ -48,7 +48,7 @@ const resolvePath = (o, path, v=null, d) => {
48
48
cur = { }
49
49
o = cur
50
50
}
51
- if ( typeof cur === 'string' ) {
51
+ if ( curIsArray || typeof cur === 'string' ) {
52
52
break
53
53
}
54
54
if ( ! next ) {
@@ -69,7 +69,7 @@ const resolvePath = (o, path, v=null, d) => {
69
69
}
70
70
}
71
71
if ( next ) {
72
- if ( cur instanceof Array ) {
72
+ if ( curIsArray ) {
73
73
cur . push ( { [ next ] : v } )
74
74
} else if ( typeof cur === 'object' ) {
75
75
cur [ next ] = v
0 commit comments