Skip to content

Commit 523f625

Browse files
authored
Merge pull request #253 from mrodrig/feat/excludeKeys-regexp-support
RegExp support for excludeKeys
2 parents cbe0d6e + e8b5170 commit 523f625

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.
8787
```
8888
* Note: This may result in CSV output that does not map back exactly to the original JSON.
8989
* `excelBOM` - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
90-
* `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`.
90+
* `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).
9191
* Default: `[]`
9292
* Note: When used with `unwindArrays`, arrays present at excluded key paths will not be unwound.
9393
* `expandNestedObjects` - Boolean - Should nested objects be deep-converted to CSV?

src/json2csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
8585
return keyPaths.filter((keyPath) => {
8686
for (const excludedKey of options.excludeKeys) {
8787
// 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
88-
const regex = new RegExp(`^${excludedKey}`);
88+
const regex = excludedKey instanceof RegExp ? excludedKey : new RegExp(`^${excludedKey}`);
8989

9090
if (excludedKey === keyPath || keyPath.match(regex)) {
9191
return false; // Exclude the key

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface Json2CsvOptions extends SharedConverterOptions {
155155
/**
156156
* Specify the keys that should be excluded from the output.
157157
*/
158-
excludeKeys?: string[];
158+
excludeKeys?: (string | RegExp)[];
159159

160160
/**
161161
* 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`.

test/json2csv.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,26 @@ export function runTests() {
578578
assert.equal(csv, updatedCsv);
579579
});
580580

581+
// Test case for #252
582+
it('should exclude a single key that matches a provided excludeKeys RegExp', () => {
583+
const updatedCsv = csvTestData.wildcardMatch.replace(',baz.array', ',baz.b').replace(',c', ',b');
584+
585+
const csv = json2csv(jsonTestData.wildcardMatch, {
586+
excludeKeys: [/array/],
587+
});
588+
assert.equal(csv, updatedCsv);
589+
});
590+
591+
// Test case for #252
592+
it('should exclude multiple keys that match a provided excludeKeys RegExp', () => {
593+
const updatedCsv = csvTestData.wildcardMatch.replace(',baz.a,baz.array', '').replace(',a,c', '');
594+
595+
const csv = json2csv(jsonTestData.wildcardMatch, {
596+
excludeKeys: [/baz/],
597+
});
598+
assert.equal(csv, updatedCsv);
599+
});
600+
581601
// Test case for #207
582602
it('should include the array indexes in CSV key headers if specified via the option', () => {
583603
const csv = json2csv(jsonTestData.arrayIndexesAsKeys, {

0 commit comments

Comments
 (0)