21 lines
705 B
JavaScript
21 lines
705 B
JavaScript
function serializeCell(value, delimiter) {
|
|
const raw = String(value ?? '');
|
|
const formulaLike = /^[\u0000-\u0020\uFEFF]*[=+\-@]/.test(raw);
|
|
const numeric = /^-?\d+(?:\.\d+)?$/.test(raw);
|
|
const text = formulaLike && !numeric ? `'${raw}` : raw;
|
|
|
|
return /[",\r\n]/.test(text) || text.includes(delimiter)
|
|
? `"${text.replaceAll('"', '""')}"`
|
|
: text;
|
|
}
|
|
|
|
/** Serializes a comma-separated cell and neutralizes spreadsheet formulas. */
|
|
export function csvCell(value) {
|
|
return serializeCell(value, ',');
|
|
}
|
|
|
|
/** Serializes a cell for a locale-specific delimited export. */
|
|
export function csvCellWithDelimiter(value, delimiter) {
|
|
return serializeCell(value, delimiter);
|
|
}
|