fix(medusa): Export/import fixes including export fields that contains new line char (#2150)
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user