Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deepKeys() #94

Merged
merged 8 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,28 @@ console.log(getProperty(object, escapedPath));
```
*/
export function escapePath(path: string): string;

/**
Returns an array that contains every path in `object`. Plain objects are iterated deeper and are not themselves yielded. 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[];
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,21 @@ export function escapePath(path) {

return path.replace(/[\\.[]/g, '\\$&');
}

function * deepKeysIterator(object, currentPath = []) {
if (!isObject(object) || Array.isArray(object)) {
if (currentPath.length > 0) {
yield currentPath.join('.');
}

return;
}

for (const [key, value] of Object.entries(object)) {
yield * deepKeysIterator(value, [...currentPath, escapePath(key)]);
}
}

export function deepKeys(object) {
Richienb marked this conversation as resolved.
Show resolved Hide resolved
return [...deepKeysIterator(object)];
}
4 changes: 3 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType, expectAssignable} from 'tsd';
import {getProperty, setProperty, hasProperty, deleteProperty} from './index.js';
import {getProperty, setProperty, hasProperty, deleteProperty, deepKeys} from './index.js';

expectType<string>(getProperty({foo: {bar: 'unicorn'}}, 'foo.bar'));
expectType<undefined>(getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep'));
Expand All @@ -16,3 +16,5 @@ expectType<typeof object>(setProperty(object, 'foo.bar', 'b'));
expectType<boolean>(hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar'));

expectType<boolean>(deleteProperty({foo: {bar: 'a'}}, 'foo.bar'));

expectType<string[]>(deepKeys({foo: {bar: 'a'}}));
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ console.log(getProperty(object, escapedPath));
//=> '🍄 The princess is in another castle!'
```

### deepKeys(object)

Returns an array that contains every path in `object`. Plain objects are iterated deeper and are not themselves yielded. Useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.
Richienb marked this conversation as resolved.
Show resolved Hide resolved

```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

Type: `object | array`
Expand Down
25 changes: 24 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import {getProperty, setProperty, hasProperty, deleteProperty, escapePath} from './index.js';
import {getProperty, setProperty, hasProperty, deleteProperty, escapePath, deepKeys} from './index.js';

test('getProperty', t => {
const fixture1 = {foo: {bar: 1}};
Expand Down Expand Up @@ -407,6 +407,29 @@ test('escapePath', t => {
});
});

test('deepKeys', t => {
t.deepEqual(deepKeys({
'a.b': {
c: {
d: [1, 2, 3],
e: '🦄',
f: 0,
},
'': {
a: 0,
},
},
}), [
'a\\.b.c.d',
Richienb marked this conversation as resolved.
Show resolved Hide resolved
'a\\.b.c.e',
'a\\.b.c.f',
'a\\.b..a',
]);
Richienb marked this conversation as resolved.
Show resolved Hide resolved

t.deepEqual(deepKeys([]), []);
t.deepEqual(deepKeys(0), []);
});

test('prevent setting/getting `__proto__`', t => {
setProperty({}, '__proto__.unicorn', '🦄');
t.not({}.unicorn, '🦄'); // eslint-disable-line no-use-extend-native/no-use-extend-native
Expand Down