UNPKG

2.04 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _default = createDedent({});
8exports.default = _default;
9function createDedent(options) {
10 dedent.withOptions = newOptions => createDedent({
11 ...options,
12 ...newOptions
13 });
14 return dedent;
15 function dedent(strings, ...values) {
16 const raw = typeof strings === "string" ? [strings] : strings.raw;
17 const {
18 escapeSpecialCharacters = Array.isArray(strings)
19 } = options;
20
21 // first, perform interpolation
22 let result = "";
23 for (let i = 0; i < raw.length; i++) {
24 let next = raw[i];
25 if (escapeSpecialCharacters) {
26 // handle escaped newlines, backticks, and interpolation characters
27 next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\{/g, "{");
28 }
29 result += next;
30 if (i < values.length) {
31 // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
32 result += values[i];
33 }
34 }
35
36 // now strip indentation
37 const lines = result.split("\n");
38 let mindent = null;
39 for (const l of lines) {
40 const m = l.match(/^(\s+)\S+/);
41 if (m) {
42 const indent = m[1].length;
43 if (!mindent) {
44 // this is the first indented line
45 mindent = indent;
46 } else {
47 mindent = Math.min(mindent, indent);
48 }
49 }
50 }
51 if (mindent !== null) {
52 const m = mindent; // appease TypeScript
53 result = lines
54 // https://github.com/typescript-eslint/typescript-eslint/issues/7140
55 // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
56 .map(l => l[0] === " " || l[0] === "\t" ? l.slice(m) : l).join("\n");
57 }
58 return result
59 // dedent eats leading and trailing whitespace too
60 .trim()
61 // handle escaped newlines at the end to ensure they don't get stripped too
62 .replace(/\\n/g, "\n");
63 }
64}
65module.exports = exports.default;
66module.exports.default = exports.default;