Skip to content

Commit

Permalink
feat: add arrayIndexesAsKeys option
Browse files Browse the repository at this point in the history
Added to work on support for use case described in mrodrig/json-2-csv#207
  • Loading branch information
mrodrig committed Feb 24, 2024
1 parent f00347b commit 355d240
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/deeks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ function generateDeepKeysList(heading: string, data: Record<string, unknown>, op
// If the given heading is empty, then we set the heading to be the subKey, otherwise set it as a nested heading w/ a dot
const keyName = buildKeyName(heading, escapeNestedDotsIfSpecified(currentKey, options));

console.log(`keyName=${keyName}\theading=${heading}\tcurrentKey=${currentKey}`);

// If we have another nested document, recur on the sub-document to retrieve the full key name
if (options.expandNestedObjects && utils.isDocumentToRecurOn(data[currentKey])) {
if (options.expandNestedObjects && utils.isDocumentToRecurOn(data[currentKey]) || (options.arrayIndexesAsKeys && Array.isArray(data[currentKey]) && (data[currentKey] as any).length)) {

Check warning on line 47 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Unexpected any. Specify a different type

Check warning on line 47 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Unexpected any. Specify a different type

Check warning on line 47 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 21)

Unexpected any. Specify a different type
console.log(' case 1');
return generateDeepKeysList(keyName, data[currentKey] as Record<string, unknown>, options);
} else if (options.expandArrayObjects && Array.isArray(data[currentKey])) {
console.log(' case 2');
console.log(' Object.keys', Object.keys(data[currentKey] as any));

Check warning on line 52 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Unexpected any. Specify a different type

Check warning on line 52 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Unexpected any. Specify a different type

Check warning on line 52 in src/deeks.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 21)

Unexpected any. Specify a different type
// If we have a nested array that we need to recur on
return processArrayKeys(data[currentKey] as object[], keyName, options);
} else if (options.ignoreEmptyArrays && Array.isArray(data[currentKey]) && !(data[currentKey] as unknown[]).length) {
console.log(' case 3');
return [];
}
console.log(' default');
// Otherwise return this key name since we don't have a sub document
return keyName;
});
Expand Down Expand Up @@ -107,6 +114,7 @@ function buildKeyName(upperKeyName: string, currentKeyName: string) {

function mergeOptions(options: DeeksOptions | undefined): DeeksOptions {
return {
arrayIndexesAsKeys: false,
expandNestedObjects: true,
expandArrayObjects: false,
ignoreEmptyArraysWhenExpanding: false,
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

export interface DeeksOptions {
/** @default false */
arrayIndexesAsKeys?: boolean,

/** @default true */
expandNestedObjects?: boolean,

Expand Down

0 comments on commit 355d240

Please sign in to comment.