From e7a0f8c7810c002bc7cec6a1160f8b091f8ea52e Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 12 Mar 2024 13:21:16 -0400 Subject: [PATCH] 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. --- assets/resources/scriptlets.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 61e93ce0029a2..cf19274e7fd59 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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; }