-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
68 lines (58 loc) · 1.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import dedent from "dedent";
import objectorarray from "objectorarray";
import parse from "fast-json-parse";
const ENDENT_ID = "twhZNwxI1aFG3r4";
function endent(strings: TemplateStringsArray, ...values: any[]) {
let result = "";
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
let value = values[i];
let isJson = false;
if (parse(value).value) {
value = parse(value).value;
isJson = true;
}
if ((value && value[ENDENT_ID]) || isJson) {
let rawlines = result.split("\n");
let l = rawlines[rawlines.length - 1].search(/\S/);
let endentation = l > 0 ? " ".repeat(l) : "";
let valueJson = isJson
? JSON.stringify(value, null, 2)
: value[ENDENT_ID];
let valueLines = valueJson.split("\n");
valueLines.forEach((l: string, index: number) => {
if (index > 0) {
result += "\n" + endentation + l;
} else {
result += l;
}
});
} else if (typeof value === "string" && value.includes("\n")) {
let endentations = result.match(/(?:^|\n)( *)$/);
if (typeof value === "string") {
let endentation = endentations ? endentations[1] : "";
result += value
.split("\n")
.map((str, i) => {
str = ENDENT_ID + str;
return i === 0 ? str : `${endentation}${str}`;
})
.join("\n");
} else {
result += value;
}
} else {
result += value;
}
}
}
result = dedent(result);
return result.split(ENDENT_ID).join("");
}
endent.pretty = (data?: object | string | number | undefined | null) => {
return objectorarray(data)
? { [ENDENT_ID]: JSON.stringify(data, null, 2) }
: data;
};
export default endent;