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' :
@@ -95,41 +98,43 @@ const typeMap = {
95
98
96
99
const arrayPart = / (?: \[ ] ) + $ / ;
97
100
98
- module . exports = {
99
- toLink : function ( typeInput ) {
100
- const typeLinks = [ ] ;
101
- typeInput = typeInput . replace ( '{' , '' ) . replace ( '}' , '' ) ;
102
- const typeTexts = typeInput . split ( '|' ) ;
103
-
104
- typeTexts . forEach ( function ( typeText ) {
105
- typeText = typeText . trim ( ) ;
106
- if ( typeText ) {
107
- let typeUrl = null ;
108
-
109
- // To support type[], type[][] etc., we store the full string
110
- // and use the bracket-less version to lookup the type URL
111
- const typeTextFull = typeText ;
112
- typeText = typeText . replace ( arrayPart , '' ) ;
113
-
114
- const primitive = jsPrimitives [ typeText . toLowerCase ( ) ] ;
115
-
116
- if ( primitive !== undefined ) {
117
- typeUrl = `${ jsPrimitiveUrl } #${ primitive } _type` ;
118
- } else if ( jsGlobalTypes . indexOf ( typeText ) !== - 1 ) {
119
- typeUrl = jsDocUrl + typeText ;
120
- } else if ( typeMap [ typeText ] ) {
121
- typeUrl = nodeDocUrl + typeMap [ typeText ] ;
122
- }
123
-
124
- if ( typeUrl ) {
125
- typeLinks . push ( `
126
- <a href="${ typeUrl } " class="type"><${ typeTextFull } ></a>` ) ;
127
- } else {
128
- typeLinks . push ( `<span class="type"><${ typeTextFull } ></span>` ) ;
129
- }
101
+ function toLink ( typeInput ) {
102
+ const typeLinks = [ ] ;
103
+ typeInput = typeInput . replace ( '{' , '' ) . replace ( '}' , '' ) ;
104
+ const typeTexts = typeInput . split ( '|' ) ;
105
+
106
+ typeTexts . forEach ( ( typeText ) => {
107
+ typeText = typeText . trim ( ) ;
108
+ if ( typeText ) {
109
+ let typeUrl = null ;
110
+
111
+ // To support type[], type[][] etc., we store the full string
112
+ // and use the bracket-less version to lookup the type URL
113
+ const typeTextFull = typeText ;
114
+ typeText = typeText . replace ( arrayPart , '' ) ;
115
+
116
+ const primitive = jsPrimitives [ typeText . toLowerCase ( ) ] ;
117
+
118
+ if ( primitive !== undefined ) {
119
+ typeUrl = `${ jsPrimitiveUrl } #${ primitive } _type` ;
120
+ } else if ( jsGlobalTypes . includes ( typeText ) ) {
121
+ typeUrl = `${ jsGlobalObjectsUrl } ${ typeText } ` ;
122
+ } else if ( customTypesMap [ typeText ] ) {
123
+ typeUrl = customTypesMap [ typeText ] ;
130
124
}
131
- } ) ;
132
125
133
- return typeLinks . length ? typeLinks . join ( ' | ' ) : typeInput ;
134
- }
135
- } ;
126
+ if ( typeUrl ) {
127
+ typeLinks . push (
128
+ `<a href="${ typeUrl } " class="type"><${ typeTextFull } ></a>` ) ;
129
+ } else {
130
+ typeLinks . push ( `<span class="type"><${ typeTextFull } ></span>` ) ;
131
+ }
132
+ } else {
133
+ throw new Error ( `Empty type slot: ${ typeInput } ` ) ;
134
+ }
135
+ } ) ;
136
+
137
+ return typeLinks . length ? typeLinks . join ( ' | ' ) : typeInput ;
138
+ }
139
+
140
+ module . exports = { toLink } ;
0 commit comments