1
1
'use strict' ;
2
- const nodeDocUrl = '' ;
2
+
3
3
const jsDocPrefix = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/' ;
4
- const jsDocUrl = ` ${ jsDocPrefix } Reference/Global_Objects/` ;
4
+
5
5
const jsPrimitiveUrl = `${ jsDocPrefix } Data_structures` ;
6
6
const jsPrimitives = {
7
7
'boolean' : 'Boolean' ,
@@ -12,6 +12,8 @@ const jsPrimitives = {
12
12
'symbol' : 'Symbol' ,
13
13
'undefined' : 'Undefined'
14
14
} ;
15
+
16
+ const jsGlobalObjectsUrl = `${ jsDocPrefix } Reference/Global_Objects/` ;
15
17
const jsGlobalTypes = [
16
18
'Array' , 'ArrayBuffer' , 'AsyncFunction' , 'DataView' , 'Date' , 'Error' ,
17
19
'EvalError' , 'Float32Array' , 'Float64Array' , 'Function' , 'Generator' ,
@@ -21,7 +23,8 @@ const jsGlobalTypes = [
21
23
'Uint16Array' , 'Uint32Array' , 'Uint8Array' , 'Uint8ClampedArray' , 'WeakMap' ,
22
24
'WeakSet'
23
25
] ;
24
- const typeMap = {
26
+
27
+ const customTypesMap = {
25
28
'Iterable' :
26
29
`${ jsDocPrefix } Reference/Iteration_protocols#The_iterable_protocol` ,
27
30
'Iterator' :
@@ -96,41 +99,43 @@ const typeMap = {
96
99
97
100
const arrayPart = / (?: \[ ] ) + $ / ;
98
101
99
- module . exports = {
100
- toLink : function ( typeInput ) {
101
- const typeLinks = [ ] ;
102
- typeInput = typeInput . replace ( '{' , '' ) . replace ( '}' , '' ) ;
103
- const typeTexts = typeInput . split ( '|' ) ;
104
-
105
- typeTexts . forEach ( function ( typeText ) {
106
- typeText = typeText . trim ( ) ;
107
- if ( typeText ) {
108
- let typeUrl = null ;
109
-
110
- // To support type[], type[][] etc., we store the full string
111
- // and use the bracket-less version to lookup the type URL
112
- const typeTextFull = typeText ;
113
- typeText = typeText . replace ( arrayPart , '' ) ;
114
-
115
- const primitive = jsPrimitives [ typeText . toLowerCase ( ) ] ;
116
-
117
- if ( primitive !== undefined ) {
118
- typeUrl = `${ jsPrimitiveUrl } #${ primitive } _type` ;
119
- } else if ( jsGlobalTypes . indexOf ( typeText ) !== - 1 ) {
120
- typeUrl = jsDocUrl + typeText ;
121
- } else if ( typeMap [ typeText ] ) {
122
- typeUrl = nodeDocUrl + typeMap [ typeText ] ;
123
- }
124
-
125
- if ( typeUrl ) {
126
- typeLinks . push ( `
127
- <a href="${ typeUrl } " class="type"><${ typeTextFull } ></a>` ) ;
128
- } else {
129
- typeLinks . push ( `<span class="type"><${ typeTextFull } ></span>` ) ;
130
- }
102
+ function toLink ( typeInput ) {
103
+ const typeLinks = [ ] ;
104
+ typeInput = typeInput . replace ( '{' , '' ) . replace ( '}' , '' ) ;
105
+ const typeTexts = typeInput . split ( '|' ) ;
106
+
107
+ typeTexts . forEach ( ( typeText ) => {
108
+ typeText = typeText . trim ( ) ;
109
+ if ( typeText ) {
110
+ let typeUrl = null ;
111
+
112
+ // To support type[], type[][] etc., we store the full string
113
+ // and use the bracket-less version to lookup the type URL
114
+ const typeTextFull = typeText ;
115
+ typeText = typeText . replace ( arrayPart , '' ) ;
116
+
117
+ const primitive = jsPrimitives [ typeText . toLowerCase ( ) ] ;
118
+
119
+ if ( primitive !== undefined ) {
120
+ typeUrl = `${ jsPrimitiveUrl } #${ primitive } _type` ;
121
+ } else if ( jsGlobalTypes . includes ( typeText ) ) {
122
+ typeUrl = `${ jsGlobalObjectsUrl } ${ typeText } ` ;
123
+ } else if ( customTypesMap [ typeText ] ) {
124
+ typeUrl = customTypesMap [ typeText ] ;
131
125
}
132
- } ) ;
133
126
134
- return typeLinks . length ? typeLinks . join ( ' | ' ) : typeInput ;
135
- }
136
- } ;
127
+ if ( typeUrl ) {
128
+ typeLinks . push (
129
+ `<a href="${ typeUrl } " class="type"><${ typeTextFull } ></a>` ) ;
130
+ } else {
131
+ typeLinks . push ( `<span class="type"><${ typeTextFull } ></span>` ) ;
132
+ }
133
+ } else {
134
+ throw new Error ( `Empty type slot: ${ typeInput } ` ) ;
135
+ }
136
+ } ) ;
137
+
138
+ return typeLinks . length ? typeLinks . join ( ' | ' ) : typeInput ;
139
+ }
140
+
141
+ module . exports = { toLink } ;
0 commit comments