feat(medusa): CSV formatter accounts for comma and semicolon (#2789)

Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
Adrien de Peretti
2022-12-15 14:06:49 +01:00
committed by GitHub
co-authored by Sebastian Rindom
parent 2d22b17b49
commit 0fa5042e35
3 changed files with 24 additions and 5 deletions
@@ -17,11 +17,10 @@ export function pickObjectPropsByRegex(
for (const k in data) { for (const k in data) {
if (variantKeyPredicate(k)) { if (variantKeyPredicate(k)) {
const formattedData = ret[k] =
typeof data[k] === "string" typeof data[k] === "string"
? csvRevertCellContentFormatter(data[k] as string) ? csvRevertCellContentFormatter(data[k] as string)
: data[k] : data[k]
ret[k] = formattedData
} }
} }
@@ -7,11 +7,27 @@ type Case = {
const cases: [string, 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.", str: "Hello, my name is Adrien and I like writing single line content.",
expected: 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."',
}, },
], ],
[ [
@@ -1,9 +1,13 @@
export function csvCellContentFormatter(str: string): string { export function csvCellContentFormatter(str: string): string {
const newLineRegexp = new RegExp(/\n/g) const newLineRegexp = new RegExp(/\n/g)
const doubleQuoteRegexp = new RegExp(/"/g) const doubleQuoteRegexp = new RegExp(/"/g)
const comaRegexp = new RegExp(/,/g)
const semicolonRegexp = new RegExp(/;/g)
const hasNewLineChar = !!str.match(newLineRegexp) const hasNewLineChar = !!str.match(newLineRegexp)
if (!hasNewLineChar) { const hasComaChar = !!str.match(comaRegexp)
const hasSemicolonChar = !!str.match(semicolonRegexp)
if (!hasNewLineChar && !hasComaChar && !hasSemicolonChar) {
return str return str
} }