JSFiddle - React, Tailwind, and code Playground

by adelura

JavaScript

class Path {
    constructor(path) {
        if (Array.isArray(path)) {
            this._path = path.slice();
        } else if (typeof path === "string") {
            this._path = path.split(".");
        } else {
            return path;
        }
    }

    push(key) {
        return new Path(this._path.concat(key));
    }

    pop() {
        let result = this._path.slice();
        result.pop();
        return new Path(result);
    }

    shift() {
        let result = this._path.slice();
        result.shift();
        return new Path(result);
    }

    toString() {
        return this.pathString;
    }

    get pathString() {
        return this._path.join(".");
    }

    get lastKey() {
        this._path[this._path.length - 1];
    }
}