xref: /expo/packages/@expo/fingerprint/src/Sort.ts (revision dbfb9e4d)
1import type { HashSource } from './Fingerprint.types';
2
3export function sortSources<T extends HashSource>(sources: T[]): T[] {
4  const typeOrder = {
5    file: 0,
6    dir: 1,
7    contents: 2,
8  };
9  return sources.sort((a, b) => {
10    const typeResult = typeOrder[a.type] - typeOrder[b.type];
11    if (typeResult === 0) {
12      if (a.type === 'file' && b.type === 'file') {
13        return a.filePath.localeCompare(b.filePath);
14      } else if (a.type === 'dir' && b.type === 'dir') {
15        return a.filePath.localeCompare(b.filePath);
16      } else if (a.type === 'contents' && b.type === 'contents') {
17        return a.id.localeCompare(b.id);
18      }
19    }
20    return typeResult;
21  });
22}
23