Skip to content

Commit

Permalink
Merge pull request #81 in ADGUARD-FILTERS/scriptlets from fix/45 to m…
Browse files Browse the repository at this point in the history
…aster

* commit '5c06f299261e228db53a7816709d5da681f0876a':
  oops, I've built it again
  uncomment tests and get previous deleting props condition back
  specify condition of deleting nested props
  fix json-prune and add one extra test for it
  • Loading branch information
slavaleleka committed Jan 21, 2020
2 parents 2484bb6 + 5c06f29 commit 396bed7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/scriptlets.corelibs.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
"json-prune.js",
"ubo-json-prune.js"
],
"scriptlet": "function(source, args){function jsonPrune(source, propsToRemove, requiredInitialProps) {var log = console.log.bind(console);var prunePaths = propsToRemove !== undefined && propsToRemove !== '' ? propsToRemove.split(/ +/) : [];var needlePaths = requiredInitialProps !== undefined && requiredInitialProps !== '' ? requiredInitialProps.split(/ +/) : [];function isPruningNeeded(root) {for (var i = 0; i < needlePaths.length; i += 1) {var needlePath = needlePaths[i];var details = getPropertyInChain(root, needlePath);var nestedPropName = needlePath.split('').pop();if (details.base[nestedPropName] === undefined) {return false;}}return true;}var nativeParse = JSON.parse;var parseWrapper = function parseWrapper() {for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}var r = nativeParse.apply(window, args);if (prunePaths.length === 0) {log(window.location.hostname, r);return r;}if (isPruningNeeded(r) === false) {return r;}prunePaths.forEach(function (path) {var ownerObj = getPropertyInChain(r, path);delete ownerObj.base[ownerObj.prop];});hit(source);return r;};JSON.parse = parseWrapper;}function hit(source, message) {if (source.verbose !== true) {return;}try {var log = console.log.bind(console);var trace = console.trace.bind(console);var prefix = source.ruleText || '';if (message) {log(\"\".concat(prefix, \" message:\\n\").concat(message));}log(\"\".concat(prefix, \" trace start\"));if (trace) {trace();}log(\"\".concat(prefix, \" trace end\"));} catch (e) {}if (typeof window.__debugScriptlets === 'function') {window.__debugScriptlets(source);}}function getPropertyInChain(base, chain) {var pos = chain.indexOf('.');if (pos === -1) {return {base: base,prop: chain};}var prop = chain.slice(0, pos);var own = base[prop];chain = chain.slice(pos + 1);if (own !== undefined) {return getPropertyInChain(own, chain);}Object.defineProperty(base, prop, {configurable: true});return {base: own,prop: prop,chain: chain};};const updatedArgs = args ? [].concat(source).concat(args) : [source];jsonPrune.apply(this, updatedArgs);}"
"scriptlet": "function(source, args){function jsonPrune(source, propsToRemove, requiredInitialProps) {var log = console.log.bind(console);var prunePaths = propsToRemove !== undefined && propsToRemove !== '' ? propsToRemove.split(/ +/) : [];var needlePaths = requiredInitialProps !== undefined && requiredInitialProps !== '' ? requiredInitialProps.split(/ +/) : [];function isPruningNeeded(root) {for (var i = 0; i < needlePaths.length; i += 1) {var needlePath = needlePaths[i];var details = getPropertyInChain(root, needlePath);var nestedPropName = needlePath.split('').pop();if (details.base[nestedPropName] === undefined) {return false;}}return true;}var nativeParse = JSON.parse;var parseWrapper = function parseWrapper() {for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}var r = nativeParse.apply(window, args);if (prunePaths.length === 0) {log(window.location.hostname, r);return r;}if (isPruningNeeded(r) === false) {return r;}prunePaths.forEach(function (path) {var ownerObj = getPropertyInChain(r, path);if (ownerObj.base) {delete ownerObj.base[ownerObj.prop];}});hit(source);return r;};JSON.parse = parseWrapper;}function hit(source, message) {if (source.verbose !== true) {return;}try {var log = console.log.bind(console);var trace = console.trace.bind(console);var prefix = source.ruleText || '';if (message) {log(\"\".concat(prefix, \" message:\\n\").concat(message));}log(\"\".concat(prefix, \" trace start\"));if (trace) {trace();}log(\"\".concat(prefix, \" trace end\"));} catch (e) {}if (typeof window.__debugScriptlets === 'function') {window.__debugScriptlets(source);}}function getPropertyInChain(base, chain) {var pos = chain.indexOf('.');if (pos === -1) {return {base: base,prop: chain};}var prop = chain.slice(0, pos);var own = base[prop];chain = chain.slice(pos + 1);if (own !== undefined) {return getPropertyInChain(own, chain);}Object.defineProperty(base, prop, {configurable: true});return {base: own,prop: prop,chain: chain};};const updatedArgs = args ? [].concat(source).concat(args) : [source];jsonPrune.apply(this, updatedArgs);}"
}
]
}
5 changes: 4 additions & 1 deletion dist/scriptlets.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/scriptlets/json-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export function jsonPrune(source, propsToRemove, requiredInitialProps) {
}
prunePaths.forEach((path) => {
const ownerObj = getPropertyInChain(r, path);
delete ownerObj.base[ownerObj.prop];
if (ownerObj.base) {
delete ownerObj.base[ownerObj.prop];
}
});
hit(source);
return r;
Expand Down
8 changes: 8 additions & 0 deletions tests/json-prune.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ test('removes nested propsToRemove', (assert) => {
{ nested: { a: 1, inner: { y: false } } }, 'should remove multiple nested propsToRemove');
});

test('can NOT remove nested propsToRemove if parental parameter is absent in the object in the first place', (assert) => {
runScriptlet('json-prune', 'nested.test');
assert.deepEqual(JSON.parse('{"a":0,"messed":{"b":1,"c":2}}'), { a: 0, messed: { b: 1, c: 2 } }, 'should NOT remove single nested propsToRemove');
runScriptlet('json-prune', 'nested.b inner.x');
assert.deepEqual(JSON.parse('{"messed":{"a":1,"b":2,"inner":{"x":true,"y":false}}}'),
{ messed: { a: 1, b: 2, inner: { x: true, y: false } } }, 'should NOT remove multiple nested propsToRemove');
});

test('can NOT remove any propsToRemove if requiredInitialProps are absent in the object', (assert) => {
runScriptlet('json-prune', 'x', 'requiredInitialProps');
assert.deepEqual(JSON.parse('{"x":1}'), { x: 1 }, 'should NOT remove propsToRemove if single requiredInitialProps is absent in the object');
Expand Down

0 comments on commit 396bed7

Please sign in to comment.