dot-object
Advanced tools
Comparing version 2.1.2 to 2.1.3
{ | ||
"name": "dot-object", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
@@ -5,0 +5,0 @@ "main": "dist/dot-object.js", |
# ChangeLog | ||
## 2020-16-02 Version 2.1.3 | ||
* fix possible pollution of prototype for paths containing __proto__ | ||
## 2019-11-02 Version 2.1.1 | ||
@@ -4,0 +7,0 @@ * fix undefined key with root level array. |
@@ -49,2 +49,7 @@ (function(global, exportName) { | ||
var blacklist = ['__proto__', 'prototype', 'constructor'] | ||
var blacklistFilter = function(part) { | ||
return blacklist.indexOf(part) === -1 | ||
} | ||
function parsePath(path, sep) { | ||
@@ -54,3 +59,12 @@ if (path.indexOf('[') >= 0) { | ||
} | ||
return path.split(sep) | ||
var parts = path.split(sep) | ||
var check = parts.filter(blacklistFilter) | ||
if (check.length !== parts.length) { | ||
throw Error('Refusing to update blacklisted property ' + path) | ||
} | ||
return parts | ||
} | ||
@@ -90,4 +104,3 @@ | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || | ||
(this.useArray && isIndex(a[0]) ? [] : {}) | ||
obj[k] = obj[k] || (this.useArray && isIndex(a[0]) ? [] : {}) | ||
@@ -110,4 +123,3 @@ if (!isArrayOrObject(obj[k])) { | ||
} else { | ||
if (!this.override && | ||
isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!this.override && isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!(isArrayOrObject(v) && isEmptyObject(v))) { | ||
@@ -204,3 +216,3 @@ throw new Error("Trying to redefine non-empty obj['" + k + "']") | ||
if (obj && typeof obj === 'object' && key in obj) { | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (remove) { | ||
@@ -350,9 +362,17 @@ val = obj[key] | ||
*/ | ||
DotObject.prototype.transfer = function(source, target, obj1, obj2, mods, merge) { | ||
DotObject.prototype.transfer = function( | ||
source, | ||
target, | ||
obj1, | ||
obj2, | ||
mods, | ||
merge | ||
) { | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
this.pick(source, obj1, true), | ||
mods | ||
), obj2, merge) | ||
this.set( | ||
target, | ||
_process(this.pick(source, obj1, true), mods), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -382,12 +402,12 @@ merge = mods | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
this.set( | ||
target, | ||
_process( | ||
// clone what is picked | ||
JSON.parse( | ||
JSON.stringify( | ||
this.pick(source, obj1, false) | ||
) | ||
), | ||
JSON.parse(JSON.stringify(this.pick(source, obj1, false))), | ||
mods | ||
), obj2, merge) | ||
), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -424,3 +444,3 @@ merge = mods | ||
key = keys[i] | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (merge && isObject(val) && isObject(obj[key])) { | ||
@@ -483,5 +503,7 @@ for (k in val) { | ||
tgt = tgt || {} | ||
Object.keys(recipe).forEach(function(key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this)) | ||
Object.keys(recipe).forEach( | ||
function(key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -512,27 +534,29 @@ } | ||
Object.keys(obj).forEach(function(key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
( | ||
Object.keys(obj).forEach( | ||
function(key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
isArrayOrObject(obj[key]) && | ||
( | ||
(isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && (!this.keepArray && (obj[key].length !== 0))) | ||
) | ||
) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(previousKey + index)) | ||
((isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && !this.keepArray && obj[key].length !== 0)) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot( | ||
obj[key], | ||
tgt, | ||
path.slice(0, -1).concat(previousKey + index) | ||
) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
} else { | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
}.bind(this)) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -551,5 +575,3 @@ } | ||
DotObject.del = DotObject.remove = wrap('remove') | ||
DotObject.dot = wrap('dot') | ||
; | ||
DotObject.dot = wrap('dot'); | ||
['override', 'overwrite'].forEach(function(prop) { | ||
@@ -564,5 +586,3 @@ Object.defineProperty(DotObject, prop, { | ||
}) | ||
}) | ||
; | ||
}); | ||
['useArray', 'keepArray', 'useBrackets'].forEach(function(prop) { | ||
@@ -569,0 +589,0 @@ Object.defineProperty(DotObject, prop, { |
@@ -1,1 +0,1 @@ | ||
!function(t){"use strict";function s(t,r){var e,i;if("function"==typeof r)void 0!==(i=r(t))&&(t=i);else if(Array.isArray(r))for(e=0;e<r.length;e++)void 0!==(i=r[e](t))&&(t=i);return t}function f(t){return"[object Object]"===Object.prototype.toString.call(t)}function a(t){return Object(t)===t}function c(t){return 0===Object.keys(t).length}function u(t,r){return 0<=t.indexOf("[")&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(r)}var p=Object.prototype.hasOwnProperty;function n(t,r,e,i){if(!(this instanceof n))return new n(t,r,e,i);void 0===r&&(r=!1),void 0===e&&(e=!0),void 0===i&&(i=!0),this.separator=t||".",this.override=r,this.useArray=e,this.useBrackets=i,this.keepArray=!1,this.cleanup=[]}var e=new n(".",!1,!0,!0);function r(t){return function(){return e[t].apply(e,arguments)}}n.prototype._fill=function(t,r,e,i){var n=t.shift();if(0<t.length){if(r[n]=r[n]||(this.useArray&&function(t){return/^\d+$/.test(t)}(t[0])?[]:{}),!a(r[n])){if(!this.override){if(!a(e)||!c(e))throw new Error("Trying to redefine `"+n+"` which is a "+typeof r[n]);return}r[n]={}}this._fill(t,r[n],e,i)}else{if(!this.override&&a(r[n])&&!c(r[n])){if(!a(e)||!c(e))throw new Error("Trying to redefine non-empty obj['"+n+"']");return}r[n]=s(e,i)}},n.prototype.object=function(i,n){var o=this;return Object.keys(i).forEach(function(t){var r=void 0===n?null:n[t],e=u(t,o.separator).join(o.separator);-1!==e.indexOf(o.separator)?(o._fill(e.split(o.separator),i,i[t],r),delete i[t]):i[t]=s(i[t],r)}),i},n.prototype.str=function(t,r,e,i){var n=u(t,this.separator).join(this.separator);return-1!==t.indexOf(this.separator)?this._fill(n.split(this.separator),e,r,i):e[t]=s(r,i),e},n.prototype.pick=function(t,r,e,i){var n,o,s,a,c,f,p;for(o=u(t,this.separator),n=0;n<o.length;n++){if(f=o[n],p=r,a="-"===f[0]&&Array.isArray(p)&&/^-\d+$/.test(f)?p.length+parseInt(f,10):f,!(r&&"object"==typeof r&&a in r))return;if(n===o.length-1)return e?(s=r[a],i&&Array.isArray(r)?r.splice(a,1):delete r[a],Array.isArray(r)&&(c=o.slice(0,-1).join("."),-1===this.cleanup.indexOf(c)&&this.cleanup.push(c)),s):r[a];r=r[a]}return e&&Array.isArray(r)&&(r=r.filter(function(t){return void 0!==t})),r},n.prototype.delete=function(t,r){return this.remove(t,r,!0)},n.prototype.remove=function(t,r,e){var i;if(this.cleanup=[],Array.isArray(t)){for(i=0;i<t.length;i++)this.pick(t[i],r,!0,e);return e||this._cleanup(r),r}return this.pick(t,r,!0,e)},n.prototype._cleanup=function(t){var r,e,i,n;if(this.cleanup.length){for(e=0;e<this.cleanup.length;e++)r=(r=(n=(i=this.cleanup[e].split(".")).splice(0,-1).join("."))?this.pick(n,t):t)[i[0]].filter(function(t){return void 0!==t}),this.set(this.cleanup[e],r,t);this.cleanup=[]}},n.prototype.del=n.prototype.remove,n.prototype.move=function(t,r,e,i,n){return"function"==typeof i||Array.isArray(i)?this.set(r,s(this.pick(t,e,!0),i),e,n):(n=i,this.set(r,this.pick(t,e,!0),e,n)),e},n.prototype.transfer=function(t,r,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(r,s(this.pick(t,e,!0),n),i,o):(o=n,this.set(r,this.pick(t,e,!0),i,o)),i},n.prototype.copy=function(t,r,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(r,s(JSON.parse(JSON.stringify(this.pick(t,e,!1))),n),i,o):(o=n,this.set(r,this.pick(t,e,!1),i,o)),i},n.prototype.set=function(t,r,e,i){var n,o,s,a;if(void 0===r)return e;for(s=u(t,this.separator),n=0;n<s.length;n++){if(a=s[n],n===s.length-1)if(i&&f(r)&&f(e[a]))for(o in r)p.call(r,o)&&(e[a][o]=r[o]);else if(i&&Array.isArray(e[a])&&Array.isArray(r))for(var c=0;c<r.length;c++)e[s[n]].push(r[c]);else e[a]=r;else p.call(e,a)&&(f(e[a])||Array.isArray(e[a]))||(/^\d+$/.test(s[n+1])?e[a]=[]:e[a]={});e=e[a]}return e},n.prototype.transform=function(r,e,i){return e=e||{},i=i||{},Object.keys(r).forEach(function(t){this.set(r[t],this.pick(t,e),i)}.bind(this)),i},n.prototype.dot=function(i,n,o){n=n||{},o=o||[];var s=Array.isArray(i);return Object.keys(i).forEach(function(t){var r=s&&this.useBrackets?"["+t+"]":t;if(a(i[t])&&(f(i[t])&&!c(i[t])||Array.isArray(i[t])&&!this.keepArray&&0!==i[t].length)){if(s&&this.useBrackets){var e=o[o.length-1]||"";return this.dot(i[t],n,o.slice(0,-1).concat(e+r))}return this.dot(i[t],n,o.concat(r))}s&&this.useBrackets?n[o.join(this.separator).concat("["+t+"]")]=i[t]:n[o.concat(r).join(this.separator)]=i[t]}.bind(this)),n},n.pick=r("pick"),n.move=r("move"),n.transfer=r("transfer"),n.transform=r("transform"),n.copy=r("copy"),n.object=r("object"),n.str=r("str"),n.set=r("set"),n.delete=r("delete"),n.del=n.remove=r("remove"),n.dot=r("dot"),["override","overwrite"].forEach(function(t){Object.defineProperty(n,t,{get:function(){return e.override},set:function(t){e.override=!!t}})}),["useArray","keepArray","useBrackets"].forEach(function(r){Object.defineProperty(n,r,{get:function(){return e[r]},set:function(t){e[r]=t}})}),n._process=s,"function"==typeof define&&define.amd?define(function(){return n}):"undefined"!=typeof module&&module.exports?module.exports=n:t.DotObject=n}(this); | ||
!function(t){"use strict";function s(t,r){var e,i;if("function"==typeof r)void 0!==(i=r(t))&&(t=i);else if(Array.isArray(r))for(e=0;e<r.length;e++)void 0!==(i=r[e](t))&&(t=i);return t}function p(t){return"[object Object]"===Object.prototype.toString.call(t)}function a(t){return Object(t)===t}function c(t){return 0===Object.keys(t).length}function i(t){return-1===r.indexOf(t)}var r=["__proto__","prototype","constructor"];function u(t,r){0<=t.indexOf("[")&&(t=t.replace(/\[/g,".").replace(/]/g,""));var e=t.split(r);if(e.filter(i).length!==e.length)throw Error("Refusing to update blacklisted property "+t);return e}var f=Object.prototype.hasOwnProperty;function n(t,r,e,i){if(!(this instanceof n))return new n(t,r,e,i);void 0===r&&(r=!1),void 0===e&&(e=!0),void 0===i&&(i=!0),this.separator=t||".",this.override=r,this.useArray=e,this.useBrackets=i,this.keepArray=!1,this.cleanup=[]}var e=new n(".",!1,!0,!0);function o(t){return function(){return e[t].apply(e,arguments)}}n.prototype._fill=function(t,r,e,i){var n=t.shift();if(0<t.length){if(r[n]=r[n]||(this.useArray&&function(t){return/^\d+$/.test(t)}(t[0])?[]:{}),!a(r[n])){if(!this.override){if(!a(e)||!c(e))throw new Error("Trying to redefine `"+n+"` which is a "+typeof r[n]);return}r[n]={}}this._fill(t,r[n],e,i)}else{if(!this.override&&a(r[n])&&!c(r[n])){if(!a(e)||!c(e))throw new Error("Trying to redefine non-empty obj['"+n+"']");return}r[n]=s(e,i)}},n.prototype.object=function(i,n){var o=this;return Object.keys(i).forEach(function(t){var r=void 0===n?null:n[t],e=u(t,o.separator).join(o.separator);-1!==e.indexOf(o.separator)?(o._fill(e.split(o.separator),i,i[t],r),delete i[t]):i[t]=s(i[t],r)}),i},n.prototype.str=function(t,r,e,i){var n=u(t,this.separator).join(this.separator);return-1!==t.indexOf(this.separator)?this._fill(n.split(this.separator),e,r,i):e[t]=s(r,i),e},n.prototype.pick=function(t,r,e,i){var n,o,s,a,c,p,f;for(o=u(t,this.separator),n=0;n<o.length;n++){if(p=o[n],f=r,a="-"===p[0]&&Array.isArray(f)&&/^-\d+$/.test(p)?f.length+parseInt(p,10):p,!(r&&"object"==typeof r&&a in r))return;if(n===o.length-1)return e?(s=r[a],i&&Array.isArray(r)?r.splice(a,1):delete r[a],Array.isArray(r)&&(c=o.slice(0,-1).join("."),-1===this.cleanup.indexOf(c)&&this.cleanup.push(c)),s):r[a];r=r[a]}return e&&Array.isArray(r)&&(r=r.filter(function(t){return void 0!==t})),r},n.prototype.delete=function(t,r){return this.remove(t,r,!0)},n.prototype.remove=function(t,r,e){var i;if(this.cleanup=[],Array.isArray(t)){for(i=0;i<t.length;i++)this.pick(t[i],r,!0,e);return e||this._cleanup(r),r}return this.pick(t,r,!0,e)},n.prototype._cleanup=function(t){var r,e,i,n;if(this.cleanup.length){for(e=0;e<this.cleanup.length;e++)r=(r=(n=(i=this.cleanup[e].split(".")).splice(0,-1).join("."))?this.pick(n,t):t)[i[0]].filter(function(t){return void 0!==t}),this.set(this.cleanup[e],r,t);this.cleanup=[]}},n.prototype.del=n.prototype.remove,n.prototype.move=function(t,r,e,i,n){return"function"==typeof i||Array.isArray(i)?this.set(r,s(this.pick(t,e,!0),i),e,n):(n=i,this.set(r,this.pick(t,e,!0),e,n)),e},n.prototype.transfer=function(t,r,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(r,s(this.pick(t,e,!0),n),i,o):(o=n,this.set(r,this.pick(t,e,!0),i,o)),i},n.prototype.copy=function(t,r,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(r,s(JSON.parse(JSON.stringify(this.pick(t,e,!1))),n),i,o):(o=n,this.set(r,this.pick(t,e,!1),i,o)),i},n.prototype.set=function(t,r,e,i){var n,o,s,a;if(void 0===r)return e;for(s=u(t,this.separator),n=0;n<s.length;n++){if(a=s[n],n===s.length-1)if(i&&p(r)&&p(e[a]))for(o in r)f.call(r,o)&&(e[a][o]=r[o]);else if(i&&Array.isArray(e[a])&&Array.isArray(r))for(var c=0;c<r.length;c++)e[s[n]].push(r[c]);else e[a]=r;else f.call(e,a)&&(p(e[a])||Array.isArray(e[a]))||(/^\d+$/.test(s[n+1])?e[a]=[]:e[a]={});e=e[a]}return e},n.prototype.transform=function(r,e,i){return e=e||{},i=i||{},Object.keys(r).forEach(function(t){this.set(r[t],this.pick(t,e),i)}.bind(this)),i},n.prototype.dot=function(i,n,o){n=n||{},o=o||[];var s=Array.isArray(i);return Object.keys(i).forEach(function(t){var r=s&&this.useBrackets?"["+t+"]":t;if(a(i[t])&&(p(i[t])&&!c(i[t])||Array.isArray(i[t])&&!this.keepArray&&0!==i[t].length)){if(s&&this.useBrackets){var e=o[o.length-1]||"";return this.dot(i[t],n,o.slice(0,-1).concat(e+r))}return this.dot(i[t],n,o.concat(r))}s&&this.useBrackets?n[o.join(this.separator).concat("["+t+"]")]=i[t]:n[o.concat(r).join(this.separator)]=i[t]}.bind(this)),n},n.pick=o("pick"),n.move=o("move"),n.transfer=o("transfer"),n.transform=o("transform"),n.copy=o("copy"),n.object=o("object"),n.str=o("str"),n.set=o("set"),n.delete=o("delete"),n.del=n.remove=o("remove"),n.dot=o("dot"),["override","overwrite"].forEach(function(t){Object.defineProperty(n,t,{get:function(){return e.override},set:function(t){e.override=!!t}})}),["useArray","keepArray","useBrackets"].forEach(function(r){Object.defineProperty(n,r,{get:function(){return e[r]},set:function(t){e[r]=t}})}),n._process=s,"function"==typeof define&&define.amd?define(function(){return n}):"undefined"!=typeof module&&module.exports?module.exports=n:t.DotObject=n}(this); |
138
index.js
@@ -48,2 +48,5 @@ 'use strict' | ||
var blacklist = ['__proto__', 'prototype', 'constructor'] | ||
var blacklistFilter = function (part) { return blacklist.indexOf(part) === -1 } | ||
function parsePath (path, sep) { | ||
@@ -53,3 +56,12 @@ if (path.indexOf('[') >= 0) { | ||
} | ||
return path.split(sep) | ||
var parts = path.split(sep) | ||
var check = parts.filter(blacklistFilter) | ||
if (check.length !== parts.length) { | ||
throw Error('Refusing to update blacklisted property ' + path) | ||
} | ||
return parts | ||
} | ||
@@ -88,4 +100,3 @@ | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || | ||
(this.useArray && isIndex(a[0]) ? [] : {}) | ||
obj[k] = obj[k] || (this.useArray && isIndex(a[0]) ? [] : {}) | ||
@@ -108,4 +119,3 @@ if (!isArrayOrObject(obj[k])) { | ||
} else { | ||
if (!this.override && | ||
isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!this.override && isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!(isArrayOrObject(v) && isEmptyObject(v))) { | ||
@@ -202,3 +212,3 @@ throw new Error("Trying to redefine non-empty obj['" + k + "']") | ||
if (obj && typeof obj === 'object' && key in obj) { | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (remove) { | ||
@@ -229,3 +239,5 @@ val = obj[key] | ||
if (remove && Array.isArray(obj)) { | ||
obj = obj.filter(function (n) { return n !== undefined }) | ||
obj = obj.filter(function (n) { | ||
return n !== undefined | ||
}) | ||
} | ||
@@ -288,3 +300,5 @@ return obj | ||
ret = root ? this.pick(root, obj) : obj | ||
ret = ret[keys[0]].filter(function (v) { return v !== undefined }) | ||
ret = ret[keys[0]].filter(function (v) { | ||
return v !== undefined | ||
}) | ||
this.set(this.cleanup[i], ret, obj) | ||
@@ -346,9 +360,17 @@ } | ||
*/ | ||
DotObject.prototype.transfer = function (source, target, obj1, obj2, mods, merge) { | ||
DotObject.prototype.transfer = function ( | ||
source, | ||
target, | ||
obj1, | ||
obj2, | ||
mods, | ||
merge | ||
) { | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
this.pick(source, obj1, true), | ||
mods | ||
), obj2, merge) | ||
this.set( | ||
target, | ||
_process(this.pick(source, obj1, true), mods), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -378,12 +400,12 @@ merge = mods | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
this.set( | ||
target, | ||
_process( | ||
// clone what is picked | ||
JSON.parse( | ||
JSON.stringify( | ||
this.pick(source, obj1, false) | ||
) | ||
), | ||
JSON.parse(JSON.stringify(this.pick(source, obj1, false))), | ||
mods | ||
), obj2, merge) | ||
), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -420,3 +442,3 @@ merge = mods | ||
key = keys[i] | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (merge && isObject(val) && isObject(obj[key])) { | ||
@@ -460,5 +482,5 @@ for (k in val) { | ||
* "id": 1, | ||
* "some": { | ||
* "thing": "else" | ||
* } | ||
* "some": { | ||
* "thing": "else" | ||
* } | ||
* } | ||
@@ -468,3 +490,3 @@ * | ||
* "id": "nr", | ||
* "some.thing": "name" | ||
* "some.thing": "name" | ||
* } | ||
@@ -481,5 +503,7 @@ * | ||
tgt = tgt || {} | ||
Object.keys(recipe).forEach(function (key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this)) | ||
Object.keys(recipe).forEach( | ||
function (key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -510,27 +534,29 @@ } | ||
Object.keys(obj).forEach(function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
( | ||
Object.keys(obj).forEach( | ||
function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
isArrayOrObject(obj[key]) && | ||
( | ||
(isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && (!this.keepArray && (obj[key].length !== 0))) | ||
) | ||
) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(previousKey + index)) | ||
((isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && !this.keepArray && obj[key].length !== 0)) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot( | ||
obj[key], | ||
tgt, | ||
path.slice(0, -1).concat(previousKey + index) | ||
) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
} else { | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
}.bind(this)) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -549,5 +575,4 @@ } | ||
DotObject.del = DotObject.remove = wrap('remove') | ||
DotObject.dot = wrap('dot') | ||
;['override', 'overwrite'].forEach(function (prop) { | ||
DotObject.dot = wrap('dot'); | ||
['override', 'overwrite'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -561,5 +586,4 @@ get: function () { | ||
}) | ||
}) | ||
;['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
}); | ||
['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -566,0 +590,0 @@ get: function () { |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Rob Halff", |
@@ -48,2 +48,5 @@ 'use strict' | ||
var blacklist = ['__proto__', 'prototype', 'constructor'] | ||
var blacklistFilter = function (part) { return blacklist.indexOf(part) === -1 } | ||
function parsePath (path, sep) { | ||
@@ -53,3 +56,12 @@ if (path.indexOf('[') >= 0) { | ||
} | ||
return path.split(sep) | ||
var parts = path.split(sep) | ||
var check = parts.filter(blacklistFilter) | ||
if (check.length !== parts.length) { | ||
throw Error('Refusing to update blacklisted property ' + path) | ||
} | ||
return parts | ||
} | ||
@@ -88,4 +100,3 @@ | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || | ||
(this.useArray && isIndex(a[0]) ? [] : {}) | ||
obj[k] = obj[k] || (this.useArray && isIndex(a[0]) ? [] : {}) | ||
@@ -108,4 +119,3 @@ if (!isArrayOrObject(obj[k])) { | ||
} else { | ||
if (!this.override && | ||
isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!this.override && isArrayOrObject(obj[k]) && !isEmptyObject(obj[k])) { | ||
if (!(isArrayOrObject(v) && isEmptyObject(v))) { | ||
@@ -202,3 +212,3 @@ throw new Error("Trying to redefine non-empty obj['" + k + "']") | ||
if (obj && typeof obj === 'object' && key in obj) { | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (remove) { | ||
@@ -229,3 +239,5 @@ val = obj[key] | ||
if (remove && Array.isArray(obj)) { | ||
obj = obj.filter(function (n) { return n !== undefined }) | ||
obj = obj.filter(function (n) { | ||
return n !== undefined | ||
}) | ||
} | ||
@@ -288,3 +300,5 @@ return obj | ||
ret = root ? this.pick(root, obj) : obj | ||
ret = ret[keys[0]].filter(function (v) { return v !== undefined }) | ||
ret = ret[keys[0]].filter(function (v) { | ||
return v !== undefined | ||
}) | ||
this.set(this.cleanup[i], ret, obj) | ||
@@ -346,9 +360,17 @@ } | ||
*/ | ||
DotObject.prototype.transfer = function (source, target, obj1, obj2, mods, merge) { | ||
DotObject.prototype.transfer = function ( | ||
source, | ||
target, | ||
obj1, | ||
obj2, | ||
mods, | ||
merge | ||
) { | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
this.pick(source, obj1, true), | ||
mods | ||
), obj2, merge) | ||
this.set( | ||
target, | ||
_process(this.pick(source, obj1, true), mods), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -378,12 +400,12 @@ merge = mods | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
this.set( | ||
target, | ||
_process( | ||
// clone what is picked | ||
JSON.parse( | ||
JSON.stringify( | ||
this.pick(source, obj1, false) | ||
) | ||
), | ||
JSON.parse(JSON.stringify(this.pick(source, obj1, false))), | ||
mods | ||
), obj2, merge) | ||
), | ||
obj2, | ||
merge | ||
) | ||
} else { | ||
@@ -420,3 +442,3 @@ merge = mods | ||
key = keys[i] | ||
if (i === (keys.length - 1)) { | ||
if (i === keys.length - 1) { | ||
if (merge && isObject(val) && isObject(obj[key])) { | ||
@@ -460,5 +482,5 @@ for (k in val) { | ||
* "id": 1, | ||
* "some": { | ||
* "thing": "else" | ||
* } | ||
* "some": { | ||
* "thing": "else" | ||
* } | ||
* } | ||
@@ -468,3 +490,3 @@ * | ||
* "id": "nr", | ||
* "some.thing": "name" | ||
* "some.thing": "name" | ||
* } | ||
@@ -481,5 +503,7 @@ * | ||
tgt = tgt || {} | ||
Object.keys(recipe).forEach(function (key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this)) | ||
Object.keys(recipe).forEach( | ||
function (key) { | ||
this.set(recipe[key], this.pick(key, obj), tgt) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -510,27 +534,29 @@ } | ||
Object.keys(obj).forEach(function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
( | ||
Object.keys(obj).forEach( | ||
function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
isArrayOrObject(obj[key]) && | ||
( | ||
(isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && (!this.keepArray && (obj[key].length !== 0))) | ||
) | ||
) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(previousKey + index)) | ||
((isObject(obj[key]) && !isEmptyObject(obj[key])) || | ||
(Array.isArray(obj[key]) && !this.keepArray && obj[key].length !== 0)) | ||
) { | ||
if (isArray && this.useBrackets) { | ||
var previousKey = path[path.length - 1] || '' | ||
return this.dot( | ||
obj[key], | ||
tgt, | ||
path.slice(0, -1).concat(previousKey + index) | ||
) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
} else { | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
}.bind(this)) | ||
}.bind(this) | ||
) | ||
return tgt | ||
@@ -549,5 +575,4 @@ } | ||
DotObject.del = DotObject.remove = wrap('remove') | ||
DotObject.dot = wrap('dot') | ||
;['override', 'overwrite'].forEach(function (prop) { | ||
DotObject.dot = wrap('dot'); | ||
['override', 'overwrite'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -561,5 +586,4 @@ get: function () { | ||
}) | ||
}) | ||
;['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
}); | ||
['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -566,0 +590,0 @@ get: function () { |
@@ -170,2 +170,8 @@ 'use strict' | ||
}) | ||
describe('Refuse to update __proto__', function () { | ||
var obj = { path: [] } | ||
;(() => Dot.set('path[0].__proto__.toString', 'test', obj)).should.throw(/Refusing to update/) | ||
}) | ||
}) |
@@ -208,2 +208,9 @@ 'use strict' | ||
}) | ||
it('Dot.object should disallow to set __proto__', function () { | ||
var row = { '__proto__.toString': 'hi' } | ||
var dot = new Dot() | ||
;(() => dot.object(row)).should.throw(/Refusing to update/) | ||
}) | ||
}) |
@@ -37,7 +37,10 @@ 'use strict' | ||
object: { | ||
fields: [{ | ||
subfield: 'value' | ||
}, { | ||
subfield: 'value1' | ||
}] | ||
fields: [ | ||
{ | ||
subfield: 'value' | ||
}, | ||
{ | ||
subfield: 'value1' | ||
} | ||
] | ||
} | ||
@@ -54,2 +57,9 @@ }) | ||
}) | ||
it('cannot set __proto__ property', function () { | ||
(() => Dot.str('__proto__.toString', 'hi', {})).should.throw( | ||
/Refusing to update/ | ||
); | ||
({}.toString().should.deepEqual('[object Object]')) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108759
3309