(
path: string,
matchingIndices: ReadonlyArray<{ start: number; end: number }>,
maxWeight: number
)
| 185 | // 3. We try to get the final weight to be as close to the maxWeight as possible |
| 186 | // |
| 187 | export function getComponentSlices( |
| 188 | path: string, |
| 189 | matchingIndices: ReadonlyArray<{ start: number; end: number }>, |
| 190 | maxWeight: number |
| 191 | ): Array<PathSlice> { |
| 192 | const calculateWeight = (pathSlices: PathSlice[]): number => { |
| 193 | let weight = 0; |
| 194 | |
| 195 | for (const slice of pathSlices) { |
| 196 | weight += calculateSliceWeight(slice); |
| 197 | } |
| 198 | |
| 199 | return weight; |
| 200 | }; |
| 201 | |
| 202 | const calculateSliceWeight = (slice: PathSlice): number => { |
| 203 | if (slice.type === "component") { |
| 204 | return slice.slice.slice.length; |
| 205 | } else if (slice.type === "ellipsis") { |
| 206 | return 2; |
| 207 | } else { |
| 208 | return 8; |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | const calculateLongestMatch = (componentSlices: ComponentSlice[]): number => { |
| 213 | return componentSlices.reduce( |
| 214 | (longestMatch, slice) => |
| 215 | slice.slice.isMatch |
| 216 | ? Math.max(longestMatch, slice.slice.slice.length) |
| 217 | : longestMatch, |
| 218 | 0 |
| 219 | ); |
| 220 | }; |
| 221 | |
| 222 | const addEllipsisToSlices = ( |
| 223 | slices: PathSlice[], |
| 224 | mostImportantComponentIndex: number |
| 225 | ): PathSlice[] => { |
| 226 | // This should take an array of slices like this: |
| 227 | // [ |
| 228 | // { type: "component", slice: { isMatch: true, slice: "re" } }, |
| 229 | // { type: "component", slice: { isMatch: false, slice: "cords" } }, |
| 230 | // { type: "join" }, |
| 231 | // { type: "ellipsis" }, |
| 232 | // { type: "join" }, |
| 233 | // { type: "ellipsis" }, |
| 234 | // { type: "ellipsis" }, |
| 235 | // { type: "join" }, |
| 236 | // { type: "component", slice: { isMatch: true, slice: "street" } }, |
| 237 | // { type: "component", slice: { isMatch: false, slice: "_address" } }, |
| 238 | // ] |
| 239 | // |
| 240 | // |
| 241 | // And should return this: |
| 242 | // [ |
| 243 | // { type: "component", slice: { isMatch: true, slice: "re" } }, |
| 244 | // { type: "component", slice: { isMatch: false, slice: "cords" } }, |
no test coverage detected