Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils/queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const setter = ({ data, key, value, force }) => {
} else {
// handles special deletion cases for obj props / array items
if (value === _delete) {
if (Array.isArray(_data)) {
if (Array.isArray(_data) && typeof _key === 'number') {
_data.splice(_key, 1)
} else {
delete _data[_key]
Expand Down
4 changes: 4 additions & 0 deletions test/lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ t.test('delete', t => {
}),
},
})
const before = readPackageJson(npm.prefix)
await npm.exec('pkg', ['delete', 'info.foo.bar.missing'])
t.strictSame(readPackageJson(npm.prefix), before,
'should leave array items unchanged when deleting a missing property')
await npm.exec('pkg', ['delete', 'info.foo.bar[0].baz'])
t.matchSnapshot(
readPackageJson(npm.prefix),
Expand Down
10 changes: 10 additions & 0 deletions test/lib/utils/queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,16 @@ t.test('delete values', async t => {
},
'should delete array item'
)

const arrays = {
keywords: ['alpha', 'beta'],
contributors: [{ name: 'alice' }, { name: 'bob' }],
}
const qq = new Queryable(structuredClone(arrays))
for (const key of ['keywords.missing', 'keywords.beta', 'contributors.missing']) {
qq.delete(key)
t.strictSame(qq.toJSON(), arrays, `should leave arrays unchanged when deleting ${key}`)
}
})

t.test('logger', async t => {
Expand Down