|
| 1 | +interface ConnectorPairMatch { |
| 2 | + from: string; |
| 3 | + matchedText: string; |
| 4 | + to: string; |
| 5 | +} |
| 6 | + |
| 7 | +interface ConnectorTokenMatch { |
| 8 | + connector: string; |
| 9 | + matchedText: string; |
| 10 | + nextIndex: number; |
| 11 | +} |
| 12 | + |
| 13 | +const createConnectorTokenMatch = ( |
| 14 | + connector: string, |
| 15 | + matchedText: string, |
| 16 | + nextIndex: number |
| 17 | +): ConnectorTokenMatch => { |
| 18 | + return { |
| 19 | + connector, |
| 20 | + matchedText, |
| 21 | + nextIndex, |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +const tokenizeConnectorWords = (text: string): string[] => { |
| 26 | + const words: string[] = []; |
| 27 | + let current = ""; |
| 28 | + |
| 29 | + for (const character of text.toLowerCase()) { |
| 30 | + const isDigit = character >= "0" && character <= "9"; |
| 31 | + const isLowercaseLetter = character >= "a" && character <= "z"; |
| 32 | + |
| 33 | + if (isDigit || isLowercaseLetter || character === ".") { |
| 34 | + current += character; |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if (current.length > 0) { |
| 39 | + words.push(current); |
| 40 | + current = ""; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (current.length > 0) { |
| 45 | + words.push(current); |
| 46 | + } |
| 47 | + |
| 48 | + return words; |
| 49 | +}; |
| 50 | + |
| 51 | +const isDigitsOnly = (value: string): boolean => { |
| 52 | + if (value.length === 0) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + for (const character of value) { |
| 57 | + if (character < "0" || character > "9") { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | +}; |
| 64 | + |
| 65 | +const readLightningToken = ( |
| 66 | + words: string[], |
| 67 | + startIndex: number |
| 68 | +): ConnectorTokenMatch | null => { |
| 69 | + if (words[startIndex] !== "lightning") { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + return createConnectorTokenMatch("Lightning", "lightning", startIndex + 1); |
| 74 | +}; |
| 75 | + |
| 76 | +const readMicroUsbToken = ( |
| 77 | + words: string[], |
| 78 | + startIndex: number |
| 79 | +): ConnectorTokenMatch | null => { |
| 80 | + const current = words[startIndex]; |
| 81 | + if (current === "microusb") { |
| 82 | + return createConnectorTokenMatch("Micro-USB", "micro usb", startIndex + 1); |
| 83 | + } |
| 84 | + |
| 85 | + if (current === "micro" && words[startIndex + 1] === "usb") { |
| 86 | + return createConnectorTokenMatch("Micro-USB", "micro usb", startIndex + 2); |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | +}; |
| 91 | + |
| 92 | +const readCollapsedUsbToken = ( |
| 93 | + words: string[], |
| 94 | + startIndex: number |
| 95 | +): ConnectorTokenMatch | null => { |
| 96 | + const current = words[startIndex]; |
| 97 | + switch (current) { |
| 98 | + case "usbc": |
| 99 | + return createConnectorTokenMatch("USB-C", "usb c", startIndex + 1); |
| 100 | + case "usba": |
| 101 | + return createConnectorTokenMatch("USB-A", "usb a", startIndex + 1); |
| 102 | + case "usb3": |
| 103 | + case "usb3.0": |
| 104 | + return createConnectorTokenMatch("USB-A", "usb 3.0", startIndex + 1); |
| 105 | + default: |
| 106 | + return null; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +const readSplitUsbToken = ( |
| 111 | + words: string[], |
| 112 | + startIndex: number |
| 113 | +): ConnectorTokenMatch | null => { |
| 114 | + if (words[startIndex] !== "usb") { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + const next = words[startIndex + 1]; |
| 119 | + switch (next) { |
| 120 | + case "c": |
| 121 | + return createConnectorTokenMatch("USB-C", "usb c", startIndex + 2); |
| 122 | + case "a": |
| 123 | + return createConnectorTokenMatch("USB-A", "usb a", startIndex + 2); |
| 124 | + case "3": |
| 125 | + case "3.0": |
| 126 | + return createConnectorTokenMatch("USB-A", "usb 3.0", startIndex + 2); |
| 127 | + default: |
| 128 | + return null; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +const readThunderboltToken = ( |
| 133 | + words: string[], |
| 134 | + startIndex: number |
| 135 | +): ConnectorTokenMatch | null => { |
| 136 | + const current = words[startIndex]; |
| 137 | + if (!current?.startsWith("thunderbolt")) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + const suffix = current.slice("thunderbolt".length); |
| 142 | + if (isDigitsOnly(suffix)) { |
| 143 | + return createConnectorTokenMatch( |
| 144 | + "USB-C", |
| 145 | + `thunderbolt ${suffix}`, |
| 146 | + startIndex + 1 |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + const next = words[startIndex + 1]; |
| 151 | + if (next && isDigitsOnly(next)) { |
| 152 | + return createConnectorTokenMatch( |
| 153 | + "USB-C", |
| 154 | + `thunderbolt ${next}`, |
| 155 | + startIndex + 2 |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + return createConnectorTokenMatch("USB-C", "thunderbolt", startIndex + 1); |
| 160 | +}; |
| 161 | + |
| 162 | +const readConnectorToken = ( |
| 163 | + words: string[], |
| 164 | + startIndex: number |
| 165 | +): ConnectorTokenMatch | null => { |
| 166 | + return ( |
| 167 | + readLightningToken(words, startIndex) ?? |
| 168 | + readMicroUsbToken(words, startIndex) ?? |
| 169 | + readCollapsedUsbToken(words, startIndex) ?? |
| 170 | + readSplitUsbToken(words, startIndex) ?? |
| 171 | + readThunderboltToken(words, startIndex) |
| 172 | + ); |
| 173 | +}; |
| 174 | + |
| 175 | +export const collectNormalizedConnectors = (text: string): string[] => { |
| 176 | + const connectors = new Set<string>(); |
| 177 | + const words = tokenizeConnectorWords(text); |
| 178 | + |
| 179 | + for (let index = 0; index < words.length; index += 1) { |
| 180 | + const token = readConnectorToken(words, index); |
| 181 | + if (!token) { |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + connectors.add(token.connector); |
| 186 | + index = token.nextIndex - 1; |
| 187 | + } |
| 188 | + |
| 189 | + return [...connectors]; |
| 190 | +}; |
| 191 | + |
| 192 | +export const extractConnectorPairFromText = ( |
| 193 | + text: string |
| 194 | +): ConnectorPairMatch | null => { |
| 195 | + const words = tokenizeConnectorWords(text); |
| 196 | + |
| 197 | + for (let index = 0; index < words.length; index += 1) { |
| 198 | + const from = readConnectorToken(words, index); |
| 199 | + if (!from || words[from.nextIndex] !== "to") { |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + const to = readConnectorToken(words, from.nextIndex + 1); |
| 204 | + if (!to) { |
| 205 | + continue; |
| 206 | + } |
| 207 | + |
| 208 | + return { |
| 209 | + from: from.connector, |
| 210 | + matchedText: `${from.matchedText} to ${to.matchedText}`, |
| 211 | + to: to.connector, |
| 212 | + }; |
| 213 | + } |
| 214 | + |
| 215 | + return null; |
| 216 | +}; |
0 commit comments