All files / src/components/pages/filesytem sortFilesAndFolders.ts

0% Statements 0/32
0% Branches 0/36
0% Functions 0/2
0% Lines 0/25

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                                                                                                 
import {FsEntity, PermissionSet} from "../../../background/api/filesystemTypes";
 
function mergeObjectArraysByProperty(leftArray: any, rightArray: any, propertyName: keyof FsEntity, sortIncreasing: boolean): FsEntity[] {
    let arr: any = []
    // Break out of loop if any one of the array gets empty
    while (leftArray.length && rightArray.length) {
        // Pick the smaller among the smallest element of left and right sub arrays
 
        if (rightArray[0] === undefined || rightArray[0][propertyName] === undefined || !rightArray[0].hasOwnProperty(propertyName)) {
            let help: FsEntity | undefined = leftArray.shift();
            if (help) arr.push(help)
            continue;
        } else if (leftArray[0] === undefined || leftArray[0][propertyName] === undefined || !leftArray[0].hasOwnProperty(propertyName)) {
            let help: (FsEntity) | undefined = rightArray.shift();
            if (help) arr.push(help)
            continue;
        }
 
        //at this point both elements should have property
        let firstLeftElement: string | number | boolean | PermissionSet | undefined = leftArray[0][propertyName];
        let firstRightElement: string | number | boolean | PermissionSet | undefined = rightArray[0][propertyName];
        if (typeof firstLeftElement === "string") firstLeftElement = firstLeftElement.toLowerCase();
        if (typeof firstRightElement === "string") firstRightElement = firstRightElement.toLowerCase();
        if (
            (firstLeftElement !== undefined && firstRightElement !== undefined && firstLeftElement <= firstRightElement && sortIncreasing)
        ) {
            let help: (FsEntity) | undefined = leftArray.shift();
            if (help) arr.push(help)
        } else {
            let help: (FsEntity) | undefined = rightArray.shift();
            if (help) arr.push(help)
        }
    }
 
    // Concatenating leftover elements
    return [...arr, ...leftArray, ...rightArray]
}
 
export function sortObjectsInArrayByProperty(originalArray: any, propertyName: keyof FsEntity, sortIncreasing: boolean): any {
    const array = [...originalArray]
    if (!array || array.length <= 1) return array ?? [];
 
    const half = array.length / 2
    const left = array.splice(0, half)
 
 
    return mergeObjectArraysByProperty(sortObjectsInArrayByProperty(left, propertyName, sortIncreasing), sortObjectsInArrayByProperty(array, propertyName, sortIncreasing), propertyName, sortIncreasing);
}