Skip to content

Commit

Permalink
Fix splitting props which have regex with spaces as value to match
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
AdamWr committed Feb 3, 2025
1 parent dd352d9 commit 950dac0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/helpers/prune-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const jsonPruner = (
// Delete operator leaves "undefined" in the array and it sometimes causes issues
ownerObj.base.splice(index, 1);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error while deleting array element', error);
}
} else {
Expand Down Expand Up @@ -188,7 +189,11 @@ export const getPrunePath = (props: unknown) => {
&& props !== '';

if (validPropsString) {
const parts = props.split(/ +/).map((part) => {
// Regular expression to split the properties string by spaces,
// but it should not split if there is space inside value, like in:
// 'foo.[=]./foo bar baz/ bar' or 'foo.[=]./foo bar \/ baz/ bar'
const splitRegexp = /(?<!\.\[=\]\.\/(?:[^/]|\\.)*)\s+/;
const parts = props.split(splitRegexp).map((part) => {
const splitPart = part.split(VALUE_MARKER);
const path = splitPart[0];
let value = splitPart[1] as any;
Expand Down
32 changes: 31 additions & 1 deletion tests/scriptlets/json-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,15 @@ test('removes propsToRemove with "fooBar2" value', (assert) => {
);
});

test('removes propsToRemove with "foo Bar" value', (assert) => {
runScriptlet('json-prune', 'test.*.[=]./foo Bar/');
assert.deepEqual(
JSON.parse('{"test":{"a":1,"b":"foo Bar abc","c":3,"d":"foo Bar2"}}'),
{ test: { a: 1, c: 3 } },
'should remove all keys which value is matched by /foo Bar/ regex',
);
});

test('removes video objects with "isAd" property', (assert) => {
runScriptlet('json-prune', 'videos.{-}.isAd');
const actualJson = {
Expand Down Expand Up @@ -1522,7 +1531,7 @@ test('removes video objects from videos and images when regex matches "value" pr

test('remove different elements', (assert) => {
// eslint-disable-next-line max-len
runScriptlet('json-prune', 'foo.advert foo.elements.[-].div.*.advert foo.elements.[-].div.foo.bar.[=].advert foo.elements.[-].div.[].baz.advert advert');
runScriptlet('json-prune', 'foo.advert foo.elements.[-].div.*.advert foo.elements.[-].div.foo.bar.[=].advert foo.elements.[-].div.foo.bar.[=]./Ad By Company/ foo.elements.[-].div.[].baz.advert foo.elements.[-].div.foo.bar.[=]./Ad Foo\\/bar test/ foo.elements.[-].div.foo.bar.[=]./ I am out of ideas \\/ :\\( / advert');
const actualJson = {
foo: {
element: { div: 1 },
Expand Down Expand Up @@ -1564,6 +1573,27 @@ test('remove different elements', (assert) => {
},
],
},
{
div: {
foo: {
bar: 'Ad By Company Name',
},
},
},
{
div: {
foo: {
bar: 'Ad Foo/bar test',
},
},
},
{
div: {
foo: {
bar: ' I am out of ideas / :( ',
},
},
},
{
div: {
foo: {
Expand Down

0 comments on commit 950dac0

Please # to comment.