UNPKG

radashi-helper

Version:
33 lines (30 loc) 843 B
import { isArray } from "./chunk-VOEQFXI5.js"; // src/util/dedent.ts function dedent(text, ...values) { if (isArray(text)) { if (values.length > 0) { return dedent( text.reduce((acc, input, i) => { let value = String(values[i] ?? ""); const indent2 = value.includes("\n") && input.match(/[ \t]*(?=[^\n]*$)/)?.[0]; if (indent2) { value = value.replace(/\n(?=[^\n]*?\S)/g, "\n" + indent2); } return acc + input + value; }, "") ); } text = text[0]; } const indent = values[0] ?? detectIndent(text); const output = indent ? text.replace(new RegExp(`^${indent}`, "gm"), "") : text; return output.replace(/^[ \t]*\n|\n[ \t]*$/g, ""); } function detectIndent(text) { return text.match(/^[ \t]*(?=\S)/m)?.[0]; } export { dedent };