Skip to content

Commit

Permalink
Merge e8b5170 into cbe0d6e
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodrig committed Mar 1, 2024
2 parents cbe0d6e + e8b5170 commit 73901a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -87,7 +87,7 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.
```
* Note: This may result in CSV output that does not map back exactly to the original JSON.
* `excelBOM` - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
* `excludeKeys` - Array - Specify the keys that should be excluded from the output. Provided keys will also be used as a RegExp to help exclude keys under a specified prefix, such as all keys of Objects in an Array when `expandArrayObjects` is `true`.
* `excludeKeys` - Array - Specify the `string` keys or `RegExp` patterns that should be excluded from the output. Provided `string` keys will also be used as a RegExp to help exclude keys under a specified prefix, such as all keys of Objects in an Array when `expandArrayObjects` is `true` (e.g., providing `'baz'` will exclude `'baz.a'` too).
* Default: `[]`
* Note: When used with `unwindArrays`, arrays present at excluded key paths will not be unwound.
* `expandNestedObjects` - Boolean - Should nested objects be deep-converted to CSV?
Expand Down
2 changes: 1 addition & 1 deletion src/json2csv.ts
Expand Up @@ -85,7 +85,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
return keyPaths.filter((keyPath) => {
for (const excludedKey of options.excludeKeys) {
// Only match if the excludedKey appears at the beginning of the string so we don't accidentally match a key farther down in a key path
const regex = new RegExp(`^${excludedKey}`);
const regex = excludedKey instanceof RegExp ? excludedKey : new RegExp(`^${excludedKey}`);

if (excludedKey === keyPath || keyPath.match(regex)) {
return false; // Exclude the key
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -155,7 +155,7 @@ export interface Json2CsvOptions extends SharedConverterOptions {
/**
* Specify the keys that should be excluded from the output.
*/
excludeKeys?: string[];
excludeKeys?: (string | RegExp)[];

/**
* Specify how values should be converted into CSV format. This function is provided a single field value at a time and must return a `String`.
Expand Down
20 changes: 20 additions & 0 deletions test/json2csv.ts
Expand Up @@ -578,6 +578,26 @@ export function runTests() {
assert.equal(csv, updatedCsv);
});

// Test case for #252
it('should exclude a single key that matches a provided excludeKeys RegExp', () => {
const updatedCsv = csvTestData.wildcardMatch.replace(',baz.array', ',baz.b').replace(',c', ',b');

const csv = json2csv(jsonTestData.wildcardMatch, {
excludeKeys: [/array/],
});
assert.equal(csv, updatedCsv);
});

// Test case for #252
it('should exclude multiple keys that match a provided excludeKeys RegExp', () => {
const updatedCsv = csvTestData.wildcardMatch.replace(',baz.a,baz.array', '').replace(',a,c', '');

const csv = json2csv(jsonTestData.wildcardMatch, {
excludeKeys: [/baz/],
});
assert.equal(csv, updatedCsv);
});

// Test case for #207
it('should include the array indexes in CSV key headers if specified via the option', () => {
const csv = json2csv(jsonTestData.arrayIndexesAsKeys, {
Expand Down

0 comments on commit 73901a4

Please sign in to comment.