UNPKG

fs-zoo

Version:

File system abstractions and implementations

34 lines (33 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tryUtf8Decode = exports.assertType = exports.assertName = exports.parseId = exports.parseParts = void 0; const parseParts = (path) => (!path ? [] : path.split('/').filter(Boolean)); exports.parseParts = parseParts; const parseId = (path) => { const collection = (0, exports.parseParts)(path); const resource = collection.pop() || ''; return [collection, resource]; }; exports.parseId = parseId; const nameRegex = /^(\.{1,2})$|^(.*([\/\\]).*)$/; const assertName = (name, method, klass) => { const isInvalid = !name || nameRegex.test(name); if (isInvalid) throw new TypeError(`Failed to execute '${method}' on '${klass}': Name is not allowed.`); }; exports.assertName = assertName; const assertType = (type, method, klass) => { const length = type.length; for (let i = 0; i < length; i++) (0, exports.assertName)(type[i], method, klass); }; exports.assertType = assertType; const tryUtf8Decode = (data) => { try { return new TextDecoder('utf-8', { fatal: true }).decode(data); } catch { return new Uint8Array(data); } }; exports.tryUtf8Decode = tryUtf8Decode;