Skip to content

Commit acc74f2

Browse files
authored
Merge pull request #33 from mrodrig/feat/add-arrayIndexesAsKeys
Add arrayIndexesAsKeys option
2 parents f00347b + a7f32e1 commit acc74f2

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ The array of keys that is returned can then be used with the
4848
at a specific key path.
4949

5050
Options (optional):
51+
- arrayIndexesAsKeys - `Boolean` (Default `false`) - Should array indexes be used as keys in the generated path?
52+
- Example:
53+
```json
54+
[
55+
{
56+
"list": [
57+
{
58+
"a": 1
59+
},
60+
{
61+
"a": 2
62+
}
63+
]
64+
}
65+
]
66+
```
67+
- arrayIndexesAsKeys = `false` results in: `['list.a']`
68+
- arrayIndexesAsKeys = `true` results in: `['list.0.a', 'list.1.a']`
5169
- expandNestedObjects - `Boolean` (Default: `true`) - Should nested objects appearing in the provided object also be expanded, such asthat keys appearing in those objects are extracted and included in the returned key path list?
5270
- Example:
5371
```json

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deeks",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"description": "Retrieve all keys and nested keys from objects and arrays of objects.",
55
"main": "lib/deeks.js",
66
"types": "lib/deeks.d.ts",

src/deeks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function generateDeepKeysList(heading: string, data: Record<string, unknown>, op
4242
const keyName = buildKeyName(heading, escapeNestedDotsIfSpecified(currentKey, options));
4343

4444
// If we have another nested document, recur on the sub-document to retrieve the full key name
45-
if (options.expandNestedObjects && utils.isDocumentToRecurOn(data[currentKey])) {
45+
if (options.expandNestedObjects && utils.isDocumentToRecurOn(data[currentKey]) || (options.arrayIndexesAsKeys && Array.isArray(data[currentKey]) && (data[currentKey] as unknown[]).length)) {
4646
return generateDeepKeysList(keyName, data[currentKey] as Record<string, unknown>, options);
4747
} else if (options.expandArrayObjects && Array.isArray(data[currentKey])) {
4848
// If we have a nested array that we need to recur on
@@ -107,6 +107,7 @@ function buildKeyName(upperKeyName: string, currentKeyName: string) {
107107

108108
function mergeOptions(options: DeeksOptions | undefined): DeeksOptions {
109109
return {
110+
arrayIndexesAsKeys: false,
110111
expandNestedObjects: true,
111112
expandArrayObjects: false,
112113
ignoreEmptyArraysWhenExpanding: false,

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22

33
export interface DeeksOptions {
4+
/** @default false */
5+
arrayIndexesAsKeys?: boolean,
6+
47
/** @default true */
58
expandNestedObjects?: boolean,
69

test/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,25 @@ describe('deeks Module', () => {
401401
assert.deepEqual(keys, ['a.c.f']);
402402
done();
403403
});
404+
405+
it('[arrayIndexesAsKeys] should properly use array indexes as keys when specified', (done) => {
406+
const testObj = {
407+
list: [{
408+
a: 1
409+
}, {
410+
a: 2
411+
}]
412+
},
413+
options = {
414+
arrayIndexesAsKeys: true
415+
},
416+
keys = deepKeys(testObj, options);
417+
418+
assert.equal(Array.isArray(keys), true);
419+
assert.equal(keys.length, 2);
420+
assert.deepEqual(keys, ['list.0.a', 'list.1.a']);
421+
done();
422+
});
404423
});
405424
});
406425

@@ -883,6 +902,25 @@ describe('deeks Module', () => {
883902
assert.deepEqual(keys, [ ['a.c.f'] ]);
884903
done();
885904
});
905+
906+
it('[arrayIndexesAsKeys] should properly use array indexes as keys when specified', (done) => {
907+
const testList = [{
908+
list: [{
909+
a: 1
910+
}, {
911+
a: 2
912+
}]
913+
}],
914+
options = {
915+
arrayIndexesAsKeys: true
916+
},
917+
keys = deepKeysFromList(testList, options);
918+
919+
assert.equal(Array.isArray(keys), true);
920+
assert.equal(keys.length, 1);
921+
assert.deepEqual(keys, [ ['list.0.a', 'list.1.a'] ]);
922+
done();
923+
});
886924
});
887925
});
888926
});

0 commit comments

Comments
 (0)