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
parent 2d22b17b49
commit 0fa5042e35
3 changed files with 24 additions and 5 deletions

View File

@@ -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
}
}

View File

@@ -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."',
},
],
[

View File

@@ -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
}