@@ -13,6 +13,15 @@ type CommentObject = {
13
13
type : string ; // Type of node
14
14
} ;
15
15
16
+ const COMMENT_RE = / \* \/ / g;
17
+ const LB_RE = / \r ? \n / g;
18
+ const DOUBLE_QUOTE_RE = / " / g;
19
+ const SINGLE_QUOTE_RE = / ' / g;
20
+ const ESC_0_RE = / \~ 0 / g;
21
+ const ESC_1_RE = / \~ 1 / g;
22
+ const TILDE_RE = / \~ / g;
23
+ const FS_RE = / \/ / g;
24
+
16
25
/**
17
26
* Preparing comments from fields
18
27
* @see {comment} for output examples
@@ -51,7 +60,7 @@ export function prepareComment(v: CommentObject): string | void {
51
60
}
52
61
53
62
export function comment ( text : string ) : string {
54
- const commentText = text . trim ( ) . replace ( / \* \/ / g , "*\\/" ) ;
63
+ const commentText = text . trim ( ) . replace ( COMMENT_RE , "*\\/" ) ;
55
64
56
65
// if single-line comment
57
66
if ( commentText . indexOf ( "\n" ) === - 1 ) {
@@ -60,7 +69,7 @@ export function comment(text: string): string {
60
69
61
70
// if multi-line comment
62
71
return `/**
63
- * ${ commentText . replace ( / \r ? \n / g , "\n * " ) }
72
+ * ${ commentText . replace ( LB_RE , "\n * " ) }
64
73
*/\n` ;
65
74
}
66
75
@@ -90,7 +99,7 @@ export type ParsedSimpleValue = string | number | boolean;
90
99
* @returns parsed value
91
100
*/
92
101
export function parseSingleSimpleValue ( value : unknown , isNodeNullable = false ) : ParsedSimpleValue {
93
- if ( typeof value === "string" ) return `'${ value . replace ( / ' / g , "\\'" ) } '` ;
102
+ if ( typeof value === "string" ) return `'${ value . replace ( SINGLE_QUOTE_RE , "\\'" ) } '` ;
94
103
95
104
if ( typeof value === "number" || typeof value === "boolean" ) return value ;
96
105
@@ -105,13 +114,13 @@ type SchemaObjectType =
105
114
| "anyOf"
106
115
| "array"
107
116
| "boolean"
117
+ | "const"
108
118
| "enum"
109
119
| "number"
110
120
| "object"
111
121
| "oneOf"
112
122
| "ref"
113
123
| "string"
114
- | "const"
115
124
| "unknown" ;
116
125
export function nodeType ( obj : any ) : SchemaObjectType {
117
126
if ( ! obj || typeof obj !== "object" ) {
@@ -138,12 +147,19 @@ export function nodeType(obj: any): SchemaObjectType {
138
147
}
139
148
140
149
// string
141
- if ( [ "binary" , "byte" , "date" , "dateTime" , "password" , "string" ] . includes ( obj . type ) ) {
150
+ if (
151
+ obj . type === "string" ||
152
+ obj . type === "binary" ||
153
+ obj . type === "byte" ||
154
+ obj . type === "date" ||
155
+ obj . type === "dateTime" ||
156
+ obj . type === "password"
157
+ ) {
142
158
return "string" ;
143
159
}
144
160
145
161
// number
146
- if ( [ "double" , "float" , "integer" , "number" ] . includes ( obj . type ) ) {
162
+ if ( obj . type === "integer" || obj . type === "number" || obj . type === "float" || obj . type === "double" ) {
147
163
return "number" ;
148
164
}
149
165
@@ -196,12 +212,12 @@ export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {
196
212
197
213
/** Decode $ref (https://swagger.io/docs/specification/using-ref/#escape) */
198
214
export function decodeRef ( ref : string ) : string {
199
- return ref . replace ( / \~ 0 / g , "~" ) . replace ( / \~ 1 / g , "/" ) . replace ( / " / g , '\\"' ) ;
215
+ return ref . replace ( ESC_0_RE , "~" ) . replace ( ESC_1_RE , "/" ) . replace ( DOUBLE_QUOTE_RE , '\\"' ) ;
200
216
}
201
217
202
218
/** Encode $ref (https://swagger.io/docs/specification/using-ref/#escape) */
203
219
export function encodeRef ( ref : string ) : string {
204
- return ref . replace ( / \~ / g , "~0" ) . replace ( / \/ / g , "~1" ) ;
220
+ return ref . replace ( TILDE_RE , "~0" ) . replace ( FS_RE , "~1" ) ;
205
221
}
206
222
207
223
/** Convert T into T[]; */
0 commit comments