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

fix(utilities): fixed getDeepObjectKeys not returning proper values for arrays of not-objects #428

Merged
merged 5 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
72 changes: 43 additions & 29 deletions packages/utilities/src/lib/getDeepObjectKeys.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
import { isObject } from './isObject';
import { isNullOrUndefinedOrEmpty } from './isNullOrUndefinedOrEmpty';
import type { AnyObject } from './utilityTypes';

/**
* Flattens an object to a list of its keys, traversing deeply into nested objects and arrays of objects.
*
* @note By default Nested array values are flattened to `arrayKey.${index}.subKey`. This can be changed to `arrayKey[${index}].subKey` by setting `arrayKeysBracedIndex` to `true`.
* @note By default Nested array values are flattened to `arrayKey.${index}.subKey`.
* This can be changed to `arrayKey[${index}].subKey` by setting `options.arrayKeysIndexStyle` to `'braces-with-dot'`.
* Or it can also be changed to `arrayKey[${index}]subKey` by setting `options.arrayKeysIndexStyle` to `'braces'`.
*
* @param obj The object of which to deeply retrieve its keys
* @param options The options with which to customize the output of this function
* @returns An array of strings holding the keys of the object
*/
export function getDeepObjectKeys<T>(
export function getDeepObjectKeys<T>(obj: AnyObject<T>, options?: GetDeepObjectKeysOptions): string[] {
return [...getDeepObjectKeysGenerator(obj, options)];
}

function* getDeepObjectKeysGenerator<T>(
obj: AnyObject<T>,
{ arrayKeysIndexStyle = 'dotted' }: GetDeepObjectKeysOptions = { arrayKeysIndexStyle: 'dotted' }
): string[] {
const keys: string[] = [];
): Generator<string> {
if (Array.isArray(obj)) {
for (const [index, value] of obj.entries()) {
const resolvedIndex = arrayKeysIndexStyle === 'dotted' ? `${index}` : arrayKeysIndexStyle === 'braces' ? `[${index}]` : `[${index}].`;
yield* getDeepObjectKeysRecursive(value, resolvedIndex, { arrayKeysIndexStyle });
}
} else {
for (const [key, value] of Object.entries(obj)) {
yield* getDeepObjectKeysRecursive(value, `${key}`, { arrayKeysIndexStyle });
}
}
}

for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value)) {
for (const [index, innerValue] of value.entries()) {
const arraySubKeys = getDeepObjectKeys(innerValue);
keys.push(
...arraySubKeys.map((arraySubKey) => {
switch (arrayKeysIndexStyle) {
case 'braces-with-dot':
return `${key}[${index}].${arraySubKey}`;
case 'braces':
return `${key}[${index}]${arraySubKey}`;
case 'dotted':
default:
return `${key}.${index}.${arraySubKey}`;
}
})
);
}
} else if (isObject(value)) {
const objectSubKeys = getDeepObjectKeys(value);
keys.push(...objectSubKeys.map((subKey) => `${key}.${subKey}`));
function* getDeepObjectKeysRecursive(obj: unknown, prefix: string, { arrayKeysIndexStyle }: GetDeepObjectKeysOptions): Generator<string> {
if (typeof obj !== 'object' || obj === null) {
yield prefix;
return;
}

if (Array.isArray(obj)) {
for (const [index, value] of obj.entries()) {
const resolvedPrefixedIndex = arrayKeysIndexStyle === 'dotted' ? `${prefix}.${index}` : `${prefix}[${index}]`;

yield* getDeepObjectKeysRecursive(value, resolvedPrefixedIndex, { arrayKeysIndexStyle });
}
} else {
const objectEntries = Object.entries(obj);
if (isNullOrUndefinedOrEmpty(objectEntries) && prefix) {
yield prefix;
} else {
keys.push(key);
for (const [key, value] of objectEntries) {
yield* getDeepObjectKeysRecursive(value, arrayKeysIndexStyle === 'braces' ? `${prefix}${key}` : `${prefix}.${key}`, {
arrayKeysIndexStyle
});
}
}
}

return keys;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/tests/getDeepObjectKeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('getDeepObjectKeys', () => {
[{ a: 1, b: 2 }, {}, ['a', 'b']],
[{ a: [[]] }, {}, []],
[{ a: [[{ b: 0 }]] }, {}, ['a.0.0.b']],
[{ id: { number: '123', dates: [new Date('12-12-2022'), new Date()] } }, {}, ['id.number', 'id.dates.0', 'id.dates.1']],
[{ a: { b: 1, c: 2 }, d: 3 }, {}, ['a.b', 'a.c', 'd']],
[{ a: [{ b: 1, c: 2 }, { d: 3 }] }, {}, ['a.0.b', 'a.0.c', 'a.1.d']],
[{ a: [{ b: 1, c: 2 }, { d: 3 }] }, { arrayKeysIndexStyle: 'braces' }, ['a[0]b', 'a[0]c', 'a[1]d']],
Expand Down