Skip to content

Commit 685181d

Browse files
committed
Better object key process
1 parent 7297db6 commit 685181d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

index.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { parse as parser } from 'fast-querystring'
22

3-
const BAD_KEYS = new Set([
4-
'length',
5-
...Object.getOwnPropertyNames(Object.prototype),
6-
])
3+
const BAD_KEYS = new Set(Object.getOwnPropertyNames(Object.prototype))
74

85
const isPosInt = (str) => {
96
const n = Number(str)
@@ -13,18 +10,21 @@ const isPosInt = (str) => {
1310
const resolvePath = (o, path, v=null, d) => {
1411
let next = path
1512
let cur = o
13+
let curIsArray
1614
for (let l, r, k; d > 0 || !next; d--) {
1715
l = next.indexOf('[')
1816
r = next.indexOf(']')
1917
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)
2120
next = next.slice(r + 1)
2221
if (isPosInt(k)) {
2322
if (!cur) {
2423
cur = []
2524
o = cur
25+
curIsArray = true
2626
}
27-
if (!(cur instanceof Array)) {
27+
if (!curIsArray) {
2828
if (!k) k = Object.keys(cur).length
2929
cur = cur[k] = {}
3030
continue
@@ -48,7 +48,7 @@ const resolvePath = (o, path, v=null, d) => {
4848
cur = {}
4949
o = cur
5050
}
51-
if (typeof cur === 'string') {
51+
if (curIsArray || typeof cur === 'string') {
5252
break
5353
}
5454
if (!next) {
@@ -69,7 +69,7 @@ const resolvePath = (o, path, v=null, d) => {
6969
}
7070
}
7171
if (next) {
72-
if (cur instanceof Array) {
72+
if (curIsArray) {
7373
cur.push({ [next]: v })
7474
} else if (typeof cur === 'object') {
7575
cur[next] = v

0 commit comments

Comments
 (0)