Skip to content

Commit

Permalink
NNS1-3480: allows "+" and "-" symbols in Csv (#5927)
Browse files Browse the repository at this point in the history
# Motivation

For the transactions report, displaying the symbols "+" and "-" helps
identify the type of operation (incoming or outgoing).

# Changes

- Changes `escapeCsvValue` to allow `+` and `-`

# Tests

- Updated tests

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary
  • Loading branch information
yhabib authored Dec 5, 2024
1 parent 0852489 commit 14c7679
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frontend/src/lib/utils/export-to-csv.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const escapeCsvValue = (value: unknown): string => {

let stringValue = String(value);

const patternForSpecialCharacters = /[",\r\n=+-@|]/;
const patternForSpecialCharacters = /[",\r\n=@|]/;
if (!patternForSpecialCharacters.test(stringValue)) {
return stringValue;
}

const formulaInjectionCharacters = "=+-@|";
const formulaInjectionCharacters = "=@|";
const characterToBreakFormula = "'";
if (formulaInjectionCharacters.includes(stringValue[0])) {
stringValue = `${characterToBreakFormula}${stringValue}`;
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/tests/lib/utils/export-to-csv.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ describe("Export to Csv", () => {
it("should prevent formula injection by prefixing with single quote", () => {
const data: TestFormulaData[] = [
{ formula: "=SUM(A1:A10)", value: 100 },
{ formula: "+1234567", value: 200 },
{ formula: "-1234567", value: 300 },
{ formula: "@SUM(A1)", value: 400 },
{ formula: "|MACRO", value: 500 },
];
Expand All @@ -91,7 +89,7 @@ describe("Export to Csv", () => {
{ id: "value", label: "value" },
];
const expected =
"formula,value\n'=SUM(A1:A10),100\n'+1234567,200\n'-1234567,300\n'@SUM(A1),400\n'|MACRO,500";
"formula,value\n'=SUM(A1:A10),100\n'@SUM(A1),400\n'|MACRO,500";
expect(convertToCsv({ data, headers })).toBe(expected);
});

Expand All @@ -104,7 +102,7 @@ describe("Export to Csv", () => {
{ id: "formula", label: "formula" },
{ id: "value", label: "value" },
];
const expected = "formula,value\n'=SUM(A1:A10),100\n\"'+1234567,12\",200";
const expected = 'formula,value\n\'=SUM(A1:A10),100\n"+1234567,12",200';
expect(convertToCsv({ data, headers })).toBe(expected);
});

Expand Down

0 comments on commit 14c7679

Please sign in to comment.