Socket
Socket
Sign inDemoInstall

dot-prop

Package Overview
Dependencies
1
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.1.1 to 7.2.0

29

index.d.ts

@@ -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[];

@@ -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)];
}

11

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc