dot-object
Advanced tools
Comparing version 0.9.3 to 0.10.0
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"homepage": "https://github.com/rhalff/dot-object", | ||
@@ -31,0 +31,0 @@ "moduleType": [ |
# ChangeLog | ||
## 2015-03-09, Version 0.9.0 | ||
## 2015-03-22, Version 0.10.0 | ||
* Make return value optional for Object/Array modifier(s) | ||
* Add modifier option to move(), transfer() & copy() | ||
## 2015-03-21, Version 0.9.0 | ||
* support multiple replacements/deletions (cli) | ||
@@ -5,0 +9,0 @@ * add del/remove method |
(function (global, exportName) { | ||
'use strict'; | ||
function _process(v, mod) { | ||
var i; | ||
var r; | ||
if (typeof mod === 'function') { | ||
r = mod(v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} else if (Array.isArray(mod)) { | ||
for (i = 0; i < mod.length; i++) { | ||
r = mod[i](v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} | ||
} | ||
return v; | ||
} | ||
function DotObject(seperator, override) { | ||
@@ -48,20 +69,6 @@ | ||
obj[k] = this.process(v, mod); | ||
obj[k] = _process(v, mod); | ||
} | ||
}; | ||
DotObject.prototype.process = function (v, mod) { | ||
var i; | ||
if (typeof mod === 'function') { | ||
v = mod(v); | ||
} else if (mod instanceof Array) { | ||
for (i = 0; i < mod.length; i++) { | ||
v = mod[i](v); | ||
} | ||
} | ||
return v; | ||
}; | ||
DotObject.prototype.object = function (obj, mods) { | ||
@@ -77,3 +84,3 @@ var self = this; | ||
} else if (self.override) { | ||
obj[k] = self.process(obj[k], mod); | ||
obj[k] = _process(obj[k], mod); | ||
} | ||
@@ -95,3 +102,3 @@ }); | ||
} else if (this.override) { | ||
obj[str] = this.process(v, mod); | ||
obj[str] = _process(v, mod); | ||
} | ||
@@ -171,8 +178,14 @@ }; | ||
* @param {Object} obj | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
DotObject.prototype.move = function (source, target, obj, merge) { | ||
DotObject.prototype.move = function (source, target, obj, mods, merge) { | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, _process(this.pick(source, obj, true), mods), obj, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
} | ||
@@ -194,10 +207,18 @@ return obj; | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.transfer = function (source, target, obj1, obj2, merge) { | ||
DotObject.prototype.transfer = | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
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); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
} | ||
return obj2; | ||
}; | ||
@@ -216,7 +237,16 @@ | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.copy = function (source, target, obj1, obj2, merge) { | ||
DotObject.prototype.copy = function (source, target, obj1, obj2, mods, merge) { | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, _process( | ||
JSON.parse( // clone what is picked | ||
JSON.stringify( | ||
this.pick(source, obj1, false))), mods), obj2, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
} | ||
@@ -301,3 +331,5 @@ return obj2; | ||
DotObject._process = _process; | ||
if (typeof define === 'function' && define.amd) { | ||
@@ -304,0 +336,0 @@ define(function () { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"use strict";function r(e,t){return this instanceof r?("undefined"==typeof e&&(e="."),"undefined"==typeof t&&(t=!1),this.seperator=e,void(this.override=t)):new r(e,t)}function o(e){return function(){return n[e].apply(n,arguments)}}function i(e){return"[object Object]"===Object.prototype.toString.call(e)}var n=new r(".",!1);r.prototype._fill=function(e,t,r,o){var i=e.shift();if(e.length>0){if(t[i]=t[i]||{},t[i]!==Object(t[i])){if(!this.override)throw new Error("Trying to redefine `"+i+"` which is a "+typeof t[i]);t[i]={}}this._fill(e,t[i],r,o)}else{if(t[i]===Object(t[i])&&Object.keys(t[i]).length)throw new Error("Trying to redefine non-empty obj['"+i+"']");t[i]=this.process(r,o)}},r.prototype.process=function(e,t){var r;if("function"==typeof t)e=t(e);else if(t instanceof Array)for(r=0;r<t.length;r++)e=t[r](e);return e},r.prototype.object=function(e,t){var r=this;Object.keys(e).forEach(function(o){var i=void 0===t?null:t[o];-1!==o.indexOf(r.seperator)?(r._fill(o.split(r.seperator),e,e[o],i),delete e[o]):r.override&&(e[o]=r.process(e[o],i))})},r.prototype.str=function(e,t,r,o){-1!==e.indexOf(this.seperator)?this._fill(e.split(this.seperator),r,t,o):this.override&&(r[e]=this.process(t,o))},r.prototype.pick=function(e,t,r){var o,i,n;if(-1!==e.indexOf(this.seperator)){for(i=e.split(this.seperator),o=0;o<i.length;o++){if(!(t&&"object"==typeof t&&i[o]in t))return void 0;if(o===i.length-1)return r?(n=t[i[o]],delete t[i[o]],n):t[i[o]];t=t[i[o]]}return t}return r?(n=t[e],delete t[e],n):t[e]},r.prototype.remove=function(e,t){return this.pick(e,t,!0)},r.prototype.del=r.prototype.remove,r.prototype.move=function(e,t,r,o){return this.set(t,this.pick(e,r,!0),r,o),r},r.prototype.transfer=function(e,t,r,o,i){return this.set(t,this.pick(e,r,!0),o,i),o},r.prototype.copy=function(e,t,r,o,i){return this.set(t,this.pick(e,r,!1),o,i),o},r.prototype.set=function(e,t,r,o){var n,s,f;if("undefined"==typeof t)return r;if(-1!==e.indexOf(this.seperator)){for(f=e.split(this.seperator),n=0;n<f.length;n++){if(n===f.length-1)if(o&&i(t)&&i(r[f[n]]))for(s in t)t.hasOwnProperty(s)&&(r[f[n]][s]=t[s]);else if(Array.isArray(r[f[n]])&&Array.isArray(t))for(var p=0;p<t.length;p++)r[f[n]].push(t[p]);else r[f[n]]=t;else r.hasOwnProperty(f[n])&&i(r[f[n]])||(r[f[n]]={});r=r[f[n]]}return r}if(o&&i(t))for(s in t)t.hasOwnProperty(s)&&(r[e][s]=t[s]);else Array.isArray(r[e])&&Array.isArray(t)?r[e].push(t):r[e]=t;return r},r.pick=o("pick"),r.move=o("move"),r.transfer=o("transfer"),r.copy=o("copy"),r.object=o("object"),r.str=o("str"),r.set=o("set"),r.del=r.remove=o("remove"),"function"==typeof define&&define.amd?define(function(){return r}):"undefined"!=typeof module&&module.exports?module.exports=r:e[t]=r}(this,"DotObject"); | ||
!function(e,t){"use strict";function r(e,t){var r,i;if("function"==typeof t)i=t(e),void 0!==i&&(e=i);else if(Array.isArray(t))for(r=0;r<t.length;r++)i=t[r](e),void 0!==i&&(e=i);return e}function i(e,t){return this instanceof i?("undefined"==typeof e&&(e="."),"undefined"==typeof t&&(t=!1),this.seperator=e,void(this.override=t)):new i(e,t)}function o(e){return function(){return s[e].apply(s,arguments)}}function n(e){return"[object Object]"===Object.prototype.toString.call(e)}var s=new i(".",!1);i.prototype._fill=function(e,t,i,o){var n=e.shift();if(e.length>0){if(t[n]=t[n]||{},t[n]!==Object(t[n])){if(!this.override)throw new Error("Trying to redefine `"+n+"` which is a "+typeof t[n]);t[n]={}}this._fill(e,t[n],i,o)}else{if(t[n]===Object(t[n])&&Object.keys(t[n]).length)throw new Error("Trying to redefine non-empty obj['"+n+"']");t[n]=r(i,o)}},i.prototype.object=function(e,t){var i=this;Object.keys(e).forEach(function(o){var n=void 0===t?null:t[o];-1!==o.indexOf(i.seperator)?(i._fill(o.split(i.seperator),e,e[o],n),delete e[o]):i.override&&(e[o]=r(e[o],n))})},i.prototype.str=function(e,t,i,o){-1!==e.indexOf(this.seperator)?this._fill(e.split(this.seperator),i,t,o):this.override&&(i[e]=r(t,o))},i.prototype.pick=function(e,t,r){var i,o,n;if(-1!==e.indexOf(this.seperator)){for(o=e.split(this.seperator),i=0;i<o.length;i++){if(!(t&&"object"==typeof t&&o[i]in t))return void 0;if(i===o.length-1)return r?(n=t[o[i]],delete t[o[i]],n):t[o[i]];t=t[o[i]]}return t}return r?(n=t[e],delete t[e],n):t[e]},i.prototype.remove=function(e,t){return this.pick(e,t,!0)},i.prototype.del=i.prototype.remove,i.prototype.move=function(e,t,i,o,n){return"function"==typeof o||Array.isArray(o)?this.set(t,r(this.pick(e,i,!0),o),i,n):(n=o,this.set(t,this.pick(e,i,!0),i,n)),i},i.prototype.transfer=function(e,t,i,o,n,s){return"function"==typeof n||Array.isArray(n)?this.set(t,r(this.pick(e,i,!0),n),o,s):(s=n,this.set(t,this.pick(e,i,!0),o,s)),o},i.prototype.copy=function(e,t,i,o,n,s){return"function"==typeof n||Array.isArray(n)?this.set(t,r(JSON.parse(JSON.stringify(this.pick(e,i,!1))),n),o,s):(s=n,this.set(t,this.pick(e,i,!1),o,s)),o},i.prototype.set=function(e,t,r,i){var o,s,f;if("undefined"==typeof t)return r;if(-1!==e.indexOf(this.seperator)){for(f=e.split(this.seperator),o=0;o<f.length;o++){if(o===f.length-1)if(i&&n(t)&&n(r[f[o]]))for(s in t)t.hasOwnProperty(s)&&(r[f[o]][s]=t[s]);else if(Array.isArray(r[f[o]])&&Array.isArray(t))for(var p=0;p<t.length;p++)r[f[o]].push(t[p]);else r[f[o]]=t;else r.hasOwnProperty(f[o])&&n(r[f[o]])||(r[f[o]]={});r=r[f[o]]}return r}if(i&&n(t))for(s in t)t.hasOwnProperty(s)&&(r[e][s]=t[s]);else Array.isArray(r[e])&&Array.isArray(t)?r[e].push(t):r[e]=t;return r},i.pick=o("pick"),i.move=o("move"),i.transfer=o("transfer"),i.copy=o("copy"),i.object=o("object"),i.str=o("str"),i.set=o("set"),i.del=i.remove=o("remove"),i._process=r,"function"==typeof define&&define.amd?define(function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:e[t]=i}(this,"DotObject"); |
'use strict'; | ||
var gulp = require('gulp'); | ||
var gutil = require('gulp-util'); | ||
var jshint = require('gulp-jshint'); | ||
@@ -16,3 +17,3 @@ var jscs = require('gulp-jscs'); | ||
gulp.task('jscheck', function() { | ||
gulp.task('jshint', function() { | ||
gulp.src(paths) | ||
@@ -26,7 +27,8 @@ .pipe(jshint()) | ||
gulp.src(['test/**/*.js']) | ||
.pipe(mocha()); | ||
.pipe(mocha()) | ||
.on('error', gutil.log); | ||
}); | ||
gulp.task('watch', function() { | ||
gulp.watch(paths, ['default']); | ||
gulp.watch(paths, ['build-node', 'mocha']); | ||
}); | ||
@@ -41,3 +43,3 @@ | ||
gulp.task('dist', function() { | ||
gulp.task('build-bower', function() { | ||
gulp.src('src/dot-object.js') | ||
@@ -53,4 +55,6 @@ .pipe(hf.header('src/header.tpl')) | ||
gulp.task('test', ['jscheck', 'build-node', 'mocha']); | ||
gulp.task('dist', ['jshint', 'build-node', 'mocha', 'build-bower']); | ||
gulp.task('test', ['jshint', 'build-node', 'mocha']); | ||
gulp.task('default', ['test']); |
91
index.js
'use strict'; | ||
function _process(v, mod) { | ||
var i; | ||
var r; | ||
if (typeof mod === 'function') { | ||
r = mod(v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} else if (Array.isArray(mod)) { | ||
for (i = 0; i < mod.length; i++) { | ||
r = mod[i](v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} | ||
} | ||
return v; | ||
} | ||
function DotObject(seperator, override) { | ||
@@ -44,20 +65,6 @@ | ||
obj[k] = this.process(v, mod); | ||
obj[k] = _process(v, mod); | ||
} | ||
}; | ||
DotObject.prototype.process = function(v, mod) { | ||
var i; | ||
if (typeof mod === 'function') { | ||
v = mod(v); | ||
} else if (mod instanceof Array) { | ||
for (i = 0; i < mod.length; i++) { | ||
v = mod[i](v); | ||
} | ||
} | ||
return v; | ||
}; | ||
DotObject.prototype.object = function(obj, mods) { | ||
@@ -73,3 +80,3 @@ var self = this; | ||
} else if (self.override) { | ||
obj[k] = self.process(obj[k], mod); | ||
obj[k] = _process(obj[k], mod); | ||
} | ||
@@ -91,3 +98,3 @@ }); | ||
} else if (this.override) { | ||
obj[str] = this.process(v, mod); | ||
obj[str] = _process(v, mod); | ||
} | ||
@@ -167,8 +174,14 @@ }; | ||
* @param {Object} obj | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
DotObject.prototype.move = function(source, target, obj, merge) { | ||
DotObject.prototype.move = function(source, target, obj, mods, merge) { | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, _process(this.pick(source, obj, true), mods), obj, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
} | ||
@@ -190,12 +203,22 @@ return obj; | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.transfer = function(source, target, obj1, obj2, merge) { | ||
DotObject.prototype.transfer = | ||
function(source, target, obj1, obj2, mods, merge) { | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
this.pick(source, obj1, true), | ||
mods | ||
), obj2, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
} | ||
return obj2; | ||
return obj2; | ||
}; | ||
}; | ||
/** | ||
@@ -212,7 +235,21 @@ * | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.copy = function(source, target, obj1, obj2, merge) { | ||
DotObject.prototype.copy = function(source, target, obj1, obj2, mods, merge) { | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
JSON.parse( // clone what is picked | ||
JSON.stringify( | ||
this.pick(source, obj1, false) | ||
) | ||
), | ||
mods | ||
), obj2, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
} | ||
@@ -298,2 +335,4 @@ return obj2; | ||
DotObject._process = _process; | ||
module.exports = DotObject; |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"author": { | ||
@@ -30,2 +30,3 @@ "name": "Rob Halff", | ||
"gulp-uglify": "^1.1.0", | ||
"gulp-util": "^3.0.4", | ||
"jscs": "^1.7.3", | ||
@@ -32,0 +33,0 @@ "jscs-jsdoc": "0.0.23", |
'use strict'; | ||
function _process(v, mod) { | ||
var i; | ||
var r; | ||
if (typeof mod === 'function') { | ||
r = mod(v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} else if (Array.isArray(mod)) { | ||
for (i = 0; i < mod.length; i++) { | ||
r = mod[i](v); | ||
if (r !== undefined) { | ||
v = r; | ||
} | ||
} | ||
} | ||
return v; | ||
} | ||
function DotObject(seperator, override) { | ||
@@ -44,20 +65,6 @@ | ||
obj[k] = this.process(v, mod); | ||
obj[k] = _process(v, mod); | ||
} | ||
}; | ||
DotObject.prototype.process = function(v, mod) { | ||
var i; | ||
if (typeof mod === 'function') { | ||
v = mod(v); | ||
} else if (mod instanceof Array) { | ||
for (i = 0; i < mod.length; i++) { | ||
v = mod[i](v); | ||
} | ||
} | ||
return v; | ||
}; | ||
DotObject.prototype.object = function(obj, mods) { | ||
@@ -73,3 +80,3 @@ var self = this; | ||
} else if (self.override) { | ||
obj[k] = self.process(obj[k], mod); | ||
obj[k] = _process(obj[k], mod); | ||
} | ||
@@ -91,3 +98,3 @@ }); | ||
} else if (this.override) { | ||
obj[str] = this.process(v, mod); | ||
obj[str] = _process(v, mod); | ||
} | ||
@@ -167,8 +174,14 @@ }; | ||
* @param {Object} obj | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
* | ||
*/ | ||
DotObject.prototype.move = function(source, target, obj, merge) { | ||
DotObject.prototype.move = function(source, target, obj, mods, merge) { | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, _process(this.pick(source, obj, true), mods), obj, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj, true), obj, merge); | ||
} | ||
@@ -190,12 +203,22 @@ return obj; | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.transfer = function(source, target, obj1, obj2, merge) { | ||
DotObject.prototype.transfer = | ||
function(source, target, obj1, obj2, mods, merge) { | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
this.pick(source, obj1, true), | ||
mods | ||
), obj2, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, true), obj2, merge); | ||
} | ||
return obj2; | ||
return obj2; | ||
}; | ||
}; | ||
/** | ||
@@ -212,7 +235,21 @@ * | ||
* @param {Object} obj2 | ||
* @param {Function|Array} mods | ||
* @param {Boolean} merge | ||
*/ | ||
DotObject.prototype.copy = function(source, target, obj1, obj2, merge) { | ||
DotObject.prototype.copy = function(source, target, obj1, obj2, mods, merge) { | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
if (typeof mods === 'function' || Array.isArray(mods)) { | ||
this.set(target, | ||
_process( | ||
JSON.parse( // clone what is picked | ||
JSON.stringify( | ||
this.pick(source, obj1, false) | ||
) | ||
), | ||
mods | ||
), obj2, merge); | ||
} else { | ||
merge = mods; | ||
this.set(target, this.pick(source, obj1, false), obj2, merge); | ||
} | ||
@@ -297,1 +334,3 @@ return obj2; | ||
DotObject.del = DotObject.remove = wrap('remove'); | ||
DotObject._process = _process; |
@@ -57,2 +57,23 @@ 'use strict'; | ||
it('Should process modifiers', function() { | ||
var link = { | ||
source: 'one', | ||
target: 'two', | ||
}; | ||
var expected = { | ||
source: {id: 'ONE'}, | ||
target: {port: 'TWO'}, | ||
}; | ||
function up(val) { return val.toUpperCase(); } | ||
Dot.move('source', 'source.id', link, up); | ||
Dot.move('target', 'target.port', link, up); | ||
link.should.eql(expected); | ||
}); | ||
}); |
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
54744
22
1591
14