MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / sortFiles

Function sortFiles

framework/utils/lib/common.ts:206–228  ·  view source on GitHub ↗
(files: Record<string, any>[] | string[], key = '_id')

Source from the content-addressed store, hash-verified

204export function sortFiles<T extends { _id: string }>(files: T[], key?: '_id'): T[];
205export function sortFiles<T extends Record<string, any>>(files: T[], key: StringKeys<T>): T[];
206export function sortFiles(files: Record<string, any>[] | string[], key = '_id') {
207 if (!files?.length) return [];
208 const isString = typeof files[0] === 'string';
209 const result = files
210 .map((i: any) => (isString ? { name: i, _weights: i.match(fSortR) } : { ...i, _weights: (i[key] || i.name).match(fSortR) }))
211 .sort((a, b) => {
212 let pos = 0;
213 const weightsA = a._weights;
214 const weightsB = b._weights;
215 let weightA = weightsA[pos];
216 let weightB = weightsB[pos];
217 while (weightA && weightB) {
218 const v = weightA - weightB;
219 if (!Number.isNaN(v) && v !== 0) return v;
220 if (weightA !== weightB) return weightA > weightB ? 1 : -1;
221 pos += 1;
222 weightA = weightsA[pos];
223 weightB = weightsB[pos];
224 }
225 return weightA ? 1 : -1;
226 });
227 return result.map((x) => (isString ? x.name : (delete x._weights && x)));
228}
229
230export const getAlphabeticId = (() => {
231 // A...Z, AA...AZ, BA...BZ, ...

Callers 10

reducerFunction · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
getMethod · 0.90
readSubtasksFromFilesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected