MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / getComponentSlices

Function getComponentSlices

app/utilities/search.ts:187–422  ·  view source on GitHub ↗
(
  path: string,
  matchingIndices: ReadonlyArray<{ start: number; end: number }>,
  maxWeight: number
)

Source from the content-addressed store, hash-verified

185// 3. We try to get the final weight to be as close to the maxWeight as possible
186//
187export 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" } },

Callers 2

SearchPathResultFunction · 0.90
search.test.tsFile · 0.90

Calls 4

createComponentSlicesFunction · 0.85
calculateWeightFunction · 0.85
calculateLongestMatchFunction · 0.85
addEllipsisToSlicesFunction · 0.85

Tested by

no test coverage detected