| 204 | export function sortFiles<T extends { _id: string }>(files: T[], key?: '_id'): T[]; |
| 205 | export function sortFiles<T extends Record<string, any>>(files: T[], key: StringKeys<T>): T[]; |
| 206 | export 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 | |
| 230 | export const getAlphabeticId = (() => { |
| 231 | // A...Z, AA...AZ, BA...BZ, ... |