-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
52 lines (48 loc) · 1.42 KB
/
common.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
import type { TypeParameter } from "../data.ts";
import type { Context } from "./context.ts";
import { typeToString } from "./type.ts";
export const documentToString = (document: string): string => {
const documentTrimmed = document.trim();
return documentTrimmed === "" ? "" : "\n/**\n" +
documentTrimmed
.split("\n")
.map((line) => line === "" ? " *" : " * " + line.replace(/\*\//gu, "* /"))
.join("\n") +
"\n */\n";
};
/**
* 文字列を`"`で囲んでエスケープする
*/
export const stringLiteralValueToString = (value: string): string =>
'"' +
value
.replace(/\\/gu, "\\\\")
.replace(/"/gu, '\\"')
.replace(/\r\n|\n/gu, "\\n") +
'"';
/**
* 型パラメーターを文字列にする `<T extends unknown>` `<ok extends unknown, error extends unknown>`
* extends unknown をつけた理由はJSXでも解釈できるようにするため
*/
export const typeParameterListToString = (
typeParameterList: ReadonlyArray<TypeParameter>,
context: Context,
): string => {
if (typeParameterList.length === 0) {
return "";
}
return (
"<" +
typeParameterList
.map((typeParameter) =>
typeParameter.name + " extends " +
(typeParameter.constraint
? typeToString(typeParameter.constraint, context)
: "unknown")
)
.join(", ") +
">"
);
};
export const indentNumberToString = (indent: number): string =>
" ".repeat(indent);