All files / shared/firestore FirestorePath.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 7/7
100% Lines 15/15

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 431x             56x       46x 48x 1x 47x         44x 44x 44x       2x       2x 1x 1x       1x       27x      
export class FirestorePath {
 
    private readonly components: string[];
 
    public constructor(
        ...components: string[]
    ) {
        this.components = components;
    }
 
    public append(...components: string[]): void {
        for (const component of components) {
            if (component.includes('/'))
                throw new Error(`Invalid component: ${component}`);
            this.components.push(component);
        }
    }
 
    public appending(...components: string[]): FirestorePath {
        const path = new FirestorePath(...this.components);
        path.append(...components);
        return path;
    }
 
    public get isEmpty(): boolean {
        return this.components.length === 0;
    }
 
    public get parentPath(): FirestorePath {
        if (this.components.length === 0)
            throw new Error('Root path has no parent');
        return new FirestorePath(...this.components.slice(0, -1));
    }
 
    public get lastComponent(): string {
        return this.components[this.components.length - 1];
    }
 
    public get fullPath(): string {
        return this.components.join('/');
    }
}