UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

40 lines (39 loc) 1.3 kB
import { isPathActive, isPathProud, requirePath } from "../util/path.js"; import { BusyStore } from "./BusyStore.js"; /** * Store an absolute path, e.g. `/a/b/c` * * @param path: The initial value for the store. * @param base: The base path to resolve relative paths against. */ export class PathStore extends BusyStore { base; // Override to set default path to `.` and base to `/` constructor(path = ".", base = "/") { super(requirePath(path, base, PathStore)); this.base = base; } // Override to convert a possible path to an absolute path (relative to `this.base`). _convert(possible, caller) { return requirePath(possible, this.base, caller); } // Override for fast equality. _equal(a, b) { return a === b; } /** Based on the current store path, is a path active? */ isActive(path) { return isPathActive(this.value, path); } /** Based on the current store path, is a path proud (i.e. a child of the current store path)? */ isProud(path) { return isPathProud(this.value, path); } /** Get an absolute path from a path relative to the current store path. */ getPath(path) { return requirePath(path, this.value); } toString() { return this.value; } }