diff --git a/dist/scriptlets.corelibs.json b/dist/scriptlets.corelibs.json index 91a58953b..4a8fa710c 100644 --- a/dist/scriptlets.corelibs.json +++ b/dist/scriptlets.corelibs.json @@ -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);}" } ] } \ No newline at end of file diff --git a/dist/scriptlets.js b/dist/scriptlets.js index 17e92d7d3..b8ca93694 100644 --- a/dist/scriptlets.js +++ b/dist/scriptlets.js @@ -2478,7 +2478,10 @@ prunePaths.forEach(function (path) { var ownerObj = getPropertyInChain(r, path); - delete ownerObj.base[ownerObj.prop]; + + if (ownerObj.base) { + delete ownerObj.base[ownerObj.prop]; + } }); hit(source); return r; diff --git a/src/scriptlets/json-prune.js b/src/scriptlets/json-prune.js index e5f5a64ae..a6c6b20da 100644 --- a/src/scriptlets/json-prune.js +++ b/src/scriptlets/json-prune.js @@ -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; diff --git a/tests/json-prune.test.js b/tests/json-prune.test.js index 70aef8c20..965dc1a5f 100644 --- a/tests/json-prune.test.js +++ b/tests/json-prune.test.js @@ -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');