Skip to content

Commit

Permalink
Merge branch 'release/1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalff committed Jul 18, 2019
2 parents 56f3f39 + ddb5aa9 commit 1130f30
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 44 additions & 13 deletions dist/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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<String>} 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)
}
}

Expand All @@ -266,7 +288,16 @@
}
}

// 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

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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]
}
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dist/dot-object.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>} 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
Expand All @@ -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)
Expand All @@ -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<String>} path
* @param {Object} obj
* @param {Boolean} reindexArray
* @return {any} The removed value
*/
DotObject.prototype.del = DotObject.prototype.remove

/**
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
39 changes: 34 additions & 5 deletions src/dot-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>} 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
Expand All @@ -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)
Expand All @@ -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<String>} path
* @param {Object} obj
* @param {Boolean} reindexArray
* @return {any} The removed value
*/
DotObject.prototype.del = DotObject.prototype.remove

/**
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 1130f30

Please sign in to comment.