fix(medusa): Export/import fixes including export fields that contains new line char (#2150)

This commit is contained in:
Adrien de Peretti
2022-09-12 15:53:45 +02:00
committed by GitHub
parent ee8fe3a88b
commit b6161d2404
22 changed files with 699 additions and 364 deletions
@@ -0,0 +1,43 @@
import { csvCellContentFormatter } from "../csv-cell-content-formatter"
type Case = {
str: string
expected: string
}
const cases: [string, Case][] = [
[
"should return the exact input when there is no new line char",
{
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 new line when there is new line chars",
{
str: `Hello,
my name is Adrien and
I like writing multiline content
in a template string`,
expected:
'"Hello,\nmy name is Adrien and\nI like writing multiline content\nin a template string"',
},
],
[
"should return a formatted string escaping new line when there is new line chars and escape the double quote when there is double quotes",
{
str: 'Hello,\nmy name is "Adrien" and\nI like writing multiline content\nin a string',
expected:
'"Hello,\nmy name is ""Adrien"" and\nI like writing multiline content\nin a string"',
},
],
]
describe("csvCellContentFormatter", function () {
it.each(cases)("%s", (title: string, { str, expected }: Case) => {
const formattedStr = csvCellContentFormatter(str)
expect(formattedStr).toBe(expected)
})
})
@@ -0,0 +1,20 @@
export function csvCellContentFormatter(str: string): string {
const newLineRegexp = new RegExp(/\n/g)
const doubleQuoteRegexp = new RegExp(/"/g)
const hasNewLineChar = !!str.match(newLineRegexp)
if (!hasNewLineChar) {
return str
}
const formatterStr = str.replace(doubleQuoteRegexp, '""')
return `"${formatterStr}"`
}
export function csvRevertCellContentFormatter(str: string): string {
if (str.startsWith('"')) {
str = str.substring(1, str.length - 1)
}
return str
}
+1
View File
@@ -5,3 +5,4 @@ export * from "./generate-entity-id"
export * from "./remove-undefined-properties"
export * from "./is-defined"
export * from "./calculate-price-tax-amount"
export * from "./csv-cell-content-formatter"