UNPKG

mahler

Version:

A automated task composer and HTN based planner for building autonomous system agents

75 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Path = exports.PathIsInvalid = void 0; function isPath(x) { return (x != null && typeof x === 'string' && (x.startsWith('/') || x === '') && /[-a-zA-Z0-9@:%._\\+~#?&/=]*/.test(x)); } // Escape slashes according to RFC 6901 function encode(p) { return p.replace(/~/g, '~0').replace(/\//g, '~1'); } function decode(s) { return s.replace(/~1/g, '/').replace(/~0/g, '~'); } function split(p) { return p .slice(1) .split('/') .filter((s) => s.length > 0) .map(decode); } class PathIsInvalid extends Error { constructor(path) { super(`Path ${path} is not a valid path`); } } exports.PathIsInvalid = PathIsInvalid; function assert(p) { if (!isPath(p)) { throw new PathIsInvalid(p); } } function from(p) { const res = Array.isArray(p) ? '/' + p.map(encode).join('/') : p; assert(res); return res; } function join(p, s) { return from(split(p).concat(s)); } /** * Return the source (parent) of the path * * e.g. * ``` * Path.source(Path.from('/a/b/c')) // '/a/b' * ``` */ function source(p) { const parts = split(p); parts.pop(); return from(parts); } /** * Return the path base name * * e.g. * ``` * Path.basename(Path.from('/a/b/c')) // 'a' * ``` */ function basename(p) { const parts = split(p); return parts.pop() ?? ''; } exports.Path = { from, split, join, source, basename, }; //# sourceMappingURL=path.js.map