| 38 | }; |
| 39 | |
| 40 | const calculateWindow = (): { start: number; end: number } => { |
| 41 | if (stringValue.length <= windowSize) { |
| 42 | return { start: 0, end: stringValue.length }; |
| 43 | } |
| 44 | |
| 45 | const largestMatch = matchingIndices.reduce( |
| 46 | (largestMatch, match) => { |
| 47 | if (match.end - match.start > largestMatch.end - largestMatch.start) { |
| 48 | return match; |
| 49 | } |
| 50 | |
| 51 | return largestMatch; |
| 52 | }, |
| 53 | { start: 0, end: 0 } |
| 54 | ); |
| 55 | |
| 56 | const largestMatchLength = largestMatch.end - largestMatch.start; |
| 57 | |
| 58 | const start = |
| 59 | largestMatch.start - Math.floor(windowSize / 2 - largestMatchLength / 2); |
| 60 | const end = |
| 61 | largestMatch.end + Math.floor(windowSize / 2 - largestMatchLength / 2); |
| 62 | |
| 63 | return { |
| 64 | start: Math.max(start, 0), |
| 65 | end: Math.min(end, stringValue.length), |
| 66 | }; |
| 67 | }; |
| 68 | |
| 69 | const window = calculateWindow(); |
| 70 | |