All files Path.js

100% Statements 33/33
100% Branches 12/12
100% Functions 9/9
100% Lines 33/33
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        1005x 1005x   1005x 1005x 1005x   1005x 964x 964x 964x 964x     1005x 1005x   1579x   1578x 1x 1x   1577x 1576x   1x 1x         1005x                 3767x       291x       24x       820x 820x 820x 770x   820x 108x   820x       2556x       1x      
import Part from './Part.js';
 
export default class Path extends Part {
    constructor (rawPath) {
        super(rawPath);
        rawPath = this._value;
 
        let isAbsolute = false;
        let hasTrailingSlash = false;
        let components = [];
 
        if (rawPath) {
            isAbsolute = rawPath[0] === '/';
            hasTrailingSlash = rawPath.length > 1 && rawPath[rawPath.length - 1] === '/';
            components = rawPath.split('/');
            components = components.slice(+isAbsolute, -hasTrailingSlash + components.length);
        }
 
        let depthLevel = 0;
        components = components
            .reverse()
            .filter((component) => component !== '.')
            .filter((component) => {
                if (component === '..') {
                    depthLevel++;
                    return false;
                }
                if (depthLevel === 0) {
                    return true;
                }
                depthLevel = Math.max(depthLevel - 1, 0);
                return false;
            })
            .reverse()
            .map(decodeURIComponent);
 
        this._value = {
            isAbsolute,
            hasTrailingSlash,
            rawPath,
            components
        };
    }
 
    isAbsolute () {
        return this._value.isAbsolute;
    }
 
    hasTrailingSlash () {
        return this._value.hasTrailingSlash;
    }
 
    isEmpty () {
        return this._value.rawPath === null;
    }
 
    toString () {
        const value = this._value;
        let rawPath = value.components.map(encodeURIComponent).join('/');
        if (value.isAbsolute) {
            rawPath = '/' + rawPath;
        }
        if (value.hasTrailingSlash) {
            rawPath += '/';
        }
        return rawPath;
    }
 
    toArray () {
        return this._value.components;
    }
 
    toLowerCase () {
        return this;
    }
}