Skip to content

Commit

Permalink
fix: resolve potential excludeKeys match to a key path other than at …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
mrodrig committed Feb 21, 2024
1 parent f9905f8 commit e18d66c
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/json2csv.ts
Expand Up @@ -83,7 +83,9 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
if (options.excludeKeys) {
return keyPaths.filter((keyPath) => {
for (const excludedKey of options.excludeKeys) {
const regex = new RegExp(excludedKey);
// 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}`);

if (excludedKey === keyPath || keyPath.match(regex)) {
return false; // Exclude the key
}
Expand Down

0 comments on commit e18d66c

Please sign in to comment.