From 0fa5042e35f1feff24e98632ef7d0b1140290670 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 15 Dec 2022 14:06:49 +0100 Subject: [PATCH] feat(medusa): CSV formatter accounts for comma and semicolon (#2789) Co-authored-by: Sebastian Rindom --- .../strategies/batch-jobs/product/utils.ts | 3 +-- .../csv-cell-content-formatter.spec.ts | 20 +++++++++++++++++-- .../src/utils/csv-cell-content-formatter.ts | 6 +++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/medusa/src/strategies/batch-jobs/product/utils.ts b/packages/medusa/src/strategies/batch-jobs/product/utils.ts index 0df977a399..dfc5b7c6d0 100644 --- a/packages/medusa/src/strategies/batch-jobs/product/utils.ts +++ b/packages/medusa/src/strategies/batch-jobs/product/utils.ts @@ -17,11 +17,10 @@ export function pickObjectPropsByRegex( for (const k in data) { if (variantKeyPredicate(k)) { - const formattedData = + ret[k] = typeof data[k] === "string" ? csvRevertCellContentFormatter(data[k] as string) : data[k] - ret[k] = formattedData } } diff --git a/packages/medusa/src/utils/__tests__/csv-cell-content-formatter.spec.ts b/packages/medusa/src/utils/__tests__/csv-cell-content-formatter.spec.ts index 888fae37ce..9358d9a730 100644 --- a/packages/medusa/src/utils/__tests__/csv-cell-content-formatter.spec.ts +++ b/packages/medusa/src/utils/__tests__/csv-cell-content-formatter.spec.ts @@ -7,11 +7,27 @@ type Case = { const cases: [string, Case][] = [ [ - "should return the exact input when there is no new line char", + "should return a the exact input content", + { + str: "Hello my name is Adrien and I like writing single line content.", + expected: + 'Hello my name is Adrien and I like writing single line content.', + }, + ], + [ + "should return a formatted string escaping the coma", { str: "Hello, my name is Adrien and I like writing single line content.", expected: - "Hello, my name is Adrien and I like writing single line content.", + '"Hello, my name is Adrien and I like writing single line content."', + }, + ], + [ + "should return a formatted string escaping the semicolon", + { + str: "Hello; my name is Adrien and I like writing single line content.", + expected: + '"Hello; my name is Adrien and I like writing single line content."', }, ], [ diff --git a/packages/medusa/src/utils/csv-cell-content-formatter.ts b/packages/medusa/src/utils/csv-cell-content-formatter.ts index 7c0d4ae1a4..f7a1dc5989 100644 --- a/packages/medusa/src/utils/csv-cell-content-formatter.ts +++ b/packages/medusa/src/utils/csv-cell-content-formatter.ts @@ -1,9 +1,13 @@ export function csvCellContentFormatter(str: string): string { const newLineRegexp = new RegExp(/\n/g) const doubleQuoteRegexp = new RegExp(/"/g) + const comaRegexp = new RegExp(/,/g) + const semicolonRegexp = new RegExp(/;/g) const hasNewLineChar = !!str.match(newLineRegexp) - if (!hasNewLineChar) { + const hasComaChar = !!str.match(comaRegexp) + const hasSemicolonChar = !!str.match(semicolonRegexp) + if (!hasNewLineChar && !hasComaChar && !hasSemicolonChar) { return str }