delete(obj, path) { if (!isObj(obj) || typeof path !== 'string') { return; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { const p = pathArr[i]; if (i === pathArr.length - 1) { delete obj[p]; return; } obj = obj[p]; if (!isObj(obj)) { return; } } }
...
// has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true
// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}
obj.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(obj, 'foo.bar.x');
console.log(obj);
//=> {foo: {bar: {y: 'x'}}}
...
get(obj, path, value) { if (!isObj(obj) || typeof path !== 'string') { return value === undefined ? obj : value; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) { return value; } obj = obj[pathArr[i]]; if (obj === undefined || obj === null) { // `obj` is either `undefined` or `null` so we want to stop the loop, and // if this is not the last bit of the path, and // if it did't return `undefined` // it would return `null` if `obj` is `null` // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` if (i !== pathArr.length - 1) { return value; } break; } } return obj; }
...
## Usage
```js
const dotProp = require('dot-prop');
// getter
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
//=> undefined
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
//=> 'default value'
...
has(obj, path) { if (!isObj(obj) || typeof path !== 'string') { return false; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { if (isObj(obj)) { if (!(pathArr[i] in obj)) { return false; } obj = obj[pathArr[i]]; } else { return false; } } return true; }
...
//=> {foo: {bar: 'b'}}
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
// has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true
// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}
...
set(obj, path, value) { if (!isObj(obj) || typeof path !== 'string') { return; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { const p = pathArr[i]; if (!isObj(obj[p])) { obj[p] = {}; } if (i === pathArr.length - 1) { obj[p] = value; } obj = obj[p]; } }
...
//=> 'default value'
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
//=> 'unicorn'
// setter
const obj = {foo: {bar: 'a'}};
dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
...
delete(obj, path) { if (!isObj(obj) || typeof path !== 'string') { return; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { const p = pathArr[i]; if (i === pathArr.length - 1) { delete obj[p]; return; } obj = obj[p]; if (!isObj(obj)) { return; } } }
...
// has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true
// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}
obj.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(obj, 'foo.bar.x');
console.log(obj);
//=> {foo: {bar: {y: 'x'}}}
...
get(obj, path, value) { if (!isObj(obj) || typeof path !== 'string') { return value === undefined ? obj : value; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) { return value; } obj = obj[pathArr[i]]; if (obj === undefined || obj === null) { // `obj` is either `undefined` or `null` so we want to stop the loop, and // if this is not the last bit of the path, and // if it did't return `undefined` // it would return `null` if `obj` is `null` // but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` if (i !== pathArr.length - 1) { return value; } break; } } return obj; }
...
## Usage
```js
const dotProp = require('dot-prop');
// getter
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
//=> undefined
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
//=> 'default value'
...
has(obj, path) { if (!isObj(obj) || typeof path !== 'string') { return false; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { if (isObj(obj)) { if (!(pathArr[i] in obj)) { return false; } obj = obj[pathArr[i]]; } else { return false; } } return true; }
...
//=> {foo: {bar: 'b'}}
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
// has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true
// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}
...
set(obj, path, value) { if (!isObj(obj) || typeof path !== 'string') { return; } const pathArr = getPathSegments(path); for (let i = 0; i < pathArr.length; i++) { const p = pathArr[i]; if (!isObj(obj[p])) { obj[p] = {}; } if (i === pathArr.length - 1) { obj[p] = value; } obj = obj[p]; } }
...
//=> 'default value'
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
//=> 'unicorn'
// setter
const obj = {foo: {bar: 'a'}};
dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
...