Files
Investidor-V2/resources/js/lib/csv.js
T
nala 1106509116
tests / ci (push) Canceled after 0s
vrsao 2
2026-10-08 14:02:01 +02:00

27 lines
860 B
JavaScript

function serializeCell(value, delimiter) {
const raw = String(value ?? '');
let prefix = 0;
while (
prefix < raw.length &&
(raw.charCodeAt(prefix) <= 32 || raw.charCodeAt(prefix) === 0xfeff)
)
prefix++;
const formulaLike = /^[=+\-@]/.test(raw.slice(prefix));
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);
}