MCPcopy Index your code
hub / github.com/codeaashu/claude-code / sortMatchersByPriority

Function sortMatchersByPriority

src/utils/hooks/hooksSettings.ts:230–271  ·  view source on GitHub ↗
(
  matchers: string[],
  hooksByEventAndMatcher: Record<
    string,
    Record<string, IndividualHookConfig[]>
  >,
  selectedEvent: HookEvent,
)

Source from the content-addressed store, hash-verified

228}
229
230export function sortMatchersByPriority(
231 matchers: string[],
232 hooksByEventAndMatcher: Record<
233 string,
234 Record<string, IndividualHookConfig[]>
235 >,
236 selectedEvent: HookEvent,
237): string[] {
238 // Create a priority map based on SOURCES order (lower index = higher priority)
239 const sourcePriority = SOURCES.reduce(
240 (acc, source, index) => {
241 acc[source] = index
242 return acc
243 },
244 {} as Record<EditableSettingSource, number>,
245 )
246
247 return [...matchers].sort((a, b) => {
248 const aHooks = hooksByEventAndMatcher[selectedEvent]?.[a] || []
249 const bHooks = hooksByEventAndMatcher[selectedEvent]?.[b] || []
250
251 const aSources = Array.from(new Set(aHooks.map(h => h.source)))
252 const bSources = Array.from(new Set(bHooks.map(h => h.source)))
253
254 // Sort by highest priority source first (lowest priority number)
255 // Plugin hooks get lowest priority (highest number)
256 const getSourcePriority = (source: HookSource) =>
257 source === 'pluginHook' || source === 'builtinHook'
258 ? 999
259 : sourcePriority[source as EditableSettingSource]
260
261 const aHighestPriority = Math.min(...aSources.map(getSourcePriority))
262 const bHighestPriority = Math.min(...bSources.map(getSourcePriority))
263
264 if (aHighestPriority !== bHighestPriority) {
265 return aHighestPriority - bHighestPriority
266 }
267
268 // If same priority, sort by matcher name
269 return a.localeCompare(b)
270 })
271}
272

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected