Skip to content

Commit e7a0f8c

Browse files
committed
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.
1 parent 664dd95 commit e7a0f8c

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

assets/resources/scriptlets.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,14 +861,33 @@ function objectFindOwnerFn(
861861
return modified;
862862
}
863863
const prop = chain.slice(0, pos);
864+
const next = chain.slice(pos + 1);
865+
let found = false;
866+
if ( prop === '[-]' && Array.isArray(owner) ) {
867+
let i = owner.length;
868+
while ( i-- ) {
869+
if ( objectFindOwnerFn(owner[i], next) === false ) { continue; }
870+
owner.splice(i, 1);
871+
found = true;
872+
}
873+
return found;
874+
}
875+
if ( prop === '{-}' && owner instanceof Object ) {
876+
for ( const key of Object.keys(owner) ) {
877+
if ( objectFindOwnerFn(owner[key], next) === false ) { continue; }
878+
delete owner[key];
879+
found = true;
880+
}
881+
return found;
882+
}
864883
if (
865884
prop === '[]' && Array.isArray(owner) ||
885+
prop === '{}' && owner instanceof Object ||
866886
prop === '*' && owner instanceof Object
867887
) {
868-
const next = chain.slice(pos + 1);
869-
let found = false;
870888
for ( const key of Object.keys(owner) ) {
871-
found = objectFindOwnerFn(owner[key], next, prune) || found;
889+
if (objectFindOwnerFn(owner[key], next, prune) === false ) { continue; }
890+
found = true;
872891
}
873892
return found;
874893
}

0 commit comments

Comments
 (0)