@pulzar/core
Version:
Next-generation Node.js framework for ultra-fast web applications with zero-reflection DI, GraphQL, WebSockets, events, and edge runtime support
139 lines • 3.7 kB
JavaScript
import { join, resolve, dirname, basename, extname } from "path";
/**
* Normalize path separators for cross-platform compatibility
*/
export function normalizePath(path) {
return path.replace(/\\/g, "/");
}
/**
* Join paths with proper normalization
*/
export function joinPaths(...paths) {
return normalizePath(join(...paths));
}
/**
* Resolve path with proper normalization
*/
export function resolvePath(...paths) {
return normalizePath(resolve(...paths));
}
/**
* Get directory name with proper normalization
*/
export function getDirName(path) {
return normalizePath(dirname(path));
}
/**
* Get base name with proper normalization
*/
export function getBaseName(path, ext) {
return basename(path, ext);
}
/**
* Get file extension
*/
export function getExtension(path) {
return extname(path);
}
/**
* Check if path is absolute
*/
export function isAbsolutePath(path) {
return path.startsWith("/") || /^[A-Z]:/i.test(path);
}
/**
* Make path relative to base directory
*/
export function makeRelative(base, path) {
const normalizedBase = resolvePath(base);
const normalizedPath = resolvePath(path);
if (normalizedPath.startsWith(normalizedBase)) {
return normalizePath(normalizedPath.slice(normalizedBase.length + 1));
}
return normalizedPath;
}
/**
* Ensure path ends with separator
*/
export function ensureTrailingSlash(path) {
return path.endsWith("/") ? path : path + "/";
}
/**
* Remove trailing slash from path
*/
export function removeTrailingSlash(path) {
return path.endsWith("/") ? path.slice(0, -1) : path;
}
/**
* Get common prefix from array of paths
*/
export function getCommonPrefix(paths) {
if (paths.length === 0)
return "";
if (paths.length === 1)
return paths[0] ?? "";
const normalizedPaths = paths.map(normalizePath);
const first = normalizedPaths[0] ?? "";
let commonPrefix = "";
for (let i = 0; i < first.length; i++) {
const char = first[i];
if (normalizedPaths.every((path) => path[i] === char)) {
commonPrefix += char;
}
else {
break;
}
}
// Find the last directory separator in common prefix
const lastSlash = commonPrefix.lastIndexOf("/");
if (lastSlash > 0) {
return commonPrefix.slice(0, lastSlash + 1);
}
return commonPrefix;
}
/**
* Check if path matches pattern (supports glob-like patterns)
*/
export function matchesPattern(path, pattern) {
const normalizedPath = normalizePath(path);
const normalizedPattern = normalizePath(pattern);
// Simple glob pattern matching
const regex = new RegExp(normalizedPattern
.replace(/\./g, "\\.")
.replace(/\*/g, ".*")
.replace(/\?/g, "."));
return regex.test(normalizedPath);
}
/**
* Get all parent directories of a path
*/
export function getParentDirectories(path) {
const normalizedPath = normalizePath(path);
const parts = normalizedPath.split("/").filter(Boolean);
const parents = [];
for (let i = 1; i <= parts.length; i++) {
parents.push("/" + parts.slice(0, i).join("/"));
}
return parents;
}
/**
* Check if path is within base directory
*/
export function isWithinBase(base, path) {
const normalizedBase = resolvePath(base);
const normalizedPath = resolvePath(path);
return normalizedPath.startsWith(normalizedBase);
}
/**
* Convert Windows path to Unix style
*/
export function toUnixPath(path) {
return path.replace(/\\/g, "/");
}
/**
* Convert Unix path to Windows style
*/
export function toWindowsPath(path) {
return path.replace(/\//g, "\\");
}
//# sourceMappingURL=path.js.map