A CSV download is an output boundary. A title can contain commas, quotes or line breaks. Quoting is about keeping the structure valid; spreadsheet formula interpretation is a separate concern.
Read, predict, run
The function doubles embedded quotes and surrounds every field with quotes. Read the result as a CSV parser would, rather than expecting a split on commas to work. The final project exposes its export through a route handler:
Before pressing Run, predict what run returns for "a,b" and "a\"b", from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Export Text Without Changing Its MeaningNext.js · 5 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Study Library · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Export Text Without Changing Its Meaning</h1>
<p>
Read the input and predict the result before running. Change
lib/contract.js for the exercise.
</p>
<Cases />
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
export function run(value) {
return '"' + String(value).replaceAll('"', '""') + '"';
}
export const examples = [
["a,b", '"a,b"'],
['a"b', '"a""b"'],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Input "a,b" → "\"a,b\"".
Input "a\"b" → "\"a\"\"b\"".
The case table computes these results on the server side of the simulation.
It compares values; it does not prove a production deployment or database works.
The failure to avoid
Joining raw values with commas corrupts fields. A cell beginning with a formula character can be evaluated by spreadsheet software even if the CSV quoting is valid.
Trace the title He said "hi", then left through run and write the exact
output. Then split that output on commas, as a naive reader would, and count
the pieces.
Exercise
Extend the contract
For this spreadsheet-oriented export, prefix a single quote when a value begins with =, +, - or @. Then apply ordinary CSV escaping.
Edit lib/contract.js, then rerun the original cases. If the requested rule
changes an earlier result, update that expected value deliberately. Keep the
other cases. Add the acceptance cases to examples so the preview checks them too.
Input "=1+1" must return "\"'=1+1\"".
Your working spaceNext.js · 5 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Study Library · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Cases from "./Cases.jsx";
export default function Page() {
return (
<main>
<h1>Export Text Without Changing Its Meaning</h1>
<p>
Read the input and predict the result before running. Change
lib/contract.js for the exercise.
</p>
<Cases />
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
export function run(value) {
return '"' + String(value).replaceAll('"', '""') + '"';
}
export const examples = [
["a,b", '"a,b"'],
['a"b', '"a""b"'],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
Spreadsheet formulas and CSV structure are separate problems. The formula
guard changes what the cell says; the quoting keeps the cell in one piece. Do
the guard first, so the quoting covers its result.
Hint 2: a first step
Handle spreadsheet interpretation before structural quoting. Explain that this deliberately changes the exported cell text.
Show one solution
Replace the exported run function and any constants shown here; keep the
examples export underneath it. Compare the result with your own approach.
JavaScript
export function run(value) { const text = String(value); const safe = /^[=+@-]/.test(text) ? "'" + text : text; return '"' + safe.replaceAll('"', '""') + '"';}
The prefix is added before quoting, so a cell that starts with = and contains
quotes gets both the apostrophe and its quotes doubled. The pattern ^[=+@-]
checks only the first character, which is all a spreadsheet looks at. This
does change the data ('=1+1 instead of =1+1), so say so in the export's
documentation; a CSV meant for another program might skip it.
Explain it without the code
A colleague suggests removing commas from titles instead of quoting. Write
what that loses.
Going further
Read the Next.js guide for the framework boundary around this rule.
The preview runs code-playpad's Next.js simulation. Real caching, hydration,
authentication and persistence require the real-server checks described on Day 7.
Completion is saved in this browser only. It does not measure mastery.