dot-prop
Advanced tools
Comparing version 7.1.1 to 7.2.0
@@ -34,3 +34,3 @@ import {Get} from 'type-fest'; | ||
defaultValue?: DefaultValue | ||
): ObjectType extends Record<string, unknown> | unknown[] ? (Get<ObjectType, PathType> extends unknown ? DefaultValue : Get<ObjectType, PathType>) : undefined; | ||
): ObjectType extends Record<string, unknown> | unknown[] ? (unknown extends Get<ObjectType, PathType> ? DefaultValue : Get<ObjectType, PathType>) : undefined; | ||
@@ -135,1 +135,28 @@ /** | ||
export function escapePath(path: string): string; | ||
/** | ||
Returns an array of every path. Plain objects are deeply recursed and are not themselves included. | ||
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal. | ||
@param object - The object to iterate through. | ||
@example | ||
``` | ||
import {getProperty, deepKeys} from 'dot-prop'; | ||
const user = { | ||
name: { | ||
first: 'Richie', | ||
last: 'Bendall', | ||
}, | ||
}; | ||
for (const property of deepKeys(user)) { | ||
console.log(`${property}: ${getProperty(user, property)}`); | ||
//=> name.first: Richie | ||
//=> name.last: Bendall | ||
} | ||
``` | ||
*/ | ||
export function deepKeys(object: unknown): string[]; |
42
index.js
@@ -287,1 +287,43 @@ const isObject = value => { | ||
} | ||
// The keys returned by Object.entries() for arrays are strings | ||
function entries(value) { | ||
if (Array.isArray(value)) { | ||
return value.map((value, index) => [index, value]); | ||
} | ||
return Object.entries(value); | ||
} | ||
function stringifyPath(pathSegments) { | ||
let result = ''; | ||
for (let [index, segment] of entries(pathSegments)) { | ||
if (typeof segment === 'number') { | ||
result += `[${segment}]`; | ||
} else { | ||
segment = escapePath(segment); | ||
result += index === 0 ? segment : `.${segment}`; | ||
} | ||
} | ||
return result; | ||
} | ||
function * deepKeysIterator(object, currentPath = []) { | ||
if (!isObject(object)) { | ||
if (currentPath.length > 0) { | ||
yield stringifyPath(currentPath); | ||
} | ||
return; | ||
} | ||
for (const [key, value] of entries(object)) { | ||
yield * deepKeysIterator(value, [...currentPath, key]); | ||
} | ||
} | ||
export function deepKeys(object) { | ||
return [...deepKeysIterator(object)]; | ||
} |
{ | ||
"name": "dot-prop", | ||
"version": "7.1.1", | ||
"version": "7.2.0", | ||
"description": "Get, set, or delete a property from a nested object using a dot path", | ||
@@ -19,3 +19,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava && tsd", | ||
"test": "xo && ava && tsc", | ||
"bench": "node benchmark.js" | ||
@@ -41,3 +41,3 @@ }, | ||
"dependencies": { | ||
"type-fest": "^2.10.0" | ||
"type-fest": "^2.11.2" | ||
}, | ||
@@ -47,5 +47,6 @@ "devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"tsd": "^0.19.1", | ||
"xo": "^0.47.0" | ||
"expect-type": "^0.13.0", | ||
"typescript": "^4.5.5", | ||
"xo": "^0.48.0" | ||
} | ||
} |
@@ -111,2 +111,25 @@ # dot-prop | ||
### deepKeys(object) | ||
Returns an array of every path. Plain objects are deeply recursed and are not themselves included. | ||
This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal. | ||
```js | ||
import {getProperty, deepKeys} from 'dot-prop'; | ||
const user = { | ||
name: { | ||
first: 'Richie', | ||
last: 'Bendall', | ||
}, | ||
}; | ||
for (const property of deepKeys(user)) { | ||
console.log(`${property}: ${getProperty(user, property)}`); | ||
//=> name.first: Richie | ||
//=> name.last: Bendall | ||
} | ||
``` | ||
#### object | ||
@@ -113,0 +136,0 @@ |
16680
379
175
5
Updatedtype-fest@^2.11.2