Comparing version 2.2.0 to 2.3.0
66
index.js
'use strict'; | ||
var isObj = require('is-obj'); | ||
function isObjOrFn(x) { | ||
return (typeof x === 'object' || typeof x === 'function') && x !== null; | ||
} | ||
module.exports.get = function (obj, path) { | ||
if (!isObjOrFn(obj) || typeof path !== 'string') { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
return obj; | ||
} | ||
var pathArr = path.split('.'); | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
obj = obj[pathArr[i]]; | ||
while (p[p.length - 1] === '\\') { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
obj = obj[p]; | ||
if (obj === undefined) { | ||
@@ -33,7 +23,7 @@ break; | ||
module.exports.set = function (obj, path, value) { | ||
if (!isObjOrFn(obj) || typeof path !== 'string') { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
return; | ||
} | ||
var pathArr = path.split('.'); | ||
var pathArr = getPathSegments(path); | ||
@@ -43,8 +33,3 @@ for (var i = 0; i < pathArr.length; i++) { | ||
while (p[p.length - 1] === '\\') { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
if (!isObjOrFn(obj[p])) { | ||
if (!isObj(obj[p])) { | ||
obj[p] = {}; | ||
@@ -60,1 +45,38 @@ } | ||
}; | ||
module.exports.delete = function (obj, path) { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
return; | ||
} | ||
var pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
if (i === pathArr.length - 1) { | ||
delete obj[p]; | ||
return; | ||
} | ||
obj = obj[p]; | ||
} | ||
}; | ||
function getPathSegments(path) { | ||
var pathArr = path.split('.'); | ||
var parts = []; | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
while (p[p.length - 1] === '\\') { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
parts.push(p); | ||
} | ||
return parts; | ||
} |
{ | ||
"name": "dot-prop", | ||
"version": "2.2.0", | ||
"description": "Get or set a property from a nested object using a dot path", | ||
"version": "2.3.0", | ||
"description": "Get, set, or delete a property from a nested object using a dot path", | ||
"license": "MIT", | ||
@@ -16,3 +16,3 @@ "repository": "sindresorhus/dot-prop", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "ava && xo" | ||
}, | ||
@@ -30,2 +30,5 @@ "files": [ | ||
"get", | ||
"set", | ||
"delete", | ||
"del", | ||
"access", | ||
@@ -35,5 +38,9 @@ "notation", | ||
], | ||
"dependencies": { | ||
"is-obj": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.0.4" | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
# dot-prop [![Build Status](https://travis-ci.org/sindresorhus/dot-prop.svg?branch=master)](https://travis-ci.org/sindresorhus/dot-prop) | ||
> Get or set a property from a nested object using a dot path | ||
> Get, set, or delete a property from a nested object using a dot path | ||
@@ -16,3 +16,3 @@ | ||
```js | ||
var dotProp = require('dot-prop'); | ||
const dotProp = require('dot-prop'); | ||
@@ -30,3 +30,3 @@ // getter | ||
// setter | ||
var obj = {foo: {bar: 'a'}}; | ||
const obj = {foo: {bar: 'a'}}; | ||
dotProp.set(obj, 'foo.bar', 'b'); | ||
@@ -40,9 +40,44 @@ console.log(obj); | ||
dotProp.set(obj, 'foo.dot\\.dot', 'unicorn'); | ||
// deleter | ||
const obj = {foo: {bar: 'a'}}; | ||
dotProp.del(obj, 'foo.bar'); | ||
console.log(obj); | ||
//=> {foo: {bar: 'b', baz: 'x', 'dot.dot': 'unicorn'}} | ||
//=> {foo: {}} | ||
obj.foo.bar = {x: 'y', y: 'x'}; | ||
dotProp.del(obj, 'foo.bar.x'); | ||
console.log(obj); | ||
//=> {foo: {bar: {y: 'x'}}} | ||
``` | ||
## API | ||
### get(obj, path) | ||
### set(obj, path, value) | ||
### delete(obj, path) | ||
#### obj | ||
Type: `object` | ||
Object to get, set, or delete the `path` value. | ||
#### path | ||
Type: `string` | ||
Path of the property in the object. Use `.` for nested objects or `\\.` to add a `.` in a key. | ||
#### value | ||
Type: `any` | ||
Value to set at `path`. | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
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
4454
58
81
1
2
+ Addedis-obj@^1.0.0
+ Addedis-obj@1.0.1(transitive)