All files / rusted-chromium/utils sort.utils.ts

100% Statements 36/36
100% Branches 4/4
100% Functions 17/17
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 6719x   19x           19x 39x   39x 25x   14x 12x     2x           19x 31x   31x 17x   14x 12x     2x           19x         24x         19x 23x   16x 4x     35x 6x     2x 2x        
import { ComparableVersion } from '../commons/ComparableVersion'
import type { MappedVersion } from '../commons/MappedVersion'
import { Compared } from '../interfaces/enums'
import type { IListStore } from '../interfaces/store.interfaces'
 
/**
 * Ascending sort comparator for ComparableVersion 
 */
export function sortAscendingComparableVersions(a: ComparableVersion, b: ComparableVersion): -1 | 0 | 1 {
    const compared = ComparableVersion.compare(a, b)
 
    if (compared === Compared.GREATER) {
        return 1
    }
    if (compared === Compared.LESS) {
        return -1
    }
 
    return 0
}
 
/**
 * Descending sort comparator for ComparableVersion 
 */
export function sortDescendingComparableVersions(a: ComparableVersion, b: ComparableVersion): -1 | 0 | 1 {
    const compared = ComparableVersion.compare(a, b)
 
    if (compared === Compared.LESS) {
        return 1
    }
    if (compared === Compared.GREATER) {
        return -1
    }
 
    return 0
}
 
/**
 * Ascending sort comparator for MappedVersion
 */
export const sortAscendingMappedVersions = (a: MappedVersion, b: MappedVersion): -1 | 0 | 1 => sortAscendingComparableVersions(a.comparable, b.comparable)
 
/**
* Descending sort comparator for MappedVersion
*/
export const sortDescendingMappedVersions = (a: MappedVersion, b: MappedVersion): -1 | 0 | 1 => sortDescendingComparableVersions(a.comparable, b.comparable)
 
/**
* Immutally sorts the entries of the given Store
*/
export function sortStoreEntries(store: IListStore): IListStore {
    return {
        win: {
            x64: [...store.win.x64].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
            x86: [...store.win.x86].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
        },
        linux: {
            x64: [...store.linux.x64].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
            x86: [...store.linux.x86].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
        },
        mac: {
            x64: [...store.mac.x64].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
            arm: [...store.mac.arm].map(v => new ComparableVersion(v)).sort(sortAscendingComparableVersions).map(c => c.toString()),
        },
    }
}