style: fix formatting
This commit is contained in:
parent
46264c85f4
commit
baae22657e
@ -30,9 +30,11 @@ export default function trimIndent(strings: TemplateStringsArray, ...args: any[]
|
|||||||
let string = populateTemplate(strings, ...args)
|
let string = populateTemplate(strings, ...args)
|
||||||
.replace(/^[\r\n]/, "")
|
.replace(/^[\r\n]/, "")
|
||||||
.replace(/\r?\n *$/, "");
|
.replace(/\r?\n *$/, "");
|
||||||
const dents = string.match(/^([ \t])+/gm)
|
const dents =
|
||||||
?.filter(s => /^\s+$/.test(s))
|
string
|
||||||
?.map(s => s.length) ?? [];
|
.match(/^([ \t])+/gm)
|
||||||
|
?.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);
|
||||||
|
@ -2,36 +2,35 @@ import trimIndent from "keycloakify/bin/tools/trimIndent";
|
|||||||
import { it, describe, assert } from "vitest";
|
import { it, describe, assert } from "vitest";
|
||||||
|
|
||||||
describe("trimIndent", () => {
|
describe("trimIndent", () => {
|
||||||
|
|
||||||
it("does not change a left-aligned string as expected", () => {
|
it("does not change a left-aligned string as expected", () => {
|
||||||
const txt = trimIndent`lorem
|
const txt = trimIndent`lorem
|
||||||
ipsum`
|
ipsum`;
|
||||||
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
|
assert.equal(txt, ["lorem", "ipsum"].join("\n"));
|
||||||
})
|
});
|
||||||
|
|
||||||
it("removes leading and trailing empty lines from a left-aligned string", () => {
|
it("removes leading and trailing empty lines from a left-aligned string", () => {
|
||||||
const txt = trimIndent`
|
const txt = trimIndent`
|
||||||
lorem
|
lorem
|
||||||
ipsum
|
ipsum
|
||||||
`
|
`;
|
||||||
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
|
assert.equal(txt, ["lorem", "ipsum"].join("\n"));
|
||||||
})
|
});
|
||||||
|
|
||||||
it("removes indent from an aligned string", () => {
|
it("removes indent from an aligned string", () => {
|
||||||
const txt = trimIndent`
|
const txt = trimIndent`
|
||||||
lorem
|
lorem
|
||||||
ipsum
|
ipsum
|
||||||
`
|
`;
|
||||||
assert.equal(txt, ['lorem', 'ipsum'].join('\n'))
|
assert.equal(txt, ["lorem", "ipsum"].join("\n"));
|
||||||
})
|
});
|
||||||
|
|
||||||
it("removes indent from unaligned string", () => {
|
it("removes indent from unaligned string", () => {
|
||||||
const txt = trimIndent`
|
const txt = trimIndent`
|
||||||
lorem
|
lorem
|
||||||
ipsum
|
ipsum
|
||||||
`
|
`;
|
||||||
assert.equal(txt, ['lorem', ' ipsum'].join('\n'))
|
assert.equal(txt, ["lorem", " ipsum"].join("\n"));
|
||||||
})
|
});
|
||||||
|
|
||||||
it("removes only first and last empty line", () => {
|
it("removes only first and last empty line", () => {
|
||||||
const txt = trimIndent`
|
const txt = trimIndent`
|
||||||
@ -39,19 +38,28 @@ ipsum
|
|||||||
lorem
|
lorem
|
||||||
ipsum
|
ipsum
|
||||||
|
|
||||||
`
|
`;
|
||||||
|
|
||||||
assert.equal(txt, ['', 'lorem', 'ipsum', ''].join('\n'))
|
|
||||||
})
|
|
||||||
|
|
||||||
|
assert.equal(txt, ["", "lorem", "ipsum", ""].join("\n"));
|
||||||
|
});
|
||||||
|
|
||||||
it("interpolates non-strings", () => {
|
it("interpolates non-strings", () => {
|
||||||
const d = new Date()
|
const d = new Date();
|
||||||
const txt = trimIndent`
|
const txt = trimIndent`
|
||||||
lorem
|
lorem
|
||||||
${d}
|
${d}
|
||||||
ipsum`
|
ipsum`;
|
||||||
|
|
||||||
assert.equal(txt, ['lorem', String(d), 'ipsum'].join('\n'))
|
assert.equal(txt, ["lorem", String(d), "ipsum"].join("\n"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("inderpolates preserving new-lines in the interpolated bits", () => {
|
||||||
|
const a = ["ipsum", "dolor", "sit"].join('\n')
|
||||||
|
const txt = trimIndent`
|
||||||
|
lorem
|
||||||
|
${a}
|
||||||
|
amet
|
||||||
|
`
|
||||||
|
assert.equal(txt, ['lorem', 'ipsum', 'dolor', 'sit', 'amet'].join('\n'))
|
||||||
})
|
})
|
||||||
})
|
|
Loading…
x
Reference in New Issue
Block a user