Skip to content

Commit

Permalink
Improve json-prune-related scriptlets
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Mar 12, 2024
1 parent 664dd95 commit e7a0f8c
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions assets/resources/scriptlets.js
Expand Up @@ -861,14 +861,33 @@ function objectFindOwnerFn(
return modified;
}
const prop = chain.slice(0, pos);
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) ) {
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
) {
const next = chain.slice(pos + 1);
let found = false;
for ( const key of Object.keys(owner) ) {
found = objectFindOwnerFn(owner[key], next, prune) || found;
if (objectFindOwnerFn(owner[key], next, prune) === false ) { continue; }
found = true;
}
return found;
}
Expand Down

0 comments on commit e7a0f8c

Please sign in to comment.