@@ -13,6 +13,15 @@ type CommentObject = {
1313 type : string ; // Type of node
1414} ;
1515
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+
1625/**
1726 * Preparing comments from fields
1827 * @see {comment} for output examples
@@ -51,7 +60,7 @@ export function prepareComment(v: CommentObject): string | void {
5160}
5261
5362export function comment ( text : string ) : string {
54- const commentText = text . trim ( ) . replace ( / \* \/ / g , "*\\/" ) ;
63+ const commentText = text . trim ( ) . replace ( COMMENT_RE , "*\\/" ) ;
5564
5665 // if single-line comment
5766 if ( commentText . indexOf ( "\n" ) === - 1 ) {
@@ -60,7 +69,7 @@ export function comment(text: string): string {
6069
6170 // if multi-line comment
6271 return `/**
63- * ${ commentText . replace ( / \r ? \n / g , "\n * " ) }
72+ * ${ commentText . replace ( LB_RE , "\n * " ) }
6473 */\n` ;
6574}
6675
@@ -90,7 +99,7 @@ export type ParsedSimpleValue = string | number | boolean;
9099 * @returns parsed value
91100 */
92101export 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 , "\\'" ) } '` ;
94103
95104 if ( typeof value === "number" || typeof value === "boolean" ) return value ;
96105
@@ -105,13 +114,13 @@ type SchemaObjectType =
105114 | "anyOf"
106115 | "array"
107116 | "boolean"
117+ | "const"
108118 | "enum"
109119 | "number"
110120 | "object"
111121 | "oneOf"
112122 | "ref"
113123 | "string"
114- | "const"
115124 | "unknown" ;
116125export function nodeType ( obj : any ) : SchemaObjectType {
117126 if ( ! obj || typeof obj !== "object" ) {
@@ -138,12 +147,19 @@ export function nodeType(obj: any): SchemaObjectType {
138147 }
139148
140149 // 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+ ) {
142158 return "string" ;
143159 }
144160
145161 // number
146- if ( [ "double" , "float" , "integer" , "number" ] . includes ( obj . type ) ) {
162+ if ( obj . type === "integer" || obj . type === "number" || obj . type === "float" || obj . type === "double" ) {
147163 return "number" ;
148164 }
149165
@@ -196,12 +212,12 @@ export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {
196212
197213/** Decode $ref (https://swagger.io/docs/specification/using-ref/#escape) */
198214export 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 , '\\"' ) ;
200216}
201217
202218/** Encode $ref (https://swagger.io/docs/specification/using-ref/#escape) */
203219export 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" ) ;
205221}
206222
207223/** Convert T into T[]; */
0 commit comments