From ff37929e366c38d53bb24de63a2dfdcf469f070a Mon Sep 17 00:00:00 2001 From: Rob Halff Date: Thu, 18 Jul 2019 17:16:35 +0200 Subject: [PATCH 1/3] prepare release --- CHANGELOG.md | 3 +++ README.md | 46 +++++++++++++++++++++++++++++++++++++++------- index.js | 39 ++++++++++++++++++++++++++++++++++----- package.json | 2 +- src/dot-object.js | 39 ++++++++++++++++++++++++++++++++++----- 5 files changed, 111 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dc0afb..5a21a3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 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 * [[`e1bb99c83e`](https://github.com/rhalff/dot-object/commit/e1bb99c83e)] - Fix isIndex numeric key matching. (Fixed by mrdivyansh #31) diff --git a/README.md b/README.md index f11f271..a65885f 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,10 @@ console.log(tgt); } ``` -#### Pick/remove a value using dot notation: +#### Pick a value using dot notation: + +Picks a value from the object without removing it. + ```js var dot = require('dot-object'); @@ -181,16 +184,45 @@ var val = dot.pick('some.nested.value', obj); console.log(val); Hi there! +``` + +#### Delete/Remove a value using dot notation: + +Remove and delete mostly behave the same, but in case of a path addressing array items: + + - `delete` will re-index the array. + - `remove` will retain array indexes + +```js +var dot = require('dot-object'); -// Pick & Remove the value -val = dot.pick('some.nested.value', obj, true); +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); -// shorthand -val = dot.remove('some.nested.value', obj); +Vidi -// or use the alias `del` -val = dot.del('some.nested.value', obj); +// 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); ``` ### Using modifiers diff --git a/index.js b/index.js index 60fba88..f7e33fb 100644 --- a/index.js +++ b/index.js @@ -221,14 +221,32 @@ DotObject.prototype.pick = function (path, obj, remove, reindexArray) { } return obj } +/** + * + * 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) +} /** * * Remove value from an object using dot notation. * - * @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} path * @param {Object} obj - * @return {Mixed} The removed value + * @param {Boolean} reindexArray + * @return {any} The removed value */ DotObject.prototype.remove = function (path, obj, reindexArray) { var i @@ -238,7 +256,9 @@ DotObject.prototype.remove = function (path, obj, reindexArray) { for (i = 0; i < path.length; i++) { this.pick(path[i], obj, true, reindexArray) } - this._cleanup(obj) + if (!reindexArray) { + this._cleanup(obj) + } return obj } else { return this.pick(path, obj, true, reindexArray) @@ -262,7 +282,16 @@ DotObject.prototype._cleanup = function (obj) { } } -// alias method +/** + * Alias method for `dot.remove` + * + * Note: this is not an alias for dot.delete() + * + * @param {String|Array} path + * @param {Object} obj + * @param {Boolean} reindexArray + * @return {any} The removed value + */ DotObject.prototype.del = DotObject.prototype.remove /** @@ -357,7 +386,7 @@ DotObject.prototype.copy = function (source, target, obj1, obj2, mods, merge) { * Set a property on an object using dot notation. * * @param {String} path - * @param {Mixed} val + * @param {any} val * @param {Object} obj * @param {Boolean} merge */ diff --git a/package.json b/package.json index 3bc1821..bd24d05 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dot-object", "description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", - "version": "1.7.2", + "version": "1.8.0", "author": { "name": "Rob Halff", "email": "rob.halff@gmail.com" diff --git a/src/dot-object.js b/src/dot-object.js index 22e15be..5c917f6 100644 --- a/src/dot-object.js +++ b/src/dot-object.js @@ -221,14 +221,32 @@ DotObject.prototype.pick = function (path, obj, remove, reindexArray) { } return obj } +/** + * + * 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) +} /** * * Remove value from an object using dot notation. * - * @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} path * @param {Object} obj - * @return {Mixed} The removed value + * @param {Boolean} reindexArray + * @return {any} The removed value */ DotObject.prototype.remove = function (path, obj, reindexArray) { var i @@ -238,7 +256,9 @@ DotObject.prototype.remove = function (path, obj, reindexArray) { for (i = 0; i < path.length; i++) { this.pick(path[i], obj, true, reindexArray) } - this._cleanup(obj) + if (!reindexArray) { + this._cleanup(obj) + } return obj } else { return this.pick(path, obj, true, reindexArray) @@ -262,7 +282,16 @@ DotObject.prototype._cleanup = function (obj) { } } -// alias method +/** + * Alias method for `dot.remove` + * + * Note: this is not an alias for dot.delete() + * + * @param {String|Array} path + * @param {Object} obj + * @param {Boolean} reindexArray + * @return {any} The removed value + */ DotObject.prototype.del = DotObject.prototype.remove /** @@ -357,7 +386,7 @@ DotObject.prototype.copy = function (source, target, obj1, obj2, mods, merge) { * Set a property on an object using dot notation. * * @param {String} path - * @param {Mixed} val + * @param {any} val * @param {Object} obj * @param {Boolean} merge */ From e2a5a1cc2540d405433618f22fe118cdc057da7e Mon Sep 17 00:00:00 2001 From: Rob Halff Date: Thu, 18 Jul 2019 17:17:39 +0200 Subject: [PATCH 2/3] update bower --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 665bcb1..647d5f1 100644 --- a/bower.json +++ b/bower.json @@ -25,7 +25,7 @@ "type": "git", "url": "git://github.com/rhalff/dot-object.git" }, - "version": "1.7.1", + "version": "1.8.0", "homepage": "https://github.com/rhalff/dot-object", "moduleType": [ "amd", From ddb5aa984164717619126e66579d4eacf8c5708d Mon Sep 17 00:00:00 2001 From: Rob Halff Date: Thu, 18 Jul 2019 17:18:03 +0200 Subject: [PATCH 3/3] update release files --- dist/dot-object.js | 57 ++++++++++++++++++++++++++++++++---------- dist/dot-object.min.js | 2 +- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/dist/dot-object.js b/dist/dot-object.js index 948e854..2d782e2 100644 --- a/dist/dot-object.js +++ b/dist/dot-object.js @@ -53,6 +53,8 @@ return path.split(sep) } + var hasOwnProperty = Object.prototype.hasOwnProperty + function DotObject(separator, override, useArray) { if (!(this instanceof DotObject)) { return new DotObject(separator, override, useArray) @@ -163,7 +165,7 @@ DotObject.prototype.str = function(path, v, obj, mod) { if (path.indexOf(this.separator) !== -1) { 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) } @@ -180,7 +182,7 @@ * @param {Object} obj * @param {Boolean} remove */ - DotObject.prototype.pick = function(path, obj, remove, reindexRemove) { + DotObject.prototype.pick = function(path, obj, remove, reindexArray) { var i var keys var val @@ -194,7 +196,7 @@ if (i === (keys.length - 1)) { if (remove) { val = obj[key] - if (reindexRemove) { + if (reindexArray && Array.isArray(obj)) { obj.splice(key, 1) } else { delete obj[key] @@ -223,27 +225,47 @@ } return obj } + /** + * + * 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) + } /** * * Remove value from an object using dot notation. * - * @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} path * @param {Object} obj - * @return {Mixed} The removed value + * @param {Boolean} reindexArray + * @return {any} The removed value */ - DotObject.prototype.remove = function(path, obj, reindexRemove) { + DotObject.prototype.remove = function(path, obj, reindexArray) { var i this.cleanup = [] if (Array.isArray(path)) { for (i = 0; i < path.length; i++) { - this.pick(path[i], obj, true, reindexRemove) + this.pick(path[i], obj, true, reindexArray) + } + if (!reindexArray) { + this._cleanup(obj) } - this._cleanup(obj) return obj } else { - return this.pick(path, obj, true, reindexRemove) + return this.pick(path, obj, true, reindexArray) } } @@ -266,7 +288,16 @@ } } - // alias method + /** + * Alias method for `dot.remove` + * + * Note: this is not an alias for dot.delete() + * + * @param {String|Array} path + * @param {Object} obj + * @param {Boolean} reindexArray + * @return {any} The removed value + */ DotObject.prototype.del = DotObject.prototype.remove /** @@ -361,7 +392,7 @@ * Set a property on an object using dot notation. * * @param {String} path - * @param {Mixed} val + * @param {any} val * @param {Object} obj * @param {Boolean} merge */ @@ -382,7 +413,7 @@ if (i === (keys.length - 1)) { if (merge && isObject(val) && isObject(obj[key])) { for (k in val) { - if (val.hasOwnProperty(k)) { + if (hasOwnProperty.call(val, k)) { obj[key][k] = val[k] } } @@ -395,7 +426,7 @@ } } else if ( // force the value to be an object - !obj.hasOwnProperty(key) || + !hasOwnProperty.call(obj, key) || (!isObject(obj[key]) && !Array.isArray(obj[key])) ) { // initialize as array if next key is numeric diff --git a/dist/dot-object.min.js b/dist/dot-object.min.js index be37b14..dec9258 100644 --- a/dist/dot-object.min.js +++ b/dist/dot-object.min.js @@ -1 +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