Files and data
Choose an explicit policy for TSV fields
For a simple tab-separated export, decide what happens when a field contains a tab or line break. Rejecting unsupported content is clearer than silently producing a structurally ambiguous file.
Worked example
Node.js example · save as example.mjs and run node example.mjs
function tsvRow(values) {
const fields = values.map(String);
if (fields.some(value => /[\t\r\n]/.test(value)))
throw new Error("A field contains an unsupported delimiter");
return fields.join("\t");
}
console.log(tsvRow(["name", "count"]));
console.log(tsvRow(["demo", 3]));How it works
Tabs separate the columns and newlines separate records under this deliberately narrow contract. Rejecting embedded delimiters prevents one logical field from becoming multiple columns or rows. Other TSV consumers may define escaping differently, so the writer and reader must agree.
Limits and mistakes
Do not assume CSV-style quotes will be understood by every TSV importer. Spreadsheet interpretation and formula handling remain separate concerns. If arbitrary multiline text must round-trip, select a format or parser with a documented escaping contract.
Check the result
Expect two tab-separated lines with two columns each. Try a field containing a tab and confirm the helper reports an error instead of emitting a broken row.
Sources
Reference documentation for the behavior used in this guide: