Skip to content

Commit

Permalink
refactor: shift to a wildcardMatch option on the keys list type to im…
Browse files Browse the repository at this point in the history
…prove behavior and allow improved control over matched keys
  • Loading branch information
mrodrig committed Feb 23, 2024
1 parent 46db726 commit 14cb00e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,40 +172,51 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
});
}

function extractWildcardMatchKeys() {
if (!options.keys) return [];

return options.keys.flatMap(item => {
if (typeof item === 'string') {
// Exclude plain strings that were passed in options.keys
return [];
} else if (item?.wildcardMatch) {
// Return "field" value for objects with wildcardMatch: true
return item.field;
}
// Exclude other objects
return [];
});
}

/**
* Retrieve the headings for all documents and return it.
* This checks that all documents have the same schema.
*/
function retrieveHeaderFields(data: object[]) {
const wildcardMatchKeys = extractWildcardMatchKeys();
const keyStrings = convertKeysToHeaderFields();
const fieldNames = getFieldNameList(data);
const processed = processSchemas(fieldNames);

if (options.keys) {
options.keys = keyStrings;

const detectedKeysMatchingProvidedKeys = keyStrings.flatMap((userProvidedKey) => {
const matchedKeys = keyStrings.flatMap((userProvidedKey) => {
// If this is not a wildcard matched key, then just return and include it in the resulting key list
if (!wildcardMatchKeys.includes(userProvidedKey)) {
return userProvidedKey;
}

// Otherwise, identify all detected keys that match with the provided wildcard key:
const matches = [];
const regex = new RegExp(`^${userProvidedKey}`);
const matchedKeys = [];


for (const detectedKey of processed) {
if (userProvidedKey === detectedKey || detectedKey.match(regex)) {
matchedKeys.push(detectedKey);
matches.push(detectedKey);
}
}
return matchedKeys;
});

const matchedKeys = Array.from(new Set(detectedKeysMatchingProvidedKeys));

keyStrings.forEach((providedKey) => {
const regex = new RegExp(`^${providedKey}`);
const anyMatch = utils.anyKeysMatch(matchedKeys, regex);

if (!anyMatch) {
matchedKeys.push(providedKey);
}

return matches;
});

if (!options.unwindArrays) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface DelimiterOptions {
export type KeysList = (string | {
field: string;
title?: string;
wildcardMatch?: boolean;
})[];

interface SharedConverterOptions {
Expand Down

0 comments on commit 14cb00e

Please sign in to comment.