upath
Version:
A drop-in replacement / proxy to Node.js path, replacing \\ with / for all results & adding file extension functions.
169 lines • 4.35 kB
JavaScript
// src/index.ts
import * as path from "path";
function isString(val) {
return typeof val === "string";
}
function toUnix(p) {
p = p.replace(/\\/g, "/");
p = p.replace(/(?<!^)\/+/g, "/");
return p;
}
var upath = {};
for (const [propName, propValue] of Object.entries(path)) {
if (propName === "posix" || propName === "win32") {
upath[propName] = propValue;
} else if (typeof propValue === "function") {
upath[propName] = (...args) => {
const mapped = args.map((a) => isString(a) ? toUnix(a) : a);
const result = propValue.apply(path, mapped);
return isString(result) ? toUnix(result) : result;
};
} else {
upath[propName] = propValue;
}
}
upath.sep = "/";
function isValidExt(ext, ignoreExts = [], maxSize) {
return !!ext && ext.length <= maxSize && !ignoreExts.map((e) => e && e[0] !== "." ? "." + e : e).includes(ext);
}
function normalizeSafe(p) {
p = toUnix(p);
let result = upath.normalize(p);
if (p.startsWith("./") && !result.startsWith("./") && !result.startsWith("..")) {
result = "./" + result;
} else if (p.startsWith("//") && !result.startsWith("//")) {
if (p.startsWith("//./")) {
result = "//." + result;
} else {
result = "/" + result;
}
}
return result;
}
function normalizeTrim(p) {
p = normalizeSafe(p);
if (p.endsWith("/") && p.length > 1) {
return p.slice(0, -1);
}
return p;
}
function joinSafe(...segments) {
const result = upath.join(...segments);
if (segments.length > 0) {
const p0 = toUnix(segments[0]);
if (p0.startsWith("./") && !result.startsWith("./") && !result.startsWith("..")) {
return "./" + result;
} else if (p0.startsWith("//") && !result.startsWith("//")) {
if (p0.startsWith("//./")) {
return "//." + result;
} else {
return "/" + result;
}
}
}
return result;
}
function addExt(file, ext) {
if (!ext) return file;
if (ext[0] !== ".") ext = "." + ext;
return file + (file.endsWith(ext) ? "" : ext);
}
function trimExt(filename, ignoreExts, maxSize) {
const _maxSize = maxSize ?? 7;
const _ignoreExts = ignoreExts ?? [];
const oldExt = upath.extname(filename);
if (isValidExt(oldExt, _ignoreExts, _maxSize)) {
return filename.slice(0, filename.length - oldExt.length);
}
return filename;
}
function removeExt(filename, ext) {
if (!ext) return filename;
ext = ext[0] === "." ? ext : "." + ext;
if (upath.extname(filename) === ext) {
return trimExt(filename, [], ext.length);
}
return filename;
}
function changeExt(filename, ext, ignoreExts, maxSize) {
const _maxSize = maxSize ?? 7;
const _ignoreExts = ignoreExts ?? [];
const trimmed = trimExt(filename, _ignoreExts, _maxSize);
if (!ext) return trimmed;
return trimmed + (ext[0] === "." ? ext : "." + ext);
}
function defaultExt(filename, ext, ignoreExts, maxSize) {
const _maxSize = maxSize ?? 7;
const _ignoreExts = ignoreExts ?? [];
const oldExt = upath.extname(filename);
if (isValidExt(oldExt, _ignoreExts, _maxSize)) {
return filename;
}
return addExt(filename, ext);
}
var extraFunctions = {
toUnix,
normalizeSafe,
normalizeTrim,
joinSafe,
addExt,
trimExt,
removeExt,
changeExt,
defaultExt
};
for (const [name, fn] of Object.entries(extraFunctions)) {
if (upath[name] !== void 0) {
throw new Error(`path.${name} already exists.`);
}
upath[name] = fn;
}
upath.VERSION = "3.0.6";
var resolve = upath.resolve;
var normalize = upath.normalize;
var isAbsolute = upath.isAbsolute;
var join = upath.join;
var relative = upath.relative;
var dirname = upath.dirname;
var basename = upath.basename;
var extname = upath.extname;
var format = upath.format;
var parse = upath.parse;
var toNamespacedPath = upath.toNamespacedPath;
var matchesGlob = upath.matchesGlob;
var sep = upath.sep;
var delimiter = upath.delimiter;
var posix = upath.posix;
var win32 = upath.win32;
var VERSION = upath.VERSION;
var index_default = upath;
export {
VERSION,
addExt,
basename,
changeExt,
index_default as default,
defaultExt,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
joinSafe,
matchesGlob,
normalize,
normalizeSafe,
normalizeTrim,
parse,
posix,
relative,
removeExt,
resolve,
sep,
toNamespacedPath,
toUnix,
trimExt,
win32
};
//# sourceMappingURL=index.js.map