Improve json-prune-related scriptlets

New special properties:
- `[-]`: remove an array entry if part right of `[-]` matches the
  inspected item.
- `{-}`: remove a property if part right of `{-}` mmatches the
  inspected item.

This is useful to remove entries which have unspecified names.
This commit is contained in:
Raymond Hill 2024-03-12 13:21:16 -04:00
parent 664dd95700
commit e7a0f8c781
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 22 additions and 3 deletions

View File

@ -861,14 +861,33 @@ function objectFindOwnerFn(
return modified;
}
const prop = chain.slice(0, pos);
if (
prop === '[]' && Array.isArray(owner) ||
prop === '*' && owner instanceof Object
) {
const next = chain.slice(pos + 1);
let found = false;
if ( prop === '[-]' && Array.isArray(owner) ) {
let i = owner.length;
while ( i-- ) {
if ( objectFindOwnerFn(owner[i], next) === false ) { continue; }
owner.splice(i, 1);
found = true;
}
return found;
}
if ( prop === '{-}' && owner instanceof Object ) {
for ( const key of Object.keys(owner) ) {
found = objectFindOwnerFn(owner[key], next, prune) || found;
if ( objectFindOwnerFn(owner[key], next) === false ) { continue; }
delete owner[key];
found = true;
}
return found;
}
if (
prop === '[]' && Array.isArray(owner) ||
prop === '{}' && owner instanceof Object ||
prop === '*' && owner instanceof Object
) {
for ( const key of Object.keys(owner) ) {
if (objectFindOwnerFn(owner[key], next, prune) === false ) { continue; }
found = true;
}
return found;
}