Add unit test and fix some more use cases

This commit is contained in:
Waldemar Reusch 2023-04-01 22:36:54 +02:00
parent 2811eb6024
commit 46264c85f4
2 changed files with 68 additions and 22 deletions

View File

@ -21,13 +21,18 @@ function populateTemplate(strings: TemplateStringsArray, ...args: unknown[]) {
return chunks.join(""); return chunks.join("");
} }
function trimIndentPrivate(removeEmptyLeadingAndTrailingLines: boolean, strings: TemplateStringsArray, ...args: any[]) { /**
* Shift all lines left by the *smallest* indentation level,
* and remove initial newline and all trailing spaces.
*/
export default function trimIndent(strings: TemplateStringsArray, ...args: any[]) {
// Remove initial and final newlines // Remove initial and final newlines
let string = populateTemplate(strings, ...args); let string = populateTemplate(strings, ...args)
if (removeEmptyLeadingAndTrailingLines) { .replace(/^[\r\n]/, "")
string = string.replace(/^[\r\n]/, "").replace(/[^\S\r\n]*[\r\n]$/, ""); .replace(/\r?\n *$/, "");
} const dents = string.match(/^([ \t])+/gm)
const dents = string.match(/^([ \t])+/gm)?.map(s => s.length) ?? []; ?.filter(s => /^\s+$/.test(s))
?.map(s => s.length) ?? [];
// No dents? no change required // No dents? no change required
if (!dents || dents.length == 0) return string; if (!dents || dents.length == 0) return string;
const minDent = Math.min(...dents); const minDent = Math.min(...dents);
@ -37,19 +42,3 @@ function trimIndentPrivate(removeEmptyLeadingAndTrailingLines: boolean, strings:
const dedented = string.replace(re, ""); const dedented = string.replace(re, "");
return dedented; return dedented;
} }
/**
* Shift all lines left by the *smallest* indentation level,
* and remove initial newline and all trailing spaces.
*/
export default function trimIndent(strings: TemplateStringsArray, ...args: unknown[]) {
return trimIndentPrivate(true, strings, ...args);
}
/**
* Shift all lines left by the *smallest* indentation level,
* and _keep_ initial newline and all trailing spaces.
*/
trimIndent.keepLeadingAndTrailingNewlines = function (strings: TemplateStringsArray, ...args: unknown[]) {
return trimIndentPrivate(false, strings, ...args);
};

View File

@ -0,0 +1,57 @@
import trimIndent from "keycloakify/bin/tools/trimIndent";
import { it, describe, assert } from "vitest";
describe("trimIndent", () => {
it("does not change a left-aligned string as expected", () => {
const txt = trimIndent`lorem
ipsum`
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
})
it("removes leading and trailing empty lines from a left-aligned string", () => {
const txt = trimIndent`
lorem
ipsum
`
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
})
it("removes indent from an aligned string", () => {
const txt = trimIndent`
lorem
ipsum
`
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
})
it("removes indent from unaligned string", () => {
const txt = trimIndent`
lorem
ipsum
`
assert.equal(txt, ['lorem', ' ipsum'].join('\n'))
})
it("removes only first and last empty line", () => {
const txt = trimIndent`
lorem
ipsum
`
assert.equal(txt, ['', 'lorem', 'ipsum', ''].join('\n'))
})
it("interpolates non-strings", () => {
const d = new Date()
const txt = trimIndent`
lorem
${d}
ipsum`
assert.equal(txt, ['lorem', String(d), 'ipsum'].join('\n'))
})
})