Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/undent. undent removes extra leading spaces from template literals and multi‑line strings. jsr.io/@okikio/undent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import { align, embed, indent, undent } from "@okikio/undent";
// · = space (shown explicitly to make indentation visible)// 1. Core — strip structural indent, keep relative indent// without undentconsole.log(` Hello, world! indented deeper back to baseline`);//// ··Hello,·world!// ····indented·deeper// ··back·to·baseline//
// with undentconsole.log(undent` Hello, world! indented deeper back to baseline`);// Hello,·world!// ··indented·deeper// back·to·baseline
// 2. align() — multi-line values stay at their insertion columnconst items = "- alpha\n- beta\n- gamma";// without align()console.log(undent` list: ${items} end`);// list:// ··- alpha// - beta ← snaps to column 0// - gamma// end
// with align()console.log(undent` list: ${align(items)} end`);// list:// ··- alpha// ··- beta ← stays at insertion column// ··- gamma// end
// 3. embed() — strip a value's own indent, then align itconst sql = ` SELECT id, name FROM users WHERE active = true`;// without embed()console.log(undent` query: ${sql}`);// query:// ··// ····SELECT·id,·name ← baked-in indent bleeds through// ····FROM···users// ····WHERE··active·=·true//
// with embed()console.log(undent` query: ${embed(sql)}`);// query:// ··SELECT·id,·name// ··FROM···users// ··WHERE··active·=·true
// 4. Indent anchor — explicit column-0 baseline// without anchor — content deeper than min indent keeps its relative offsetconsole.log(undent` if (ready) { run(); }`);// if·(ready)·{// ··run();// }
// with anchor — anchor column becomes column 0; content deeper than anchor keeps offsetconsole.log(undent` ${indent} if (ready) { run(); }`);// ··if·(ready)·{ ← 2 cols deeper than anchor, preserved// ····run();// ··}
// 5. Newline fidelity — \r\n and \r pass through untouchedconst crlf = "A\r\nB\r\nC";// with undent + align — CRLF in values is never touchedconsole.log(JSON.stringify(undent`prefix\n${align(crlf)}`));// "prefix\nA\r\nB\r\nC"
// 6. Trim modes — per-side, fine-grained control// default: "all" — strips all blank wrapper linesconsole.log(JSON.stringify(undent` hello`));// "hello"
// "none" — keeps blank lines at both endsconsole.log(JSON.stringify(undent.with({ trim: "none" })` hello`));// "\nhello\n"
// "one" — strips at most one blank line from each endconsole.log(JSON.stringify(undent.with({ trim: "one" })` hello`));// "hello"
// per-side: keep leading blank, strip trailingconsole.log( JSON.stringify(undent.with({ trim: { leading: "none", trailing: "all" } })` hello`),);// "\nhello"