dot-object
Advanced tools
Comparing version 1.9.0 to 2.0.0
{ | ||
"name": "dot-object", | ||
"version": "1.9.0", | ||
"version": "2.0.0", | ||
"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 | ||
## 2019-11-02 Version 2.0.0 | ||
* [[`2cb41bbd1b`](https://github.com/rhalff/dot-object/commit/2cb41bbd1b)] - Add useBrackets option for the .dot() method (by z1m1n #42) | ||
* dot() now writes brackets by default (possibly breaking change). | ||
Use Dot.useBrackets = false to keep the old behavior | ||
## 2019-07-29 Version 1.9.0 | ||
@@ -4,0 +9,0 @@ * allows allow to process root level property using dot.object |
@@ -58,5 +58,5 @@ (function(global, exportName) { | ||
function DotObject(separator, override, useArray) { | ||
function DotObject(separator, override, useArray, useBrackets) { | ||
if (!(this instanceof DotObject)) { | ||
return new DotObject(separator, override, useArray) | ||
return new DotObject(separator, override, useArray, useBrackets) | ||
} | ||
@@ -66,5 +66,7 @@ | ||
if (typeof useArray === 'undefined') useArray = true | ||
if (typeof useBrackets === 'undefined') useBrackets = true | ||
this.separator = separator || '.' | ||
this.override = override | ||
this.useArray = useArray | ||
this.useBrackets = useBrackets | ||
this.keepArray = false | ||
@@ -76,3 +78,3 @@ | ||
var dotDefault = new DotObject('.', false, true) | ||
var dotDefault = new DotObject('.', false, true, true) | ||
@@ -500,3 +502,6 @@ function wrap(method) { | ||
path = path || [] | ||
var isArray = Array.isArray(obj) | ||
Object.keys(obj).forEach(function(key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
@@ -511,5 +516,13 @@ ( | ||
) { | ||
return this.dot(obj[key], tgt, path.concat(key)) | ||
if (isArray && this.useBrackets) { | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(path[path.length - 1] + index)) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
tgt[path.concat(key).join(this.separator)] = obj[key] | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
@@ -544,3 +557,3 @@ }.bind(this)) | ||
; | ||
['useArray', 'keepArray'].forEach(function(prop) { | ||
['useArray', 'keepArray', 'useBrackets'].forEach(function(prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -547,0 +560,0 @@ get: function() { |
@@ -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 p(t){return"[object Object]"===Object.prototype.toString.call(t)}function o(t){return Object(t)===t}function f(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 a=Object.prototype.hasOwnProperty;function i(t,r,e){if(!(this instanceof i))return new i(t,r,e);void 0===r&&(r=!1),void 0===e&&(e=!0),this.separator=t||".",this.override=r,this.useArray=e,this.keepArray=!1,this.cleanup=[]}var e=new i(".",!1,!0);function r(t){return function(){return e[t].apply(e,arguments)}}i.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])?[]:{}),!o(r[n])){if(!this.override){if(!o(e)||!f(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&&o(r[n])&&!f(r[n])){if(!o(e)||!f(e))throw new Error("Trying to redefine non-empty obj['"+n+"']");return}r[n]=s(e,i)}},i.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},i.prototype.str=function(t,r,e,i){return-1!==t.indexOf(this.separator)?this._fill(t.split(this.separator),e,r,i):e[t]=s(r,i),e},i.prototype.pick=function(t,r,e,i){var n,o,s,f,c,p,a;for(o=u(t,this.separator),n=0;n<o.length;n++){if(p=o[n],a=r,f="-"===p[0]&&Array.isArray(a)&&/^-\d+$/.test(p)?a.length+parseInt(p,10):p,!(r&&"object"==typeof r&&f in r))return;if(n===o.length-1)return e?(s=r[f],i&&Array.isArray(r)?r.splice(f,1):delete r[f],Array.isArray(r)&&(c=o.slice(0,-1).join("."),-1===this.cleanup.indexOf(c)&&this.cleanup.push(c)),s):r[f];r=r[f]}return e&&Array.isArray(r)&&(r=r.filter(function(t){return void 0!==t})),r},i.prototype.delete=function(t,r){return this.remove(t,r,!0)},i.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)},i.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=[]}},i.prototype.del=i.prototype.remove,i.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},i.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},i.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},i.prototype.set=function(t,r,e,i){var n,o,s,f;if(void 0===r)return e;for(s=u(t,this.separator),n=0;n<s.length;n++){if(f=s[n],n===s.length-1)if(i&&p(r)&&p(e[f]))for(o in r)a.call(r,o)&&(e[f][o]=r[o]);else if(i&&Array.isArray(e[f])&&Array.isArray(r))for(var c=0;c<r.length;c++)e[s[n]].push(r[c]);else e[f]=r;else a.call(e,f)&&(p(e[f])||Array.isArray(e[f]))||(/^\d+$/.test(s[n+1])?e[f]=[]:e[f]={});e=e[f]}return e},i.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},i.prototype.dot=function(r,e,i){return e=e||{},i=i||[],Object.keys(r).forEach(function(t){if(o(r[t])&&(p(r[t])&&!f(r[t])||Array.isArray(r[t])&&!this.keepArray&&0!==r[t].length))return this.dot(r[t],e,i.concat(t));e[i.concat(t).join(this.separator)]=r[t]}.bind(this)),e},i.pick=r("pick"),i.move=r("move"),i.transfer=r("transfer"),i.transform=r("transform"),i.copy=r("copy"),i.object=r("object"),i.str=r("str"),i.set=r("set"),i.del=i.remove=r("remove"),i.dot=r("dot"),["override","overwrite"].forEach(function(t){Object.defineProperty(i,t,{get:function(){return e.override},set:function(t){e.override=!!t}})}),["useArray","keepArray"].forEach(function(r){Object.defineProperty(i,r,{get:function(){return e[r]},set:function(t){e[r]=t}})}),i._process=s,"function"==typeof define&&define.amd?define(function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:t.DotObject=i}(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 f(t){return"[object Object]"===Object.prototype.toString.call(t)}function c(t){return Object(t)===t}function a(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])?[]:{}),!c(r[n])){if(!this.override){if(!c(e)||!a(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&&c(r[n])&&!a(r[n])){if(!c(e)||!a(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){return-1!==t.indexOf(this.separator)?this._fill(t.split(this.separator),e,r,i):e[t]=s(r,i),e},n.prototype.pick=function(t,r,e,i){var n,o,s,c,a,f,p;for(o=u(t,this.separator),n=0;n<o.length;n++){if(f=o[n],p=r,c="-"===f[0]&&Array.isArray(p)&&/^-\d+$/.test(f)?p.length+parseInt(f,10):f,!(r&&"object"==typeof r&&c in r))return;if(n===o.length-1)return e?(s=r[c],i&&Array.isArray(r)?r.splice(c,1):delete r[c],Array.isArray(r)&&(a=o.slice(0,-1).join("."),-1===this.cleanup.indexOf(a)&&this.cleanup.push(a)),s):r[c];r=r[c]}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,c;if(void 0===r)return e;for(s=u(t,this.separator),n=0;n<s.length;n++){if(c=s[n],n===s.length-1)if(i&&f(r)&&f(e[c]))for(o in r)p.call(r,o)&&(e[c][o]=r[o]);else if(i&&Array.isArray(e[c])&&Array.isArray(r))for(var a=0;a<r.length;a++)e[s[n]].push(r[a]);else e[c]=r;else p.call(e,c)&&(f(e[c])||Array.isArray(e[c]))||(/^\d+$/.test(s[n+1])?e[c]=[]:e[c]={});e=e[c]}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(e,i,n){i=i||{},n=n||[];var o=Array.isArray(e);return Object.keys(e).forEach(function(t){var r=o&&this.useBrackets?"["+t+"]":t;if(c(e[t])&&(f(e[t])&&!a(e[t])||Array.isArray(e[t])&&!this.keepArray&&0!==e[t].length))return o&&this.useBrackets?this.dot(e[t],i,n.slice(0,-1).concat(n[n.length-1]+r)):this.dot(e[t],i,n.concat(r));o&&this.useBrackets?i[n.join(this.separator).concat("["+t+"]")]=e[t]:i[n.concat(r).join(this.separator)]=e[t]}.bind(this)),i},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.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); |
25
index.js
@@ -57,5 +57,5 @@ 'use strict' | ||
function DotObject (separator, override, useArray) { | ||
function DotObject (separator, override, useArray, useBrackets) { | ||
if (!(this instanceof DotObject)) { | ||
return new DotObject(separator, override, useArray) | ||
return new DotObject(separator, override, useArray, useBrackets) | ||
} | ||
@@ -65,5 +65,7 @@ | ||
if (typeof useArray === 'undefined') useArray = true | ||
if (typeof useBrackets === 'undefined') useBrackets = true | ||
this.separator = separator || '.' | ||
this.override = override | ||
this.useArray = useArray | ||
this.useBrackets = useBrackets | ||
this.keepArray = false | ||
@@ -75,3 +77,3 @@ | ||
var dotDefault = new DotObject('.', false, true) | ||
var dotDefault = new DotObject('.', false, true, true) | ||
function wrap (method) { | ||
@@ -494,3 +496,6 @@ return function () { | ||
path = path || [] | ||
var isArray = Array.isArray(obj) | ||
Object.keys(obj).forEach(function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
@@ -505,5 +510,13 @@ ( | ||
) { | ||
return this.dot(obj[key], tgt, path.concat(key)) | ||
if (isArray && this.useBrackets) { | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(path[path.length - 1] + index)) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
tgt[path.concat(key).join(this.separator)] = obj[key] | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
@@ -536,3 +549,3 @@ }.bind(this)) | ||
;['useArray', 'keepArray'].forEach(function (prop) { | ||
;['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -539,0 +552,0 @@ get: function () { |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "1.9.0", | ||
"version": "2.0.0", | ||
"author": { | ||
@@ -32,8 +32,8 @@ "name": "Rob Halff", | ||
"devDependencies": { | ||
"eslint": "^6.0.1", | ||
"eslint-config-standard": "^13.0.1", | ||
"eslint-plugin-import": "^2.18.0", | ||
"eslint-plugin-node": "^9.1.0", | ||
"eslint": "^6.6.0", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-node": "^10.0.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"gulp": "^4.0.2", | ||
@@ -43,3 +43,3 @@ "gulp-beautify": "^3.0.0", | ||
"gulp-headerfooter": "^1.0.3", | ||
"gulp-mocha": "^6.0.0", | ||
"gulp-mocha": "^7.0.2", | ||
"gulp-rename": "^1.4.0", | ||
@@ -60,5 +60,5 @@ "gulp-uglify": "^3.0.2", | ||
"dependencies": { | ||
"commander": "^2.20.0", | ||
"glob": "^7.1.4" | ||
"commander": "^4.0.0", | ||
"glob": "^7.1.5" | ||
} | ||
} |
@@ -357,4 +357,4 @@ [![Build Status](https://travis-ci.org/rhalff/dot-object.png)](https://travis-ci.org/rhalff/dot-object) | ||
"other.nested.stuff": 5, | ||
"some.array.0": "A", | ||
"some.array.1": "B" | ||
"some.array[0]": "A", | ||
"some.array[1]": "B" | ||
} | ||
@@ -361,0 +361,0 @@ ``` |
@@ -57,5 +57,5 @@ 'use strict' | ||
function DotObject (separator, override, useArray) { | ||
function DotObject (separator, override, useArray, useBrackets) { | ||
if (!(this instanceof DotObject)) { | ||
return new DotObject(separator, override, useArray) | ||
return new DotObject(separator, override, useArray, useBrackets) | ||
} | ||
@@ -65,5 +65,7 @@ | ||
if (typeof useArray === 'undefined') useArray = true | ||
if (typeof useBrackets === 'undefined') useBrackets = true | ||
this.separator = separator || '.' | ||
this.override = override | ||
this.useArray = useArray | ||
this.useBrackets = useBrackets | ||
this.keepArray = false | ||
@@ -75,3 +77,3 @@ | ||
var dotDefault = new DotObject('.', false, true) | ||
var dotDefault = new DotObject('.', false, true, true) | ||
function wrap (method) { | ||
@@ -494,3 +496,6 @@ return function () { | ||
path = path || [] | ||
var isArray = Array.isArray(obj) | ||
Object.keys(obj).forEach(function (key) { | ||
var index = isArray && this.useBrackets ? '[' + key + ']' : key | ||
if ( | ||
@@ -505,5 +510,13 @@ ( | ||
) { | ||
return this.dot(obj[key], tgt, path.concat(key)) | ||
if (isArray && this.useBrackets) { | ||
return this.dot(obj[key], tgt, path.slice(0, -1).concat(path[path.length - 1] + index)) | ||
} else { | ||
return this.dot(obj[key], tgt, path.concat(index)) | ||
} | ||
} else { | ||
tgt[path.concat(key).join(this.separator)] = obj[key] | ||
if (isArray && this.useBrackets) { | ||
tgt[path.join(this.separator).concat('[' + key + ']')] = obj[key] | ||
} else { | ||
tgt[path.concat(index).join(this.separator)] = obj[key] | ||
} | ||
} | ||
@@ -536,3 +549,3 @@ }.bind(this)) | ||
;['useArray', 'keepArray'].forEach(function (prop) { | ||
;['useArray', 'keepArray', 'useBrackets'].forEach(function (prop) { | ||
Object.defineProperty(DotObject, prop, { | ||
@@ -539,0 +552,0 @@ get: function () { |
@@ -23,2 +23,12 @@ 'use strict' | ||
}, | ||
nested: { | ||
array: [ | ||
{ | ||
with: 'object1' | ||
}, | ||
{ | ||
and: 'object2' | ||
} | ||
] | ||
}, | ||
some: { | ||
@@ -39,4 +49,6 @@ array: ['A', 'B'] | ||
'other.nested.stuff': 5, | ||
'some.array.0': 'A', | ||
'some.array.1': 'B', | ||
'nested.array[0].with': 'object1', | ||
'nested.array[1].and': 'object2', | ||
'some.array[0]': 'A', | ||
'some.array[1]': 'B', | ||
ehrm: 123, | ||
@@ -58,2 +70,7 @@ 'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)') | ||
'other.nested.stuff': 5, | ||
'nested.array': [{ | ||
with: 'object1' | ||
}, { | ||
and: 'object2' | ||
}], | ||
'some.array': ['A', 'B'], | ||
@@ -71,2 +88,18 @@ ehrm: 123, | ||
it('useBrackets wrap indexes with brackets', function () { | ||
var expected = { | ||
id: 'my-id', | ||
'nes.ted.value': true, | ||
'other.nested.stuff': 5, | ||
'nested.array[0].with': 'object1', | ||
'nested.array[1].and': 'object2', | ||
'some.array[0]': 'A', | ||
'some.array[1]': 'B', | ||
ehrm: 123, | ||
'dates.first': new Date('Mon Oct 13 2014 00:00:00 GMT+0100 (BST)') | ||
} | ||
Dot.dot(obj).should.eql(expected) | ||
}) | ||
it('Always keeps empty arrays', function () { | ||
@@ -73,0 +106,0 @@ Dot.dot({ hello: [] }).should.eql({ hello: [] }) |
@@ -53,2 +53,3 @@ 'use strict' | ||
Dot.useArray = true | ||
Dot.useBrackets = false | ||
dotObjectExpected.should.eql(Dot.dot(Dot.object(dotObject))) | ||
@@ -55,0 +56,0 @@ }) |
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
104215
3164
+ Addedcommander@4.1.1(transitive)
- Removedcommander@2.20.3(transitive)
Updatedcommander@^4.0.0
Updatedglob@^7.1.5