dot-prop
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -111,1 +111,24 @@ import {Get} from 'type-fest'; | ||
export function deleteProperty(object: Record<string, any>, path: string): boolean; | ||
/** | ||
Escape special characters in a path. Useful for sanitizing user input. | ||
@param path - The dot path to sanitize. | ||
@example | ||
``` | ||
import {getProperty, escapePath} from 'dot-prop'; | ||
const object = { | ||
foo: { | ||
bar: 'πΈπ» You found me Mario!', | ||
}, | ||
'foo.bar' : 'π The princess is in another castle!', | ||
}; | ||
const escapedPath = escapePath('foo.bar'); | ||
console.log(getProperty(object, escapedPath)); | ||
//=> 'π The princess is in another castle!' | ||
``` | ||
*/ | ||
export function escapePath(path: string): string; |
59
index.js
@@ -183,7 +183,7 @@ const isObject = value => { | ||
for (let i = 0; i < pathArray.length; i++) { | ||
const key = pathArray[i]; | ||
for (let index = 0; index < pathArray.length; index++) { | ||
const key = pathArray[index]; | ||
if (isStringIndex(object, key)) { | ||
object = i === pathArray.length - 1 ? undefined : null; | ||
object = index === pathArray.length - 1 ? undefined : null; | ||
} else { | ||
@@ -199,3 +199,3 @@ object = object[key]; | ||
// but we want `get({foo: null}, 'foo.bar')` to equal `undefined`, or the supplied value, not `null` | ||
if (i !== pathArray.length - 1) { | ||
if (index !== pathArray.length - 1) { | ||
return value; | ||
@@ -219,15 +219,16 @@ } | ||
for (let i = 0; i < pathArray.length; i++) { | ||
const p = pathArray[i]; | ||
assertNotStringIndex(object, p); | ||
for (let index = 0; index < pathArray.length; index++) { | ||
const key = pathArray[index]; | ||
if (!isObject(object[p])) { | ||
object[p] = Number.isInteger(pathArray[i + 1]) ? [] : {}; | ||
assertNotStringIndex(object, key); | ||
if (!isObject(object[key])) { | ||
object[key] = typeof pathArray[index + 1] === 'number' ? [] : {}; | ||
} | ||
if (i === pathArray.length - 1) { | ||
object[p] = value; | ||
if (index === pathArray.length - 1) { | ||
object[key] = value; | ||
} | ||
object = object[p]; | ||
object = object[key]; | ||
} | ||
@@ -245,12 +246,13 @@ | ||
for (let i = 0; i < pathArray.length; i++) { | ||
const p = pathArray[i]; | ||
assertNotStringIndex(object, p); | ||
for (let index = 0; index < pathArray.length; index++) { | ||
const key = pathArray[index]; | ||
if (i === pathArray.length - 1) { | ||
delete object[p]; | ||
assertNotStringIndex(object, key); | ||
if (index === pathArray.length - 1) { | ||
delete object[key]; | ||
return true; | ||
} | ||
object = object[p]; | ||
object = object[key]; | ||
@@ -273,13 +275,8 @@ if (!isObject(object)) { | ||
// eslint-disable-next-line unicorn/no-for-loop | ||
for (let i = 0; i < pathArray.length; i++) { | ||
if (isObject(object)) { | ||
if (!(pathArray[i] in object && !isStringIndex(object, pathArray[i]))) { | ||
return false; | ||
} | ||
object = object[pathArray[i]]; | ||
} else { | ||
for (const key of pathArray) { | ||
if (!isObject(object) || !(key in object) || isStringIndex(object, key)) { | ||
return false; | ||
} | ||
object = object[key]; | ||
} | ||
@@ -289,1 +286,9 @@ | ||
} | ||
export function escapePath(path) { | ||
if (typeof path !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
return path.replace(/[\\.[]/g, '\\$&'); | ||
} |
{ | ||
"name": "dot-prop", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "Get, set, or delete a property from a nested object using a dot path", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -92,2 +92,21 @@ # dot-prop | ||
### escapePath(path) | ||
Escape special characters in a path. Useful for sanitizing user input. | ||
```js | ||
import {getProperty, escapePath} from 'dot-prop'; | ||
const object = { | ||
foo: { | ||
bar: 'πΈπ» You found me Mario!', | ||
}, | ||
'foo.bar' : 'π The princess is in another castle!', | ||
}; | ||
const escapedPath = escapePath('foo.bar'); | ||
console.log(getProperty(object, escapedPath)); | ||
//=> 'π The princess is in another castle!' | ||
``` | ||
#### object | ||
@@ -94,0 +113,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14617
326
152
0