Comparing version 3.0.0 to 4.0.0
87
index.js
'use strict'; | ||
var isObj = require('is-obj'); | ||
const isObj = require('is-obj'); | ||
module.exports.get = function (obj, path) { | ||
function getPathSegments(path) { | ||
const pathArr = path.split('.'); | ||
const parts = []; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
let p = pathArr[i]; | ||
while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
parts.push(p); | ||
} | ||
return parts; | ||
} | ||
module.exports.get = (obj, path, value) => { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
@@ -9,8 +27,7 @@ return obj; | ||
var pathArr = getPathSegments(path); | ||
const pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, pathArr[i]) || Object.getOwnPropertyDescriptor(Object.prototype, pathArr[i]); | ||
if (descriptor && !descriptor.enumerable) { | ||
return; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
if (!Object.prototype.propertyIsEnumerable.call(obj, pathArr[i])) { | ||
return value; | ||
} | ||
@@ -25,5 +42,5 @@ | ||
// it would return `null` if `obj` is `null` | ||
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined` not `null` | ||
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` | ||
if (i !== pathArr.length - 1) { | ||
return undefined; | ||
return value; | ||
} | ||
@@ -38,3 +55,3 @@ | ||
module.exports.set = function (obj, path, value) { | ||
module.exports.set = (obj, path, value) => { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
@@ -44,6 +61,6 @@ return; | ||
var pathArr = getPathSegments(path); | ||
const pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
const p = pathArr[i]; | ||
@@ -62,3 +79,3 @@ if (!isObj(obj[p])) { | ||
module.exports.delete = function (obj, path) { | ||
module.exports.delete = (obj, path) => { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
@@ -68,6 +85,6 @@ return; | ||
var pathArr = getPathSegments(path); | ||
const pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
var p = pathArr[i]; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
const p = pathArr[i]; | ||
@@ -80,6 +97,10 @@ if (i === pathArr.length - 1) { | ||
obj = obj[p]; | ||
if (!isObj(obj)) { | ||
return; | ||
} | ||
} | ||
}; | ||
module.exports.has = function (obj, path) { | ||
module.exports.has = (obj, path) => { | ||
if (!isObj(obj) || typeof path !== 'string') { | ||
@@ -89,8 +110,12 @@ return false; | ||
var pathArr = getPathSegments(path); | ||
const pathArr = getPathSegments(path); | ||
for (var i = 0; i < pathArr.length; i++) { | ||
obj = obj[pathArr[i]]; | ||
for (let i = 0; i < pathArr.length; i++) { | ||
if (isObj(obj)) { | ||
if (!(pathArr[i] in obj)) { | ||
return false; | ||
} | ||
if (obj === undefined) { | ||
obj = obj[pathArr[i]]; | ||
} else { | ||
return false; | ||
@@ -102,19 +127,1 @@ } | ||
}; | ||
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] === '\\' && pathArr[i + 1] !== undefined) { | ||
p = p.slice(0, -1) + '.'; | ||
p += pathArr[++i]; | ||
} | ||
parts.push(p); | ||
} | ||
return parts; | ||
} |
{ | ||
"name": "dot-prop", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Get, set, or delete a property from a nested object using a dot path", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -45,3 +45,6 @@ "scripts": { | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -25,2 +25,5 @@ # dot-prop [![Build Status](https://travis-ci.org/sindresorhus/dot-prop.svg?branch=master)](https://travis-ci.org/sindresorhus/dot-prop) | ||
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value'); | ||
//=> 'default value' | ||
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot'); | ||
@@ -39,2 +42,6 @@ //=> 'unicorn' | ||
// has | ||
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar'); | ||
//=> true | ||
// deleter | ||
@@ -55,6 +62,8 @@ const obj = {foo: {bar: 'a'}}; | ||
### get(obj, path) | ||
### get(obj, path, [value]) | ||
### set(obj, path, value) | ||
### has(obj, path) | ||
### delete(obj, path) | ||
@@ -80,3 +89,3 @@ | ||
Value to set at `path`. | ||
Value to set at `path` or optional default value to return from get. | ||
@@ -83,0 +92,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
5706
89
92