-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.ts
198 lines (184 loc) · 3.53 KB
/
util.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2021, J2 Innovations. All Rights Reserved
*/
import { Node } from './Node'
import {
Kind,
HBool,
HCoord,
HDate,
HDateTime,
HDict,
HGrid,
HList,
HMarker,
HNa,
HNum,
HRef,
HRemove,
HStr,
HSymbol,
HTime,
HXStr,
HNamespace,
HUri,
} from 'haystack-core'
const RESERVED_NAMES = [
'Date',
'Kind',
'HBool',
'HCoord',
'HDate',
'HDateTime',
'HDict',
'HGrid',
'HList',
'HMarker',
'HNa',
'HNum',
'HRef',
'HRemove',
'HStr',
'HSymbol',
'HTime',
'HXStr',
'HNamespace',
'HUri',
'valueIsKind',
]
/**
* Generate the code string from a group of nodes.
*
* @param out The output to write too.
* @param nodes The nodes.
* @returns The code.
*/
export function generateNodes(
out: (code: string) => void,
nodes: Node[]
): void {
for (const node of nodes) {
let isEmpty = true
node.generateCode((code: string) => {
if (code) {
isEmpty = false
out(code)
}
})
if (!isEmpty) {
for (let i = 0; i < node.newLines; ++i) {
out('')
}
}
}
}
/**
* Return the generated code from the node.
*
* @param node The node to run.
* @returns The generated code.
*/
export function generateCodeFromNode(node: Node): string {
let str = ''
node.generateCode((code: string) => {
str += code + '\n'
})
return str
}
/**
* Return a name that can be used as a type.
*
* @param name The name to create the type name from.
* @returns The type name.
*/
export function makeTypeName(name: string): string {
if (!name) {
throw new Error(`Invalid interface name: ${name}`)
}
if (HNamespace.isConjunct(name)) {
name = HNamespace.splitConjunct(name)
.map((nm, i) => (i > 0 ? capitalizeFirstChar(nm) : nm))
.join('_')
}
if (HNamespace.isFeature(name)) {
name = name.split(':')[1]
}
let interfaceName = capitalizeFirstChar(name)
if (RESERVED_NAMES.includes(interfaceName)) {
interfaceName = `I${interfaceName}`
}
return interfaceName
}
/**
* Capitalize the first character of the string.
*
* @param str The string to capitalize the first letter of.
* @returns A string with the first letter being a capital.
*/
export function capitalizeFirstChar(str: string): string {
return str && str.length > 0
? `${str[0].toUpperCase()}${str.substring(1, str.length)}`
: ''
}
/**
* Convert the haystack kind to the name of the `haystack-core` contructor name.
*
* @param kind The haystack kind value.
* @returns The constructor name.
* @throws If the kind is unsupported or invalid.
*/
export function convertKindToCtorName(kind: Kind): string {
switch (kind) {
case Kind.Bool:
return HBool.name
case Kind.Coord:
return HCoord.name
case Kind.Date:
return HDate.name
case Kind.DateTime:
return HDateTime.name
case Kind.Dict:
return HDict.name
case Kind.Grid:
return HGrid.name
case Kind.List:
return HList.name
case Kind.Marker:
return HMarker.name
case Kind.NA:
return HNa.name
case Kind.Number:
return HNum.name
case Kind.Ref:
return HRef.name
case Kind.Remove:
return HRemove.name
case Kind.Str:
return HStr.name
case Kind.Symbol:
return HSymbol.name
case Kind.Time:
return HTime.name
case Kind.XStr:
return HXStr.name
case Kind.Uri:
return HUri.name
default:
throw new Error(`Unsupported kind: ${kind}`)
}
}
/**
* Write the document comments.
*
* @param out Used to output the code.
* @param doc The documentation to write.
*/
export function writeDocComment(
out: (code: string) => void,
doc: string
): void {
if (doc.trim()) {
out(' *')
doc.split('\n').forEach((line) => out(` * ${line.trim()}`))
}
}