dot-object
Advanced tools
Comparing version 1.0.1 to 1.1.0
# ChangeLog | ||
## 2015-08-08, Version 1.1.0 | ||
* Also let .object() understand array notation. | ||
## 2015-08-03, Version 1.0.0 | ||
@@ -4,0 +7,0 @@ * Convert object to dotted-key/value pairs |
@@ -33,2 +33,6 @@ (function (global, exportName) { | ||
function isIndex(k) { | ||
return /^\d+/.test(k); | ||
} | ||
function parsePath(path, sep) { | ||
@@ -74,3 +78,3 @@ if (path.indexOf('[') >= 0) { | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || {}; | ||
obj[k] = obj[k] || (a.length === 1 && isIndex(a[0]) ? [] : {}); | ||
@@ -122,5 +126,7 @@ if (obj[k] !== Object(obj[k])) { | ||
var mod = mods === undefined ? null : mods[k]; | ||
// normalize array notation. | ||
var ok = parsePath(k, self.seperator).join(self.seperator); | ||
if (k.indexOf(self.seperator) !== -1) { | ||
self._fill(k.split(self.seperator), obj, obj[k], mod); | ||
if (ok.indexOf(self.seperator) !== -1) { | ||
self._fill(ok.split(self.seperator), obj, obj[k], mod); | ||
delete obj[k]; | ||
@@ -127,0 +133,0 @@ } else if (self.override) { |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"use strict";function r(t,e){var r,i;if("function"==typeof e)i=e(t),void 0!==i&&(t=i);else if(Array.isArray(e))for(r=0;r<e.length;r++)i=e[r](t),void 0!==i&&(t=i);return t}function i(t,e){return"-"===t[0]&&Array.isArray(e)&&/^-\d+$/.test(t)?e.length+parseInt(t,10):t}function n(t,e){return t.indexOf("[")>=0&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(e)}function o(t,e){return this instanceof o?("undefined"==typeof t&&(t="."),"undefined"==typeof e&&(e=!1),this.seperator=t,this.override=e,void(this.cleanup=[])):new o(t,e)}function s(t){return function(){return f[t].apply(f,arguments)}}function p(t){return"[object Object]"===Object.prototype.toString.call(t)}var f=new o(".",!1);o.prototype._fill=function(t,e,i,n){var o=t.shift();if(t.length>0){if(e[o]=e[o]||{},e[o]!==Object(e[o])){if(!this.override)throw new Error("Trying to redefine `"+o+"` which is a "+typeof e[o]);e[o]={}}this._fill(t,e[o],i,n)}else{if(e[o]===Object(e[o])&&Object.keys(e[o]).length)throw new Error("Trying to redefine non-empty obj['"+o+"']");e[o]=r(i,n)}},o.prototype.object=function(t,e){var i=this;Object.keys(t).forEach(function(n){var o=void 0===e?null:e[n];-1!==n.indexOf(i.seperator)?(i._fill(n.split(i.seperator),t,t[n],o),delete t[n]):i.override&&(t[n]=r(t[n],o))})},o.prototype.str=function(t,e,i,n){-1!==t.indexOf(this.seperator)?this._fill(t.split(this.seperator),i,e,n):this.override&&(i[t]=r(e,n))},o.prototype.pick=function(t,e,r){var o,s,p,f,c;for(s=n(t,this.seperator),o=0;o<s.length;o++){if(f=i(s[o],e),!(e&&"object"==typeof e&&f in e))return void 0;if(o===s.length-1)return r?(p=e[f],delete e[f],Array.isArray(e)&&(c=s.slice(0,-1).join("."),-1===this.cleanup.indexOf(c)&&this.cleanup.push(c)),p):e[f];e=e[f]}return r&&Array.isArray(e)&&(e=e.filter(function(t){return void 0!==t})),e},o.prototype.remove=function(t,e){var r;if(this.cleanup=[],Array.isArray(t)){for(r=0;r<t.length;r++)this.pick(t[r],e,!0);return this._cleanup(e),e}return this.pick(t,e,!0)},o.prototype._cleanup=function(t){var e,r,i,n;if(this.cleanup.length){for(r=0;r<this.cleanup.length;r++)i=this.cleanup[r].split("."),n=i.splice(0,-1).join("."),e=n?this.pick(n,t):t,e=e[i[0]].filter(function(t){return void 0!==t}),this.set(this.cleanup[r],e,t);this.cleanup=[]}},o.prototype.del=o.prototype.remove,o.prototype.move=function(t,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(e,r(this.pick(t,i,!0),n),i,o):(o=n,this.set(e,this.pick(t,i,!0),i,o)),i},o.prototype.transfer=function(t,e,i,n,o,s){return"function"==typeof o||Array.isArray(o)?this.set(e,r(this.pick(t,i,!0),o),n,s):(s=o,this.set(e,this.pick(t,i,!0),n,s)),n},o.prototype.copy=function(t,e,i,n,o,s){return"function"==typeof o||Array.isArray(o)?this.set(e,r(JSON.parse(JSON.stringify(this.pick(t,i,!1))),o),n,s):(s=o,this.set(e,this.pick(t,i,!1),n,s)),n},o.prototype.set=function(t,e,r,i){var o,s,f,c;if("undefined"==typeof e)return r;for(f=n(t,this.seperator),o=0;o<f.length;o++){if(c=f[o],o===f.length-1)if(i&&p(e)&&p(r[c]))for(s in e)e.hasOwnProperty(s)&&(r[c][s]=e[s]);else if(i&&Array.isArray(r[c])&&Array.isArray(e))for(var u=0;u<e.length;u++)r[f[o]].push(e[u]);else r[c]=e;else r.hasOwnProperty(c)&&(p(r[c])||Array.isArray(r[c]))||(r[c]=/^\d+$/.test(f[o+1])?[]:{});r=r[c]}return r},o.prototype.dot=function(t,e,r){return e=e?e:{},r=r?r:[],Object.keys(t).forEach(function(i){return Object(t[i])===t[i]?this.dot(t[i],e,r.concat(i)):void(e[r.concat(i).join(this.seperator)]=t[i])}.bind(this)),e},o.pick=s("pick"),o.move=s("move"),o.transfer=s("transfer"),o.copy=s("copy"),o.object=s("object"),o.str=s("str"),o.set=s("set"),o.del=o.remove=s("remove"),o.dot=s("dot"),o._process=r,"function"==typeof define&&define.amd?define(function(){return o}):"undefined"!=typeof module&&module.exports?module.exports=o:t[e]=o}(this,"DotObject"); | ||
!function(t,e){"use strict";function r(t,e){var r,i;if("function"==typeof e)i=e(t),void 0!==i&&(t=i);else if(Array.isArray(e))for(r=0;r<e.length;r++)i=e[r](t),void 0!==i&&(t=i);return t}function i(t,e){return"-"===t[0]&&Array.isArray(e)&&/^-\d+$/.test(t)?e.length+parseInt(t,10):t}function n(t){return/^\d+/.test(t)}function o(t,e){return t.indexOf("[")>=0&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(e)}function s(t,e){return this instanceof s?("undefined"==typeof t&&(t="."),"undefined"==typeof e&&(e=!1),this.seperator=t,this.override=e,void(this.cleanup=[])):new s(t,e)}function p(t){return function(){return c[t].apply(c,arguments)}}function f(t){return"[object Object]"===Object.prototype.toString.call(t)}var c=new s(".",!1);s.prototype._fill=function(t,e,i,o){var s=t.shift();if(t.length>0){if(e[s]=e[s]||(1===t.length&&n(t[0])?[]:{}),e[s]!==Object(e[s])){if(!this.override)throw new Error("Trying to redefine `"+s+"` which is a "+typeof e[s]);e[s]={}}this._fill(t,e[s],i,o)}else{if(e[s]===Object(e[s])&&Object.keys(e[s]).length)throw new Error("Trying to redefine non-empty obj['"+s+"']");e[s]=r(i,o)}},s.prototype.object=function(t,e){var i=this;Object.keys(t).forEach(function(n){var s=void 0===e?null:e[n],p=o(n,i.seperator).join(i.seperator);-1!==p.indexOf(i.seperator)?(i._fill(p.split(i.seperator),t,t[n],s),delete t[n]):i.override&&(t[n]=r(t[n],s))})},s.prototype.str=function(t,e,i,n){-1!==t.indexOf(this.seperator)?this._fill(t.split(this.seperator),i,e,n):this.override&&(i[t]=r(e,n))},s.prototype.pick=function(t,e,r){var n,s,p,f,c;for(s=o(t,this.seperator),n=0;n<s.length;n++){if(f=i(s[n],e),!(e&&"object"==typeof e&&f in e))return void 0;if(n===s.length-1)return r?(p=e[f],delete e[f],Array.isArray(e)&&(c=s.slice(0,-1).join("."),-1===this.cleanup.indexOf(c)&&this.cleanup.push(c)),p):e[f];e=e[f]}return r&&Array.isArray(e)&&(e=e.filter(function(t){return void 0!==t})),e},s.prototype.remove=function(t,e){var r;if(this.cleanup=[],Array.isArray(t)){for(r=0;r<t.length;r++)this.pick(t[r],e,!0);return this._cleanup(e),e}return this.pick(t,e,!0)},s.prototype._cleanup=function(t){var e,r,i,n;if(this.cleanup.length){for(r=0;r<this.cleanup.length;r++)i=this.cleanup[r].split("."),n=i.splice(0,-1).join("."),e=n?this.pick(n,t):t,e=e[i[0]].filter(function(t){return void 0!==t}),this.set(this.cleanup[r],e,t);this.cleanup=[]}},s.prototype.del=s.prototype.remove,s.prototype.move=function(t,e,i,n,o){return"function"==typeof n||Array.isArray(n)?this.set(e,r(this.pick(t,i,!0),n),i,o):(o=n,this.set(e,this.pick(t,i,!0),i,o)),i},s.prototype.transfer=function(t,e,i,n,o,s){return"function"==typeof o||Array.isArray(o)?this.set(e,r(this.pick(t,i,!0),o),n,s):(s=o,this.set(e,this.pick(t,i,!0),n,s)),n},s.prototype.copy=function(t,e,i,n,o,s){return"function"==typeof o||Array.isArray(o)?this.set(e,r(JSON.parse(JSON.stringify(this.pick(t,i,!1))),o),n,s):(s=o,this.set(e,this.pick(t,i,!1),n,s)),n},s.prototype.set=function(t,e,r,i){var n,s,p,c;if("undefined"==typeof e)return r;for(p=o(t,this.seperator),n=0;n<p.length;n++){if(c=p[n],n===p.length-1)if(i&&f(e)&&f(r[c]))for(s in e)e.hasOwnProperty(s)&&(r[c][s]=e[s]);else if(i&&Array.isArray(r[c])&&Array.isArray(e))for(var u=0;u<e.length;u++)r[p[n]].push(e[u]);else r[c]=e;else r.hasOwnProperty(c)&&(f(r[c])||Array.isArray(r[c]))||(r[c]=/^\d+$/.test(p[n+1])?[]:{});r=r[c]}return r},s.prototype.dot=function(t,e,r){return e=e?e:{},r=r?r:[],Object.keys(t).forEach(function(i){return Object(t[i])===t[i]?this.dot(t[i],e,r.concat(i)):void(e[r.concat(i).join(this.seperator)]=t[i])}.bind(this)),e},s.pick=p("pick"),s.move=p("move"),s.transfer=p("transfer"),s.copy=p("copy"),s.object=p("object"),s.str=p("str"),s.set=p("set"),s.del=s.remove=p("remove"),s.dot=p("dot"),s._process=r,"function"==typeof define&&define.amd?define(function(){return s}):"undefined"!=typeof module&&module.exports?module.exports=s:t[e]=s}(this,"DotObject"); |
12
index.js
@@ -32,2 +32,6 @@ 'use strict'; | ||
function isIndex(k) { | ||
return /^\d+/.test(k); | ||
} | ||
function parsePath(path, sep) { | ||
@@ -68,3 +72,3 @@ if (path.indexOf('[') >= 0) { | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || {}; | ||
obj[k] = obj[k] || (a.length === 1 && isIndex(a[0]) ? [] : {}); | ||
@@ -118,5 +122,7 @@ if (obj[k] !== Object(obj[k])) { | ||
var mod = mods === undefined ? null : mods[k]; | ||
// normalize array notation. | ||
var ok = parsePath(k, self.seperator).join(self.seperator); | ||
if (k.indexOf(self.seperator) !== -1) { | ||
self._fill(k.split(self.seperator), obj, obj[k], mod); | ||
if (ok.indexOf(self.seperator) !== -1) { | ||
self._fill(ok.split(self.seperator), obj, obj[k], mod); | ||
delete obj[k]; | ||
@@ -123,0 +129,0 @@ } else if (self.override) { |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Rob Halff", |
@@ -107,3 +107,7 @@ [![Build Status](https://travis-ci.org/rhalff/dot-object.png)](https://travis-ci.org/rhalff/dot-object) | ||
'contact.email': 'example@gmail.com', | ||
'contact.info.about.me': 'classified' | ||
'contact.info.about.me': 'classified', | ||
'devices[0]': 'mobile', | ||
'devices[1]': 'laptop', | ||
'some.other.things.0': 'this', | ||
'some.other.things.1': 'that' | ||
}; | ||
@@ -125,5 +129,17 @@ | ||
"about": { | ||
"me": "classified" | ||
"me": "classified" | ||
} | ||
} | ||
}, | ||
"devices": [ | ||
"mobile", | ||
"laptop" | ||
], | ||
"some": { | ||
"other": { | ||
"things": [ | ||
"this", | ||
"that" | ||
] | ||
} | ||
} | ||
@@ -130,0 +146,0 @@ } |
@@ -32,2 +32,6 @@ 'use strict'; | ||
function isIndex(k) { | ||
return /^\d+/.test(k); | ||
} | ||
function parsePath(path, sep) { | ||
@@ -68,3 +72,3 @@ if (path.indexOf('[') >= 0) { | ||
if (a.length > 0) { | ||
obj[k] = obj[k] || {}; | ||
obj[k] = obj[k] || (a.length === 1 && isIndex(a[0]) ? [] : {}); | ||
@@ -118,5 +122,7 @@ if (obj[k] !== Object(obj[k])) { | ||
var mod = mods === undefined ? null : mods[k]; | ||
// normalize array notation. | ||
var ok = parsePath(k, self.seperator).join(self.seperator); | ||
if (k.indexOf(self.seperator) !== -1) { | ||
self._fill(k.split(self.seperator), obj, obj[k], mod); | ||
if (ok.indexOf(self.seperator) !== -1) { | ||
self._fill(ok.split(self.seperator), obj, obj[k], mod); | ||
delete obj[k]; | ||
@@ -123,0 +129,0 @@ } else if (self.override) { |
@@ -39,2 +39,26 @@ 'use strict'; | ||
it('Should expand dotted keys with array notation', function() { | ||
var row = { | ||
'id': 2, | ||
'my.arr.0': 'one', | ||
'my.arr.1': 'two', | ||
'my.arr.2': 'three', | ||
'my.arr2[0]': 'one', | ||
'my.arr2[1]': 'two', | ||
'my.arr2[2]': 'three' | ||
}; | ||
Dot.object(row); | ||
row.should.eql({ | ||
'id': 2, | ||
'my': { | ||
'arr': ['one', 'two', 'three'], | ||
'arr2': ['one', 'two', 'three'] | ||
} | ||
}); | ||
}); | ||
it('Should expand dotted string', function() { | ||
@@ -41,0 +65,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
69654
2069
334