|
| 1 | +'use strict'; |
| 2 | +const nodeDocUrl = ''; |
| 3 | +const jsDocUrl = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/' + |
| 4 | + 'Reference/Global_Objects/'; |
| 5 | +const jsPrimitiveUrl = 'https://developer.mozilla.org/en-US/docs/Web/' + |
| 6 | + 'JavaScript/Data_structures'; |
| 7 | +const jsPrimitives = [ |
| 8 | + 'Number', 'String', 'Boolean', 'Null', 'Symbol' |
| 9 | +] |
| 10 | +const jsGlobalTypes = [ |
| 11 | + 'Error', 'Object', 'Function', 'Array', 'Uint8Array', |
| 12 | + 'Uint16Array', 'Uint32Array', 'Int8Array', 'Int16Array', 'Int32Array', |
| 13 | + 'Uint8ClampedArray', 'Float32Array', 'Float64Array', 'Date', 'RegExp', |
| 14 | + 'ArrayBuffer', 'DataView', 'Promise' |
| 15 | +]; |
| 16 | +const typeMap = { |
| 17 | + 'Buffer': 'buffer.html#buffer_class_buffer', |
| 18 | + 'Handle': 'net.html#net_server_listen_handle_backlog_callback', |
| 19 | + 'Stream': 'stream.html#stream_stream', |
| 20 | + 'stream.Writable': 'stream.html#stream_class_stream_writable', |
| 21 | + 'stream.Readable': 'stream.html#stream_class_stream_readable', |
| 22 | + 'ChildProcess': 'child_process.html#child_process_class_childprocess', |
| 23 | + 'cluster.Worker': 'cluster.html#cluster_class_worker', |
| 24 | + 'dgram.Socket': 'dgram.html#dgram_class_dgram_socket', |
| 25 | + 'net.Socket': 'net.html#net_class_net_socket', |
| 26 | + 'EventEmitter': 'events.html#events_class_events_eventemitter', |
| 27 | + 'Timer': 'timers.html#timers_timers' |
| 28 | +}; |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + toLink: function (typeInput) { |
| 32 | + let typeLinks = []; |
| 33 | + typeInput = typeInput.replace('{', '').replace('}', ''); |
| 34 | + let typeTexts = typeInput.split('|'); |
| 35 | + |
| 36 | + typeTexts.forEach(function (typeText) { |
| 37 | + typeText = typeText.trim(); |
| 38 | + if (typeText) { |
| 39 | + let typeUrl = null; |
| 40 | + if (jsPrimitives.indexOf(typeText) !== -1) { |
| 41 | + typeUrl = jsPrimitiveUrl + '#' + typeText + '_type'; |
| 42 | + } else if (jsGlobalTypes.indexOf(typeText) !== -1) { |
| 43 | + typeUrl = jsDocUrl + typeText; |
| 44 | + } else if (typeMap[typeText]) { |
| 45 | + typeUrl = nodeDocUrl + typeMap[typeText]; |
| 46 | + } |
| 47 | + |
| 48 | + if (typeUrl) { |
| 49 | + typeLinks.push('<a href="' + typeUrl + '" class="type"><' + |
| 50 | + typeText + '></a>'); |
| 51 | + } else { |
| 52 | + typeLinks.push('<span class="type"><' + typeText + '></span>'); |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + return typeLinks.length ? typeLinks.join(' | ') : typeInput; |
| 58 | + } |
| 59 | +} |
0 commit comments