All files / rusted-chromium/store Store.ts

100% Statements 30/30
100% Branches 24/24
100% Functions 12/12
100% Lines 29/29

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132      17x 17x   17x         87x                                 32x       32x       36x 2x   34x 21x     13x 3x   10x 2x     8x 3x   5x 1x     4x             11x                     5x       5x                             5x               6x   6x       2x       2x       170x               5x              
import type { ComparableVersion } from '../commons/ComparableVersion'
import type { OS } from '../interfaces/os.interfaces'
import type { Arch, IListStore as IListStore, StoreSize, ISetStore } from '../interfaces/store.interfaces'
import { setStoreToListStore } from '../utils'
import { sortStoreEntries } from '../utils/sort.utils'
 
export class Store {
 
    private store: ISetStore
 
    public constructor(store_: IListStore) {
        this.store = {
            linux: {
                x64: new Set(store_.linux.x64),
                x86: new Set(store_.linux.x86),
            },
            win: {
                x64: new Set(store_.win.x64),
                x86: new Set(store_.win.x86),
            },
            mac: {
                x64: new Set(store_.mac.x64),
                arm: new Set(store_.mac.arm),
            },
        }
    }
 
    private getByOs<T extends keyof IListStore>(os: T): ISetStore[T] {
        return this.store[os]
    }
 
    private getByOsArch<O extends keyof IListStore, A extends keyof ISetStore[O]>(os: O, arch: A): ISetStore[O][A] {
        return this.getByOs<typeof os>(os)[arch]
    }
 
    public getBy(os: OS, arch: Arch): Set<string> {
        if (os === 'linux' && arch === 'x86') {
            return this.getByOsArch<'linux', 'x86'>(os, arch)
        }
        if (os === 'linux' && arch === 'x64') {
            return this.getByOsArch<'linux', 'x64'>(os, arch)
        }
 
        if (os === 'win' && arch === 'x64') {
            return this.getByOsArch<'win', 'x64'>(os, arch)
        }
        if (os === 'win' && arch === 'x86') {
            return this.getByOsArch<'win', 'x86'>(os, arch)
        }
 
        if (os === 'mac' && arch === 'x64') {
            return this.getByOsArch<'mac', 'x64'>(os, arch)
        }
        if (os === 'mac' && arch === 'arm') {
            return this.getByOsArch<'mac', 'arm'>(os, arch)
        }
 
        throw new Error(`Unsupported os/arch combination: ${os}/${arch}`)
    }
 
    /**
     * Returns whether the store contains the given version for the given os/arch combination
     */
    public has(os: OS, arch: Arch, version: ComparableVersion): boolean {
        return this.getBy(os, arch as Arch).has(version.toString())
    }
 
    /**
    * Merges an already existing localStore with a newStore.
    * Fluently returns itself
    */
    public merge(store: IListStore): Store
    public merge(store: Store): Store
    public merge(store: Store | IListStore): Store {
 
        const newStore = store instanceof Store
            ? store.store
            : store
 
        this.store = {
            win: {
                x64: new Set([...this.store.win.x64, ...newStore.win.x64]),
                x86: new Set([...this.store.win.x86, ...newStore.win.x86]),
            },
            linux: {
                x64: new Set([...this.store.linux.x64, ...newStore.linux.x64]),
                x86: new Set([...this.store.linux.x86, ...newStore.linux.x86]),
            },
            mac: {
                x64: new Set([...this.store.mac.x64, ...newStore.mac.x64]),
                arm: new Set([...this.store.mac.arm, ...newStore.mac.arm]),
            },
        }
 
        return this
    }
 
    /**
     * Adds a new version for an os and arch.
     * Fluently returns itself
     */
    public add(os: OS, arch: Arch, version: ComparableVersion): Store {
        this.getBy(os, arch).add(version.toString())
 
        return this
    }
 
    public toString(): string {
        return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)))
    }
 
    public toFormattedString(spaces = 4): string {
        return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), null, spaces)
    }
 
    public toMinimalFormattedString(spaces = 4): string {
        return JSON.stringify(sortStoreEntries(setStoreToListStore(this.store)), (k, v) => v instanceof Array ? JSON.stringify(v) : v, spaces)
            .replace(/"\[/g, '[')
            .replace(/\]"/g, ']')
            .replace(/\\"/g, '"')
            .replace(/""/g, '"')
    }
 
    public size(): StoreSize {
        return {
            linux: this.store.linux.x64.size + this.store.linux.x86.size,
            win: this.store.win.x64.size + this.store.win.x86.size,
            mac: this.store.mac.x64.size + this.store.mac.arm.size,
        }
    }
}