UNPKG

@dbs-portal/core-module-registry

Version:

Core module registry system for automatic module discovery and registration

166 lines 5.35 kB
/** * Browser-compatible path utilities * * Provides path manipulation functions that work in browser environments * while maintaining compatibility with Node.js path module API. */ class BrowserPath { sep = '/'; delimiter = ':'; /** * Resolve path segments into an absolute path */ resolve(...paths) { let resolvedPath = ''; let resolvedAbsolute = false; for (let i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) { const path = i >= 0 ? paths[i] : this.getCurrentDirectory(); if (!path) continue; resolvedPath = path + '/' + resolvedPath; resolvedAbsolute = this.isAbsolute(path); } // Normalize the path resolvedPath = this.normalizeArray(resolvedPath.split('/').filter(Boolean), !resolvedAbsolute).join('/'); return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; } /** * Join path segments */ join(...paths) { const joined = paths.filter(Boolean).join('/'); return joined ? this.normalize(joined) : '.'; } /** * Get directory name */ dirname(path) { if (!path) return '.'; const normalizedPath = this.normalize(path); const lastSlash = normalizedPath.lastIndexOf('/'); if (lastSlash === -1) return '.'; if (lastSlash === 0) return '/'; return normalizedPath.slice(0, lastSlash); } /** * Get base name */ basename(path, ext) { if (!path) return ''; const normalizedPath = this.normalize(path); const lastSlash = normalizedPath.lastIndexOf('/'); let base = lastSlash === -1 ? normalizedPath : normalizedPath.slice(lastSlash + 1); if (ext && base.endsWith(ext)) { base = base.slice(0, -ext.length); } return base; } /** * Get file extension */ extname(path) { if (!path) return ''; const base = this.basename(path); const lastDot = base.lastIndexOf('.'); return lastDot === -1 || lastDot === 0 ? '' : base.slice(lastDot); } /** * Get relative path */ relative(from, to) { const fromParts = this.resolve(from).split('/').filter(Boolean); const toParts = this.resolve(to).split('/').filter(Boolean); let commonLength = 0; const minLength = Math.min(fromParts.length, toParts.length); for (let i = 0; i < minLength; i++) { if (fromParts[i] === toParts[i]) { commonLength++; } else { break; } } const upCount = fromParts.length - commonLength; const relativeParts = Array(upCount).fill('..').concat(toParts.slice(commonLength)); return relativeParts.join('/') || '.'; } /** * Check if path is absolute */ isAbsolute(path) { return path.startsWith('/'); } /** * Normalize path */ normalize(path) { if (!path) return '.'; const isAbsolute = this.isAbsolute(path); const trailingSlash = path.endsWith('/'); const parts = path.split('/').filter(Boolean); const normalizedParts = this.normalizeArray(parts, !isAbsolute); let result = normalizedParts.join('/'); if (!result && !isAbsolute) { result = '.'; } if (result && trailingSlash) { result += '/'; } return (isAbsolute ? '/' : '') + result; } /** * Normalize path array */ normalizeArray(parts, allowAboveRoot) { const result = []; for (const part of parts) { if (part === '..') { if (result.length && result[result.length - 1] !== '..') { result.pop(); } else if (allowAboveRoot) { result.push('..'); } } else if (part !== '.') { result.push(part); } } return result; } /** * Get current directory (browser simulation) */ getCurrentDirectory() { if (typeof window !== 'undefined') { // Browser environment - use current URL path return window.location.pathname; } // Fallback return '/'; } } // Create browser path instance const browserPath = new BrowserPath(); // Export individual functions for compatibility export const resolve = browserPath.resolve.bind(browserPath); export const join = browserPath.join.bind(browserPath); export const dirname = browserPath.dirname.bind(browserPath); export const basename = browserPath.basename.bind(browserPath); export const extname = browserPath.extname.bind(browserPath); export const relative = browserPath.relative.bind(browserPath); export const isAbsolute = browserPath.isAbsolute.bind(browserPath); export const normalize = browserPath.normalize.bind(browserPath); export const sep = browserPath.sep; export const delimiter = browserPath.delimiter; // Export default object for compatibility export default browserPath; // Export the class for advanced usage export { BrowserPath }; //# sourceMappingURL=browser-path.js.map