-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
typeFormatters.js
65 lines (57 loc) · 1.84 KB
/
typeFormatters.js
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
const _ = require("lodash");
const { checkAndRenameModelName } = require("./modelNames");
const formatters = {
enum: (content) => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(",\n"),
intEnum: (content) => _.map(content, ({ value }) => value).join(" | "),
object: (content) =>
_.map(content, (part) => {
const extraSpace = " ";
const result = `${extraSpace}${part.field};\n`;
const comments = _.compact([part.title, part.description]).reduce(
(acc, comment) => [...acc, ...comment.split(/\n/g)],
[],
);
const commonText = comments.length
? [
"",
...(comments.length === 1
? [`/** ${comments[0]} */`]
: ["/**", ...comments.map((commentPart) => ` * ${commentPart}`), " */"]),
]
.map((part) => `${extraSpace}${part}\n`)
.join("")
: "";
return `${commonText}${result}`;
}).join(""),
type: (content) => {
if (content.includes(" & ")) {
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
}
if (content.includes(" | ")) {
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
}
return content;
},
primitive: (content) => checkAndRenameModelName(content),
};
const inlineExtraFormatters = {
object: (parsedSchema) => {
return {
...parsedSchema,
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : "type",
content: parsedSchema.content.length
? `{ ${parsedSchema.content.map((part) => part.field).join(", ")} }`
: "object",
};
},
enum: (parsedSchema) => {
return {
...parsedSchema,
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(" | "),
};
},
};
module.exports = {
formatters,
inlineExtraFormatters,
};