|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Line | |
|---|
| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | function dedent(strings) {
|
|---|
| 4 |
|
|---|
| 5 | var raw = void 0;
|
|---|
| 6 | if (typeof strings === "string") {
|
|---|
| 7 | // dedent can be used as a plain function
|
|---|
| 8 | raw = [strings];
|
|---|
| 9 | } else {
|
|---|
| 10 | raw = strings.raw;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | // first, perform interpolation
|
|---|
| 14 | var result = "";
|
|---|
| 15 | for (var i = 0; i < raw.length; i++) {
|
|---|
| 16 | result += raw[i].
|
|---|
| 17 | // join lines when there is a suppressed newline
|
|---|
| 18 | replace(/\\\n[ \t]*/g, "").
|
|---|
| 19 |
|
|---|
| 20 | // handle escaped backticks
|
|---|
| 21 | replace(/\\`/g, "`");
|
|---|
| 22 |
|
|---|
| 23 | if (i < (arguments.length <= 1 ? 0 : arguments.length - 1)) {
|
|---|
| 24 | result += arguments.length <= i + 1 ? undefined : arguments[i + 1];
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | // now strip indentation
|
|---|
| 29 | var lines = result.split("\n");
|
|---|
| 30 | var mindent = null;
|
|---|
| 31 | lines.forEach(function (l) {
|
|---|
| 32 | var m = l.match(/^(\s+)\S+/);
|
|---|
| 33 | if (m) {
|
|---|
| 34 | var indent = m[1].length;
|
|---|
| 35 | if (!mindent) {
|
|---|
| 36 | // this is the first indented line
|
|---|
| 37 | mindent = indent;
|
|---|
| 38 | } else {
|
|---|
| 39 | mindent = Math.min(mindent, indent);
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | });
|
|---|
| 43 |
|
|---|
| 44 | if (mindent !== null) {
|
|---|
| 45 | result = lines.map(function (l) {
|
|---|
| 46 | return l[0] === " " ? l.slice(mindent) : l;
|
|---|
| 47 | }).join("\n");
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // dedent eats leading and trailing whitespace too
|
|---|
| 51 | result = result.trim();
|
|---|
| 52 |
|
|---|
| 53 | // handle escaped newlines at the end to ensure they don't get stripped too
|
|---|
| 54 | return result.replace(/\\n/g, "\n");
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (typeof module !== "undefined") {
|
|---|
| 58 | module.exports = dedent;
|
|---|
| 59 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.