Skip to content

Commit e18d66c

Browse files
committed
fix: resolve potential excludeKeys match to a key path other than at the start of a string
In a case where `excludeKeys` is set to `["arr"]`, there was a possibility that with JSON data looking like this: ``` [ { "id": 1, "arr": [ { "name": "foo" } ], "name": { "arr": "this should appear" } }, { "id": 2, "arr": [ { "name": "bar" } ], "name": { "arr": "this should also appear" } } ] ``` could potentially result in both `arr.name` and `name.arr` being excluded. By adjusting the RegExp thats being used for the matching, this fixes a potential bug that caused `name.arr` to be mistakenly excluded.
1 parent f9905f8 commit e18d66c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/json2csv.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
8383
if (options.excludeKeys) {
8484
return keyPaths.filter((keyPath) => {
8585
for (const excludedKey of options.excludeKeys) {
86-
const regex = new RegExp(excludedKey);
86+
// 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
87+
const regex = new RegExp(`^${excludedKey}`);
88+
8789
if (excludedKey === keyPath || keyPath.match(regex)) {
8890
return false; // Exclude the key
8991
}

0 commit comments

Comments
 (0)