dot-object
Advanced tools
Comparing version 0.11.0 to 1.0.0
@@ -93,2 +93,24 @@ (function (global, exportName) { | ||
/** | ||
* | ||
* Converts an object with dotted-key/value pairs to it's expanded version | ||
* | ||
* Optionally transformed by a set of modifiers. | ||
* | ||
* Usage: | ||
* | ||
* var row = { | ||
* 'nr': 200, | ||
* 'doc.name': ' My Document ' | ||
* }; | ||
* | ||
* var mods = { | ||
* 'doc.name': [_s.trim, _s.underscored] | ||
* }; | ||
* | ||
* dot.object(row, mods); | ||
* | ||
* @param {Object} obj | ||
* @param {Object} mods | ||
*/ | ||
DotObject.prototype.object = function (obj, mods) { | ||
@@ -110,14 +132,12 @@ var self = this; | ||
/** | ||
* | ||
* @param {String} str | ||
* @param {String} v | ||
* @param {Object} obj | ||
* @param {Function|Array} mod | ||
* | ||
* @param {String} path dotted path | ||
* @param {String} v value to be set | ||
* @param {Object} obj object to be modified | ||
* @param {Function|Array} mod optional modifier | ||
*/ | ||
DotObject.prototype.str = function (str, v, obj, mod) { | ||
if (str.indexOf(this.seperator) !== -1) { | ||
this._fill(str.split(this.seperator), obj, v, mod); | ||
DotObject.prototype.str = function (path, v, obj, mod) { | ||
if (path.indexOf(this.seperator) !== -1) { | ||
this._fill(path.split(this.seperator), obj, v, mod); | ||
} else if (this.override) { | ||
obj[str] = _process(v, mod); | ||
obj[path] = _process(v, mod); | ||
} | ||
@@ -233,3 +253,2 @@ }; | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
@@ -366,2 +385,32 @@ DotObject.prototype.move = function (source, target, obj, mods, merge) { | ||
/** | ||
* | ||
* Convert object to dotted-key/value pair | ||
* | ||
* Usage: | ||
* | ||
* var tgt = dot.dot(obj); | ||
* | ||
* or | ||
* | ||
* var tgt = {}; | ||
* dot.dot(obj, tgt); | ||
* | ||
* @param {Object} obj source object | ||
* @param {Object} tgt target object | ||
* @param {Array} path path array (internal) | ||
*/ | ||
DotObject.prototype.dot = function (obj, tgt, path) { | ||
tgt = tgt ? tgt : {}; | ||
path = path ? path : []; | ||
Object.keys(obj).forEach(function (key) { | ||
if (Object(obj[key]) === obj[key]) { | ||
return this.dot(obj[key], tgt, path.concat(key)); | ||
} else { | ||
tgt[path.concat(key).join(this.seperator)] = obj[key]; | ||
} | ||
}.bind(this)); | ||
return tgt; | ||
}; | ||
DotObject.pick = wrap('pick'); | ||
@@ -375,2 +424,3 @@ DotObject.move = wrap('move'); | ||
DotObject.del = DotObject.remove = wrap('remove'); | ||
DotObject.dot = wrap('dot'); | ||
@@ -377,0 +427,0 @@ DotObject._process = _process; |
@@ -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.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._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,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"); |
72
index.js
@@ -89,2 +89,24 @@ 'use strict'; | ||
/** | ||
* | ||
* Converts an object with dotted-key/value pairs to it's expanded version | ||
* | ||
* Optionally transformed by a set of modifiers. | ||
* | ||
* Usage: | ||
* | ||
* var row = { | ||
* 'nr': 200, | ||
* 'doc.name': ' My Document ' | ||
* }; | ||
* | ||
* var mods = { | ||
* 'doc.name': [_s.trim, _s.underscored] | ||
* }; | ||
* | ||
* dot.object(row, mods); | ||
* | ||
* @param {Object} obj | ||
* @param {Object} mods | ||
*/ | ||
DotObject.prototype.object = function(obj, mods) { | ||
@@ -106,14 +128,12 @@ var self = this; | ||
/** | ||
* | ||
* @param {String} str | ||
* @param {String} v | ||
* @param {Object} obj | ||
* @param {Function|Array} mod | ||
* | ||
* @param {String} path dotted path | ||
* @param {String} v value to be set | ||
* @param {Object} obj object to be modified | ||
* @param {Function|Array} mod optional modifier | ||
*/ | ||
DotObject.prototype.str = function(str, v, obj, mod) { | ||
if (str.indexOf(this.seperator) !== -1) { | ||
this._fill(str.split(this.seperator), obj, v, mod); | ||
DotObject.prototype.str = function(path, v, obj, mod) { | ||
if (path.indexOf(this.seperator) !== -1) { | ||
this._fill(path.split(this.seperator), obj, v, mod); | ||
} else if (this.override) { | ||
obj[str] = _process(v, mod); | ||
obj[path] = _process(v, mod); | ||
} | ||
@@ -225,3 +245,2 @@ }; | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
@@ -367,2 +386,32 @@ DotObject.prototype.move = function(source, target, obj, mods, merge) { | ||
/** | ||
* | ||
* Convert object to dotted-key/value pair | ||
* | ||
* Usage: | ||
* | ||
* var tgt = dot.dot(obj); | ||
* | ||
* or | ||
* | ||
* var tgt = {}; | ||
* dot.dot(obj, tgt); | ||
* | ||
* @param {Object} obj source object | ||
* @param {Object} tgt target object | ||
* @param {Array} path path array (internal) | ||
*/ | ||
DotObject.prototype.dot = function(obj, tgt, path) { | ||
tgt = tgt ? tgt : {}; | ||
path = path ? path : []; | ||
Object.keys(obj).forEach(function(key) { | ||
if (Object(obj[key]) === obj[key]) { | ||
return this.dot(obj[key], tgt, path.concat(key)); | ||
} else { | ||
tgt[path.concat(key).join(this.seperator)] = obj[key]; | ||
} | ||
}.bind(this)); | ||
return tgt; | ||
}; | ||
DotObject.pick = wrap('pick'); | ||
@@ -376,2 +425,3 @@ DotObject.move = wrap('move'); | ||
DotObject.del = DotObject.remove = wrap('remove'); | ||
DotObject.dot = wrap('dot'); | ||
@@ -378,0 +428,0 @@ DotObject._process = _process; |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "0.11.0", | ||
"version": "1.0.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Rob Halff", |
@@ -241,3 +241,34 @@ [![Build Status](https://travis-ci.org/rhalff/dot-object.png)](https://travis-ci.org/rhalff/dot-object) | ||
``` | ||
### Convert object to dotted-key/value pair | ||
```javascript | ||
var dot = require('dot-object'); | ||
var obj = { | ||
id: 'my-id', | ||
nes: { ted: { value: true } }, | ||
other: { nested: { stuff: 5 } }, | ||
some: { array: ['A', 'B'] } | ||
}; | ||
var tgt = dot.dot(obj); | ||
// or | ||
var tgt = {}; | ||
dot.dot(obj, tgt); | ||
console.log(tgt); | ||
``` | ||
Result: | ||
```json | ||
{ | ||
"id": "my-id", | ||
"nes.ted.value": true, | ||
"other.nested.stuff": 5, | ||
"some.array.0": "A", | ||
"some.array.1": "B" | ||
} | ||
``` | ||
## Using a different seperator | ||
@@ -244,0 +275,0 @@ |
@@ -89,2 +89,24 @@ 'use strict'; | ||
/** | ||
* | ||
* Converts an object with dotted-key/value pairs to it's expanded version | ||
* | ||
* Optionally transformed by a set of modifiers. | ||
* | ||
* Usage: | ||
* | ||
* var row = { | ||
* 'nr': 200, | ||
* 'doc.name': ' My Document ' | ||
* }; | ||
* | ||
* var mods = { | ||
* 'doc.name': [_s.trim, _s.underscored] | ||
* }; | ||
* | ||
* dot.object(row, mods); | ||
* | ||
* @param {Object} obj | ||
* @param {Object} mods | ||
*/ | ||
DotObject.prototype.object = function(obj, mods) { | ||
@@ -106,14 +128,12 @@ var self = this; | ||
/** | ||
* | ||
* @param {String} str | ||
* @param {String} v | ||
* @param {Object} obj | ||
* @param {Function|Array} mod | ||
* | ||
* @param {String} path dotted path | ||
* @param {String} v value to be set | ||
* @param {Object} obj object to be modified | ||
* @param {Function|Array} mod optional modifier | ||
*/ | ||
DotObject.prototype.str = function(str, v, obj, mod) { | ||
if (str.indexOf(this.seperator) !== -1) { | ||
this._fill(str.split(this.seperator), obj, v, mod); | ||
DotObject.prototype.str = function(path, v, obj, mod) { | ||
if (path.indexOf(this.seperator) !== -1) { | ||
this._fill(path.split(this.seperator), obj, v, mod); | ||
} else if (this.override) { | ||
obj[str] = _process(v, mod); | ||
obj[path] = _process(v, mod); | ||
} | ||
@@ -225,3 +245,2 @@ }; | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
@@ -367,2 +386,32 @@ DotObject.prototype.move = function(source, target, obj, mods, merge) { | ||
/** | ||
* | ||
* Convert object to dotted-key/value pair | ||
* | ||
* Usage: | ||
* | ||
* var tgt = dot.dot(obj); | ||
* | ||
* or | ||
* | ||
* var tgt = {}; | ||
* dot.dot(obj, tgt); | ||
* | ||
* @param {Object} obj source object | ||
* @param {Object} tgt target object | ||
* @param {Array} path path array (internal) | ||
*/ | ||
DotObject.prototype.dot = function(obj, tgt, path) { | ||
tgt = tgt ? tgt : {}; | ||
path = path ? path : []; | ||
Object.keys(obj).forEach(function(key) { | ||
if (Object(obj[key]) === obj[key]) { | ||
return this.dot(obj[key], tgt, path.concat(key)); | ||
} else { | ||
tgt[path.concat(key).join(this.seperator)] = obj[key]; | ||
} | ||
}.bind(this)); | ||
return tgt; | ||
}; | ||
DotObject.pick = wrap('pick'); | ||
@@ -376,3 +425,4 @@ DotObject.move = wrap('move'); | ||
DotObject.del = DotObject.remove = wrap('remove'); | ||
DotObject.dot = wrap('dot'); | ||
DotObject._process = _process; |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
68120
24
2035
1
318