UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

68 lines (67 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FilePath = void 0; var Uri_1 = require("../Uri"); var Url_1 = require("../Url"); exports.FilePath = { fromUrl: function (input) { input = (input || '').trim(); var parsed = Url_1.Url.parse(input); var parts = parsed.path.replace(/^\/*/, '').split('/'); var path = ''; var dir = ''; var filename = ''; var error = ''; if (!Uri_1.Uri.is.cell(parts[0])) { error = 'The path does not start with a cell URI'; } if (!error && parts[1] !== 'fs') { error = 'not a file-system path (eg "/cell:foo:A1/fs/filename")'; } if (!error && parts.length > 2) { parts = parts.slice(2); path = parts.join('/'); dir = parts.slice(0, parts.length - 1).join('/'); filename = parts[parts.length - 1]; } return { ok: !error, path: path, dir: dir, filename: filename, error: error ? error : undefined, toString: function () { return parsed.path; }, }; }, Local: { toAbsolutePath: function (args) { var root = Clean.root(args.root); var path = Clean.path(args.path, root); return "".concat(root, "/").concat(path); }, toRelativePath: function (args) { var root = Clean.root(args.root); var path = Clean.path(args.path, root); return "~/".concat(path); }, toAbsoluteLocation: function (args) { return "file://".concat(exports.FilePath.Local.toAbsolutePath(args)); }, toRelativeLocation: function (args) { return "file://".concat(exports.FilePath.Local.toRelativePath(args)); }, }, }; var Clean = { root: function (path) { return (path !== null && path !== void 0 ? path : '').trim().replace(/\/*$/, ''); }, path: function (path, root) { return (path !== null && path !== void 0 ? path : '') .trim() .replace(/^file:\/\//, '') .replace(new RegExp("^".concat(root)), '') .replace(/^~\//, '') .replace(/^\/*/, ''); }, };