Skip to content

Commit

Permalink
fix: add escapeCsv() for CSV file saving
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Jul 29, 2023
1 parent f76c521 commit 17c542a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/persist/fileAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class FileAdapter implements Adapter {
gList.forEach((n) => {
n.policy.forEach((m) => {
result += n.key + ', ';
result += arrayToString(m);
result += arrayToString(m.map((element) => this.escapeCsv(element)));
result += '\n';
});
});
Expand All @@ -80,6 +80,14 @@ export class FileAdapter implements Adapter {
return true;
}

private escapeCsv(value: string): string {
// If the value contains a comma, wrap it in double quotes and escape any existing double quotes
if (value.includes(',')) {
return `"${value.replace(/"/g, '""')}"`;
}
return value;
}

private async savePolicyFile(text: string): Promise<void> {
(this.fs ? this.fs : mustGetDefaultFileSystem()).writeFileSync(this.filePath, text);
}
Expand Down

0 comments on commit 17c542a

Please sign in to comment.