(
path: string,
matchingIndices: ReadonlyArray<{ start: number; end: number }>
)
| 422 | } |
| 423 | |
| 424 | export function createComponentSlices( |
| 425 | path: string, |
| 426 | matchingIndices: ReadonlyArray<{ start: number; end: number }> |
| 427 | ): Array<PathSlice> { |
| 428 | const slices: PathSlice[] = []; |
| 429 | |
| 430 | const addComponent = (slice: StringSlice, componentIndex: number) => { |
| 431 | slices.push({ type: "component", componentIndex, slice }); |
| 432 | }; |
| 433 | |
| 434 | const addJoin = () => { |
| 435 | slices.push({ type: "join" }); |
| 436 | }; |
| 437 | |
| 438 | const addEllipsis = () => { |
| 439 | slices.push({ type: "ellipsis" }); |
| 440 | }; |
| 441 | |
| 442 | const components = path.split("."); |
| 443 | |
| 444 | let currentIndex = 0; |
| 445 | let currentComponentIndex = 0; |
| 446 | |
| 447 | for (const component of components) { |
| 448 | if (currentComponentIndex !== 0) { |
| 449 | // Adds the "." |
| 450 | currentIndex += 1; |
| 451 | } |
| 452 | |
| 453 | const endIndex = currentIndex + component.length; |
| 454 | |
| 455 | // Example matchingIndices = [{ start: 0, end: 2 }, { start: 6, end: 11 }, { start: 12, end: 21 }] |
| 456 | // currentIndex = 7 |
| 457 | // endIndex = 7 + 6 = 13 |
| 458 | const intersectingMatches = matchingIndices |
| 459 | .filter( |
| 460 | ({ start, end }) => |
| 461 | (currentIndex >= start && currentIndex <= end) || |
| 462 | (endIndex >= start && endIndex <= end) || |
| 463 | (start >= currentIndex && end <= endIndex) |
| 464 | ) |
| 465 | .map(({ start, end }) => ({ |
| 466 | start: Math.max(start - currentIndex, 0), |
| 467 | end: end - currentIndex, |
| 468 | })); |
| 469 | |
| 470 | const stringSlices = getStringSlices( |
| 471 | component, |
| 472 | intersectingMatches, |
| 473 | component.length + 1 |
| 474 | ); |
| 475 | |
| 476 | for (const stringSlice of stringSlices) { |
| 477 | addComponent(stringSlice, currentComponentIndex); |
| 478 | } |
| 479 | |
| 480 | if (currentComponentIndex + 1 < components.length) { |
| 481 | addJoin(); |
no test coverage detected