(itemsArr)
| 319 | * @returns {string} regexp |
| 320 | */ |
| 321 | const itemsToRegexp = (itemsArr) => { |
| 322 | if (itemsArr.length === 1) { |
| 323 | return quoteMeta(itemsArr[0]); |
| 324 | } |
| 325 | |
| 326 | /** @type {string[]} */ |
| 327 | const finishedItems = []; |
| 328 | |
| 329 | // merge single char items: (a|b|c|d|ef) => ([abcd]|ef) |
| 330 | let countOfSingleCharItems = 0; |
| 331 | |
| 332 | for (const item of itemsArr) { |
| 333 | if (item.length === 1) { |
| 334 | countOfSingleCharItems++; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // special case for only single char items |
| 339 | if (countOfSingleCharItems === itemsArr.length) { |
| 340 | return `[${quoteMeta(itemsArr.sort().join(""))}]`; |
| 341 | } |
| 342 | |
| 343 | const items = new Set(itemsArr.sort()); |
| 344 | |
| 345 | if (countOfSingleCharItems > 2) { |
| 346 | let singleCharItems = ""; |
| 347 | for (const item of items) { |
| 348 | if (item.length === 1) { |
| 349 | singleCharItems += item; |
| 350 | items.delete(item); |
| 351 | } |
| 352 | } |
| 353 | finishedItems.push(`[${quoteMeta(singleCharItems)}]`); |
| 354 | } |
| 355 | |
| 356 | // special case for 2 items with common prefix/suffix |
| 357 | if (finishedItems.length === 0 && items.size === 2) { |
| 358 | const prefix = getCommonPrefix(itemsArr); |
| 359 | const suffix = getCommonSuffix( |
| 360 | itemsArr.map((item) => item.slice(prefix.length)), |
| 361 | ); |
| 362 | |
| 363 | if (prefix.length > 0 || suffix.length > 0) { |
| 364 | return `${quoteMeta(prefix)}${itemsToRegexp( |
| 365 | itemsArr.map((i) => |
| 366 | i.slice(prefix.length, -suffix.length || undefined), |
| 367 | ), |
| 368 | )}${quoteMeta(suffix)}`; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // special case for 2 items with common suffix |
| 373 | if (finishedItems.length === 0 && items.size === 2) { |
| 374 | /** @type {Iterator<string>} */ |
| 375 | const it = items[Symbol.iterator](); |
| 376 | const a = it.next().value; |
| 377 | const b = it.next().value; |
| 378 | if (a.length > 0 && b.length > 0 && a.slice(-1) === b.slice(-1)) { |
no test coverage detected
searching dependent graphs…