util.ts 1.11 KB
import { File } from './api/file';

export class Util {

    static getNameHash(path:string) {
        for(var r=0,i=0;i<path.length;i++) {
            r=(r<<5)-r+path.charCodeAt(i),r&=r;
        }
        return "bsync_" + Math.abs(r);
    }

    /**
     *  index >= 0, localFile is in files
     *  index < 0, localFile is not in files
     */
    static getFileIndex(files:Array<File>, localFile:string) : number {
        for (let i = 0; i < files.length; i++) {
            if (localFile == files[i].target) {
                return i;
            }
        }
        return -1;
    }

    static getFilesForCleanup(files:Array<File>, localFiles:Array<string>) : Array<string> {
        let filesForCleanup = [];

        for (let localFile of localFiles) {
            let index = -1;

            index = Util.getFileIndex(files, localFile);
                    
            if (index < 0) {
                filesForCleanup.push(localFile);
            } else {
                // splice for performance improvement only
                files.splice(index,1);
            }
        }

        return filesForCleanup;
    }

}