dot-object
Advanced tools
Comparing version 1.7.1 to 1.8.0
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"homepage": "https://github.com/rhalff/dot-object", | ||
@@ -31,0 +31,0 @@ "moduleType": [ |
# ChangeLog | ||
## 2019-07-18 Version 1.8.0 | ||
* [[`cdc691424b`](https://github.com/rhalff/dot-object/commit/cdc691424b)] - Options to remove array elements and reindex the array. (Fixed by MechJosh0 #36) | ||
## 2018-10-26 Version 1.7.1 | ||
@@ -4,0 +7,0 @@ * [[`e1bb99c83e`](https://github.com/rhalff/dot-object/commit/e1bb99c83e)] - Fix isIndex numeric key matching. (Fixed by mrdivyansh #31) |
@@ -56,2 +56,4 @@ (function(global, exportName) { | ||
var hasOwnProperty = Object.prototype.hasOwnProperty | ||
function DotObject(separator, override, useArray) { | ||
@@ -167,3 +169,3 @@ if (!(this instanceof DotObject)) { | ||
this._fill(path.split(this.separator), obj, v, mod) | ||
} else if (!obj.hasOwnProperty(path) || this.override) { | ||
} else if (!hasOwnProperty.call(obj, path) || this.override) { | ||
obj[path] = _process(v, mod) | ||
@@ -185,3 +187,3 @@ } | ||
*/ | ||
DotObject.prototype.pick = function(path, obj, remove) { | ||
DotObject.prototype.pick = function(path, obj, remove, reindexArray) { | ||
var i | ||
@@ -200,3 +202,7 @@ var keys | ||
val = obj[key] | ||
delete obj[key] | ||
if (reindexArray && Array.isArray(obj)) { | ||
obj.splice(key, 1) | ||
} else { | ||
delete obj[key] | ||
} | ||
if (Array.isArray(obj)) { | ||
@@ -226,2 +232,13 @@ cp = keys.slice(0, -1).join('.') | ||
} | ||
/** | ||
* | ||
* Delete value from an object using dot notation. | ||
* | ||
* @param {String} path | ||
* @param {Object} obj | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.delete = function(path, obj) { | ||
return this.remove(path, obj, true) | ||
} | ||
@@ -232,7 +249,14 @@ /** | ||
* | ||
* @param {String} path | ||
* Will remove multiple items if path is an array. | ||
* In this case array indexes will be retained until all | ||
* removals have been processed. | ||
* | ||
* Use dot.delete() to automatically re-index arrays. | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @return {Mixed} The removed value | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.remove = function(path, obj) { | ||
DotObject.prototype.remove = function(path, obj, reindexArray) { | ||
var i | ||
@@ -243,8 +267,10 @@ | ||
for (i = 0; i < path.length; i++) { | ||
this.pick(path[i], obj, true) | ||
this.pick(path[i], obj, true, reindexArray) | ||
} | ||
this._cleanup(obj) | ||
if (!reindexArray) { | ||
this._cleanup(obj) | ||
} | ||
return obj | ||
} else { | ||
return this.pick(path, obj, true) | ||
return this.pick(path, obj, true, reindexArray) | ||
} | ||
@@ -272,3 +298,12 @@ } | ||
// alias method | ||
/** | ||
* Alias method for `dot.remove` | ||
* | ||
* Note: this is not an alias for dot.delete() | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.del = DotObject.prototype.remove | ||
@@ -368,3 +403,3 @@ | ||
* @param {String} path | ||
* @param {Mixed} val | ||
* @param {any} val | ||
* @param {Object} obj | ||
@@ -390,3 +425,3 @@ * @param {Boolean} merge | ||
for (k in val) { | ||
if (val.hasOwnProperty(k)) { | ||
if (hasOwnProperty.call(val, k)) { | ||
obj[key][k] = val[k] | ||
@@ -404,3 +439,3 @@ } | ||
// force the value to be an object | ||
!obj.hasOwnProperty(key) || | ||
!hasOwnProperty.call(obj, key) || | ||
(!isObject(obj[key]) && !Array.isArray(obj[key])) | ||
@@ -407,0 +442,0 @@ ) { |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"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 c(t){return"[object Object]"===Object.prototype.toString.call(t)}function f(t){return Object(t)===t}function p(t){return 0===Object.keys(t).length}function a(t,r){return 0<=t.indexOf("[")&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(r)}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 n(t){return function(){return e[t].apply(e,arguments)}}i.prototype._fill=function(t,r,e,i){var n,o=t.shift();if(0<t.length){if(r[o]=r[o]||(this.useArray&&(n=t[0],/^\d+$/.test(n))?[]:{}),!f(r[o])){if(!this.override){if(!f(e)||!p(e))throw new Error("Trying to redefine `"+o+"` which is a "+typeof r[o]);return}r[o]={}}this._fill(t,r[o],e,i)}else{if(!this.override&&f(r[o])&&!p(r[o])){if(!f(e)||!p(e))throw new Error("Trying to redefine non-empty obj['"+o+"']");return}r[o]=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=a(t,o.separator).join(o.separator);-1!==e.indexOf(o.separator)?(o._fill(e.split(o.separator),i,i[t],r),delete i[t]):o.override&&(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.hasOwnProperty(t)&&!this.override||(e[t]=s(r,i)),e},i.prototype.pick=function(t,r,e){var i,n,o,s,f,p,c;for(n=a(t,this.separator),i=0;i<n.length;i++){if(p=n[i],c=r,s="-"===p[0]&&Array.isArray(c)&&/^-\d+$/.test(p)?c.length+parseInt(p,10):p,!(r&&"object"==typeof r&&s in r))return;if(i===n.length-1)return e?(o=r[s],delete r[s],Array.isArray(r)&&(f=n.slice(0,-1).join("."),-1===this.cleanup.indexOf(f)&&this.cleanup.push(f)),o):r[s];r=r[s]}return e&&Array.isArray(r)&&(r=r.filter(function(t){return void 0!==t})),r},i.prototype.remove=function(t,r){var e;if(this.cleanup=[],Array.isArray(t)){for(e=0;e<t.length;e++)this.pick(t[e],r,!0);return this._cleanup(r),r}return this.pick(t,r,!0)},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=a(t,this.separator),n=0;n<s.length;n++){if(f=s[n],n===s.length-1)if(i&&c(r)&&c(e[f]))for(o in r)r.hasOwnProperty(o)&&(e[f][o]=r[o]);else if(i&&Array.isArray(e[f])&&Array.isArray(r))for(var p=0;p<r.length;p++)e[s[n]].push(r[p]);else e[f]=r;else e.hasOwnProperty(f)&&(c(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(f(r[t])&&(c(r[t])&&!p(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=n("pick"),i.move=n("move"),i.transfer=n("transfer"),i.transform=n("transform"),i.copy=n("copy"),i.object=n("object"),i.str=n("str"),i.set=n("set"),i.del=i.remove=n("remove"),i.dot=n("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 p(t){return"[object Object]"===Object.prototype.toString.call(t)}function o(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 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)||!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&&o(r[n])&&!c(r[n])){if(!o(e)||!c(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]):o.override&&(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):a.call(e,t)&&!this.override||(e[t]=s(r,i)),e},i.prototype.pick=function(t,r,e,i){var n,o,s,c,f,p,a;for(o=u(t,this.separator),n=0;n<o.length;n++){if(p=o[n],a=r,c="-"===p[0]&&Array.isArray(a)&&/^-\d+$/.test(p)?a.length+parseInt(p,10):p,!(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)&&(f=o.slice(0,-1).join("."),-1===this.cleanup.indexOf(f)&&this.cleanup.push(f)),s):r[c];r=r[c]}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,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&&p(r)&&p(e[c]))for(o in r)a.call(r,o)&&(e[c][o]=r[o]);else if(i&&Array.isArray(e[c])&&Array.isArray(r))for(var f=0;f<r.length;f++)e[s[n]].push(r[f]);else e[c]=r;else a.call(e,c)&&(p(e[c])||Array.isArray(e[c]))||(/^\d+$/.test(s[n+1])?e[c]=[]:e[c]={});e=e[c]}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])&&!c(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); |
@@ -10,3 +10,3 @@ 'use strict' | ||
var beautify = require('gulp-beautify') | ||
var standard = require('gulp-standard') | ||
var eslint = require('gulp-eslint') | ||
@@ -17,21 +17,22 @@ var DEST = 'dist/' | ||
gulp.task('lint', function () { | ||
gulp.task('lint', function (done) { | ||
gulp.src(paths) | ||
.pipe(standard()) | ||
.pipe(standard.reporter('default', { | ||
breakOnError: true | ||
})) | ||
.pipe(eslint()) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()) | ||
done() | ||
}) | ||
gulp.task('mocha', function () { | ||
gulp.task('mocha', function (done) { | ||
gulp.src(['test/**/*.js']) | ||
.pipe(mocha()) | ||
.on('error', gutil.log) | ||
done() | ||
}) | ||
gulp.task('watch', function () { | ||
gulp.watch(paths, ['build-node', 'mocha']) | ||
gulp.watch(paths, gulp.series('build-node', 'mocha')) | ||
}) | ||
gulp.task('build-node', function () { | ||
gulp.task('build-node', function (done) { | ||
gulp.src('src/dot-object.js') | ||
@@ -41,5 +42,6 @@ .pipe(hf.footer('\nmodule.exports = DotObject\n')) | ||
.pipe(gulp.dest('./')) | ||
done() | ||
}) | ||
gulp.task('build-bower', function () { | ||
gulp.task('build-bower', function (done) { | ||
gulp.src('src/dot-object.js') | ||
@@ -53,8 +55,9 @@ .pipe(hf.header('src/header.tpl')) | ||
.pipe(gulp.dest(DEST)) | ||
done() | ||
}) | ||
gulp.task('dist', ['lint', 'build-node', 'mocha', 'build-bower']) | ||
gulp.task('dist', gulp.parallel('lint', 'build-node', 'mocha', 'build-bower')) | ||
gulp.task('test', ['lint', 'build-node', 'mocha']) | ||
gulp.task('test', gulp.parallel('lint', 'build-node', 'mocha')) | ||
gulp.task('default', ['test']) | ||
gulp.task('default', gulp.parallel('test')) |
61
index.js
@@ -55,2 +55,4 @@ 'use strict' | ||
var hasOwnProperty = Object.prototype.hasOwnProperty | ||
function DotObject (separator, override, useArray) { | ||
@@ -165,3 +167,3 @@ if (!(this instanceof DotObject)) { | ||
this._fill(path.split(this.separator), obj, v, mod) | ||
} else if (!obj.hasOwnProperty(path) || this.override) { | ||
} else if (!hasOwnProperty.call(obj, path) || this.override) { | ||
obj[path] = _process(v, mod) | ||
@@ -183,3 +185,3 @@ } | ||
*/ | ||
DotObject.prototype.pick = function (path, obj, remove) { | ||
DotObject.prototype.pick = function (path, obj, remove, reindexArray) { | ||
var i | ||
@@ -198,3 +200,7 @@ var keys | ||
val = obj[key] | ||
delete obj[key] | ||
if (reindexArray && Array.isArray(obj)) { | ||
obj.splice(key, 1) | ||
} else { | ||
delete obj[key] | ||
} | ||
if (Array.isArray(obj)) { | ||
@@ -222,2 +228,13 @@ cp = keys.slice(0, -1).join('.') | ||
} | ||
/** | ||
* | ||
* Delete value from an object using dot notation. | ||
* | ||
* @param {String} path | ||
* @param {Object} obj | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.delete = function (path, obj) { | ||
return this.remove(path, obj, true) | ||
} | ||
@@ -228,7 +245,14 @@ /** | ||
* | ||
* @param {String} path | ||
* Will remove multiple items if path is an array. | ||
* In this case array indexes will be retained until all | ||
* removals have been processed. | ||
* | ||
* Use dot.delete() to automatically re-index arrays. | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @return {Mixed} The removed value | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.remove = function (path, obj) { | ||
DotObject.prototype.remove = function (path, obj, reindexArray) { | ||
var i | ||
@@ -239,8 +263,10 @@ | ||
for (i = 0; i < path.length; i++) { | ||
this.pick(path[i], obj, true) | ||
this.pick(path[i], obj, true, reindexArray) | ||
} | ||
this._cleanup(obj) | ||
if (!reindexArray) { | ||
this._cleanup(obj) | ||
} | ||
return obj | ||
} else { | ||
return this.pick(path, obj, true) | ||
return this.pick(path, obj, true, reindexArray) | ||
} | ||
@@ -266,3 +292,12 @@ } | ||
// alias method | ||
/** | ||
* Alias method for `dot.remove` | ||
* | ||
* Note: this is not an alias for dot.delete() | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.del = DotObject.prototype.remove | ||
@@ -362,3 +397,3 @@ | ||
* @param {String} path | ||
* @param {Mixed} val | ||
* @param {any} val | ||
* @param {Object} obj | ||
@@ -384,3 +419,3 @@ * @param {Boolean} merge | ||
for (k in val) { | ||
if (val.hasOwnProperty(k)) { | ||
if (hasOwnProperty.call(val, k)) { | ||
obj[key][k] = val[k] | ||
@@ -398,3 +433,3 @@ } | ||
// force the value to be an object | ||
!obj.hasOwnProperty(key) || | ||
!hasOwnProperty.call(obj, key) || | ||
(!isObject(obj[key]) && !Array.isArray(obj[key])) | ||
@@ -401,0 +436,0 @@ ) { |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "1.7.1", | ||
"version": "1.8.0", | ||
"author": { | ||
@@ -32,11 +32,15 @@ "name": "Rob Halff", | ||
"devDependencies": { | ||
"gulp": "^3.9.1", | ||
"gulp-beautify": "^2.0.1", | ||
"eslint": "^6.0.1", | ||
"eslint-plugin-import": "^2.18.0", | ||
"eslint-plugin-node": "^9.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"gulp": "^4.0.2", | ||
"gulp-beautify": "^3.0.0", | ||
"gulp-eslint": "^6.0.0", | ||
"gulp-headerfooter": "^1.0.3", | ||
"gulp-mocha": "^6.0.0", | ||
"gulp-rename": "^1.4.0", | ||
"gulp-standard": "^12.0.0", | ||
"gulp-uglify": "^3.0.1", | ||
"gulp-uglify": "^3.0.2", | ||
"gulp-util": "^3.0.8", | ||
"mocha": "5.x.x", | ||
"mocha": "6.x.x", | ||
"should": "13.x.x", | ||
@@ -53,5 +57,5 @@ "underscore.string": "latest" | ||
"dependencies": { | ||
"commander": "^2.19.0", | ||
"glob": "^7.1.3" | ||
"commander": "^2.20.0", | ||
"glob": "^7.1.4" | ||
} | ||
} |
@@ -168,3 +168,6 @@ [![Build Status](https://travis-ci.org/rhalff/dot-object.png)](https://travis-ci.org/rhalff/dot-object) | ||
#### Pick/remove a value using dot notation: | ||
#### Pick a value using dot notation: | ||
Picks a value from the object without removing it. | ||
```js | ||
@@ -185,12 +188,41 @@ var dot = require('dot-object'); | ||
Hi there! | ||
``` | ||
// Pick & Remove the value | ||
val = dot.pick('some.nested.value', obj, true); | ||
#### Delete/Remove a value using dot notation: | ||
// shorthand | ||
val = dot.remove('some.nested.value', obj); | ||
Remove and delete mostly behave the same, but in case of a path addressing array items: | ||
// or use the alias `del` | ||
val = dot.del('some.nested.value', obj); | ||
- `delete` will re-index the array. | ||
- `remove` will retain array indexes | ||
```js | ||
var dot = require('dot-object'); | ||
var obj = { | ||
a: 'Hi There!', | ||
nested: { | ||
array: [ | ||
'Veni', | ||
'Vidi', | ||
'Vici', | ||
] | ||
} | ||
}; | ||
var val = dot.delete('a', obj); | ||
console.log(val); | ||
Hi There! | ||
// To remove an item and directly update any array indexes use: | ||
var val = dot.delete('nested.array[1]', obj); | ||
console.log(val); | ||
Vidi | ||
// Remove a value but retain array indexes. | ||
var val = dot.remove('nested.array[1]', obj); | ||
// To remove multiple paths at once: | ||
var val = dot.remove(['nested.array[0]', 'nested.array[2]'], obj); | ||
``` | ||
@@ -197,0 +229,0 @@ |
@@ -55,2 +55,4 @@ 'use strict' | ||
var hasOwnProperty = Object.prototype.hasOwnProperty | ||
function DotObject (separator, override, useArray) { | ||
@@ -165,3 +167,3 @@ if (!(this instanceof DotObject)) { | ||
this._fill(path.split(this.separator), obj, v, mod) | ||
} else if (!obj.hasOwnProperty(path) || this.override) { | ||
} else if (!hasOwnProperty.call(obj, path) || this.override) { | ||
obj[path] = _process(v, mod) | ||
@@ -183,3 +185,3 @@ } | ||
*/ | ||
DotObject.prototype.pick = function (path, obj, remove) { | ||
DotObject.prototype.pick = function (path, obj, remove, reindexArray) { | ||
var i | ||
@@ -198,3 +200,7 @@ var keys | ||
val = obj[key] | ||
delete obj[key] | ||
if (reindexArray && Array.isArray(obj)) { | ||
obj.splice(key, 1) | ||
} else { | ||
delete obj[key] | ||
} | ||
if (Array.isArray(obj)) { | ||
@@ -222,2 +228,13 @@ cp = keys.slice(0, -1).join('.') | ||
} | ||
/** | ||
* | ||
* Delete value from an object using dot notation. | ||
* | ||
* @param {String} path | ||
* @param {Object} obj | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.delete = function (path, obj) { | ||
return this.remove(path, obj, true) | ||
} | ||
@@ -228,7 +245,14 @@ /** | ||
* | ||
* @param {String} path | ||
* Will remove multiple items if path is an array. | ||
* In this case array indexes will be retained until all | ||
* removals have been processed. | ||
* | ||
* Use dot.delete() to automatically re-index arrays. | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @return {Mixed} The removed value | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.remove = function (path, obj) { | ||
DotObject.prototype.remove = function (path, obj, reindexArray) { | ||
var i | ||
@@ -239,8 +263,10 @@ | ||
for (i = 0; i < path.length; i++) { | ||
this.pick(path[i], obj, true) | ||
this.pick(path[i], obj, true, reindexArray) | ||
} | ||
this._cleanup(obj) | ||
if (!reindexArray) { | ||
this._cleanup(obj) | ||
} | ||
return obj | ||
} else { | ||
return this.pick(path, obj, true) | ||
return this.pick(path, obj, true, reindexArray) | ||
} | ||
@@ -266,3 +292,12 @@ } | ||
// alias method | ||
/** | ||
* Alias method for `dot.remove` | ||
* | ||
* Note: this is not an alias for dot.delete() | ||
* | ||
* @param {String|Array<String>} path | ||
* @param {Object} obj | ||
* @param {Boolean} reindexArray | ||
* @return {any} The removed value | ||
*/ | ||
DotObject.prototype.del = DotObject.prototype.remove | ||
@@ -362,3 +397,3 @@ | ||
* @param {String} path | ||
* @param {Mixed} val | ||
* @param {any} val | ||
* @param {Object} obj | ||
@@ -384,3 +419,3 @@ * @param {Boolean} merge | ||
for (k in val) { | ||
if (val.hasOwnProperty(k)) { | ||
if (hasOwnProperty.call(val, k)) { | ||
obj[key][k] = val[k] | ||
@@ -398,3 +433,3 @@ } | ||
// force the value to be an object | ||
!obj.hasOwnProperty(key) || | ||
!hasOwnProperty.call(obj, key) || | ||
(!isObject(obj[key]) && !Array.isArray(obj[key])) | ||
@@ -401,0 +436,0 @@ ) { |
@@ -14,10 +14,10 @@ 'use strict' | ||
path: [{ | ||
'longitude': 5.512482166290283, | ||
'latitude': 52.5006217956543 | ||
longitude: 5.512482166290283, | ||
latitude: 52.5006217956543 | ||
}, { | ||
'longitude': 5.512370586395264, | ||
'latitude': 52.50059509277344 | ||
longitude: 5.512370586395264, | ||
latitude: 52.50059509277344 | ||
}, { | ||
'longitude': 5.512370586395264, | ||
'latitude': 52.50059509277344 | ||
longitude: 5.512370586395264, | ||
latitude: 52.50059509277344 | ||
}] | ||
@@ -24,0 +24,0 @@ } |
@@ -10,3 +10,3 @@ 'use strict' | ||
var row = { | ||
'id': 2, | ||
id: 2, | ||
'contact.name.first': 'John', | ||
@@ -21,12 +21,12 @@ 'contact.name.last': 'Doe', | ||
row.should.eql({ | ||
'id': 2, | ||
'contact': { | ||
'name': { | ||
'first': 'John', | ||
'last': 'Doe' | ||
id: 2, | ||
contact: { | ||
name: { | ||
first: 'John', | ||
last: 'Doe' | ||
}, | ||
'email': 'example@gmail.com', | ||
'info': { | ||
'about': { | ||
'me': 'classified' | ||
email: 'example@gmail.com', | ||
info: { | ||
about: { | ||
me: 'classified' | ||
} | ||
@@ -40,3 +40,3 @@ } | ||
var row = { | ||
'id': 2, | ||
id: 2, | ||
'my.arr.0': 'one', | ||
@@ -53,6 +53,6 @@ 'my.arr.1': 'two', | ||
row.should.eql({ | ||
'id': 2, | ||
'my': { | ||
'arr': ['one', 'two', 'three'], | ||
'arr2': ['one', 'two', 'three'] | ||
id: 2, | ||
my: { | ||
arr: ['one', 'two', 'three'], | ||
arr2: ['one', 'two', 'three'] | ||
} | ||
@@ -64,3 +64,3 @@ }) | ||
var row = { | ||
'id': 2, | ||
id: 2, | ||
'0A': 'a', | ||
@@ -74,3 +74,3 @@ '0A9': 'b', | ||
row.should.eql({ | ||
'id': 2, | ||
id: 2, | ||
'0A': 'a', | ||
@@ -80,3 +80,3 @@ '0A9': 'b', | ||
'1AB': { | ||
'A34C9': 'c' | ||
A34C9: 'c' | ||
} | ||
@@ -93,6 +93,6 @@ } | ||
tgt.should.eql({ | ||
'this': { | ||
'is': { | ||
'my': { | ||
'string': 'value' | ||
this: { | ||
is: { | ||
my: { | ||
string: 'value' | ||
} | ||
@@ -106,3 +106,3 @@ } | ||
var tgt = { | ||
'already': 'set' | ||
already: 'set' | ||
} | ||
@@ -121,6 +121,6 @@ | ||
tgt.should.eql({ | ||
'this': { | ||
'is': { | ||
'my': { | ||
'string': 'Value' | ||
this: { | ||
is: { | ||
my: { | ||
string: 'Value' | ||
} | ||
@@ -142,6 +142,6 @@ } | ||
tgt.should.eql({ | ||
'this': { | ||
'is': { | ||
'my': { | ||
'string': 'this_is_a_test' | ||
this: { | ||
is: { | ||
my: { | ||
string: 'this_is_a_test' | ||
} | ||
@@ -166,3 +166,3 @@ } | ||
row.should.eql({ 'page': { 'title': 'My Page', 'slug': 'my-page' } }) | ||
row.should.eql({ page: { title: 'My Page', slug: 'my-page' } }) | ||
}) | ||
@@ -172,9 +172,9 @@ | ||
function () { | ||
var row = { 'title': 'my page', 'slug': 'My Page' } | ||
var row = { title: 'my page', slug: 'My Page' } | ||
var mods = { 'title': _s.titleize, 'slug': _s.slugify } | ||
var mods = { title: _s.titleize, slug: _s.slugify } | ||
Dot.object(row, mods) | ||
row.should.eql({ 'title': 'my page', 'slug': 'My Page' }) | ||
row.should.eql({ title: 'my page', slug: 'My Page' }) | ||
} | ||
@@ -190,3 +190,3 @@ ) | ||
row.should.eql({ 'page': { 'name': 'my_page' } }) | ||
row.should.eql({ page: { name: 'my_page' } }) | ||
}) | ||
@@ -202,4 +202,4 @@ | ||
row.should.eql({ 'page': { 'name': 'my_page' } }) | ||
row.should.eql({ page: { name: 'my_page' } }) | ||
}) | ||
}) |
@@ -69,3 +69,3 @@ 'use strict' | ||
it('Always keeps empty arrays', function () { | ||
Dot.dot({ hello: [] }).should.eql({ 'hello': [] }) | ||
Dot.dot({ hello: [] }).should.eql({ hello: [] }) | ||
Dot.dot({ hello: { world: [] } }).should.eql({ 'hello.world': [] }) | ||
@@ -75,5 +75,5 @@ }) | ||
it('Always keeps empty objects', function () { | ||
Dot.dot({ hello: {} }).should.eql({ 'hello': {} }) | ||
Dot.dot({ hello: {} }).should.eql({ hello: {} }) | ||
Dot.dot({ hello: { world: {} } }).should.eql({ 'hello.world': {} }) | ||
}) | ||
}) |
@@ -12,4 +12,4 @@ 'use strict' | ||
var obj = { | ||
'some': 'value', | ||
'already': 'set' | ||
some: 'value', | ||
already: 'set' | ||
} | ||
@@ -20,4 +20,4 @@ | ||
obj.should.eql({ | ||
'some': 'value', | ||
'already': { 'new': 'value' } | ||
some: 'value', | ||
already: { new: 'value' } | ||
}) | ||
@@ -30,4 +30,4 @@ }) | ||
var obj = { | ||
'some': 'value', | ||
'already': 'set' | ||
some: 'value', | ||
already: 'set' | ||
} | ||
@@ -39,4 +39,4 @@ | ||
obj.should.eql({ | ||
'some': 'new_value', | ||
'already': { 'new': 'value' } | ||
some: 'new_value', | ||
already: { new: 'value' } | ||
}) | ||
@@ -80,9 +80,9 @@ }) | ||
var row = { | ||
'title': 'my page', | ||
'slug': 'My Page' | ||
title: 'my page', | ||
slug: 'My Page' | ||
} | ||
var mods = { | ||
'title': _s.titleize, | ||
'slug': _s.slugify | ||
title: _s.titleize, | ||
slug: _s.slugify | ||
} | ||
@@ -93,4 +93,4 @@ | ||
row.should.eql({ | ||
'title': 'My Page', | ||
'slug': 'my-page' | ||
title: 'My Page', | ||
slug: 'my-page' | ||
}) | ||
@@ -97,0 +97,0 @@ } |
@@ -11,4 +11,4 @@ 'use strict' | ||
var obj = { | ||
'some': 'value', | ||
'already': 'set' | ||
some: 'value', | ||
already: 'set' | ||
} | ||
@@ -23,4 +23,4 @@ | ||
var obj = { | ||
'some': { | ||
'other': 'value' | ||
some: { | ||
other: 'value' | ||
} | ||
@@ -36,3 +36,3 @@ } | ||
var obj = { | ||
'some': null | ||
some: null | ||
} | ||
@@ -47,3 +47,3 @@ | ||
var obj = { | ||
'some': null | ||
some: null | ||
} | ||
@@ -59,3 +59,3 @@ | ||
var obj = { | ||
'some': null | ||
some: null | ||
} | ||
@@ -71,4 +71,4 @@ | ||
var obj = { | ||
'some': { | ||
'other': 'value' | ||
some: { | ||
other: 'value' | ||
} | ||
@@ -84,2 +84,58 @@ } | ||
}) | ||
it('Should be able to delete picked value', function () { | ||
var obj = { | ||
some: { | ||
other: 'value', | ||
foo: 'bar' | ||
} | ||
} | ||
var val = Dot.pick('some.foo', obj, true) | ||
val.should.eql('bar') | ||
obj.should.eql({ | ||
some: { | ||
other: 'value' | ||
} | ||
}) | ||
}) | ||
it('Should be able to delete picked array value', function () { | ||
var obj = { | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'bar', 'baz'] | ||
} | ||
} | ||
var val = Dot.pick('some.arrayItems[1]', obj, true) | ||
val.should.eql('bar') | ||
obj.should.eql({ | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', , 'baz'] /* eslint-disable-line no-sparse-arrays */ | ||
} | ||
}) | ||
}) | ||
it('Should be able to delete picked array value and reindex', function () { | ||
var obj = { | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'bar', 'baz'] | ||
} | ||
} | ||
var val = Dot.pick('some.arrayItems[1]', obj, true, true) | ||
val.should.eql('bar') | ||
obj.should.eql({ | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'baz'] | ||
} | ||
}) | ||
}) | ||
}) |
@@ -43,2 +43,39 @@ 'use strict' | ||
}) | ||
it('Should be able to remove() array item and reindex array', function () { | ||
var obj = { | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'bar', 'baz'] | ||
} | ||
} | ||
var val = Dot.remove('some.arrayItems[1]', obj, true, true) | ||
val.should.eql('bar') | ||
obj.should.eql({ | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'baz'] | ||
} | ||
}) | ||
}) | ||
it('Should be handle being told to reindex an object by ignoring reindex rule', function () { | ||
var obj = { | ||
some: { | ||
other: 'value', | ||
arrayItems: ['foo', 'bar', 'baz'] | ||
} | ||
} | ||
var val = Dot.remove('some.other', obj, true, true) | ||
val.should.eql('value') | ||
obj.should.eql({ | ||
some: { | ||
arrayItems: ['foo', 'bar', 'baz'] | ||
} | ||
}) | ||
}) | ||
}) |
@@ -25,3 +25,3 @@ 'use strict' | ||
a: { | ||
'0': 'value' | ||
0: 'value' | ||
} | ||
@@ -31,3 +31,3 @@ } | ||
a: { | ||
'0': 'value' | ||
0: 'value' | ||
} | ||
@@ -34,0 +34,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
100616
3075
407
15
Updatedcommander@^2.20.0
Updatedglob@^7.1.4