upath
Version:
A drop-in replacement / proxy to Node.js path, replacing \\ with / for all results & adding file extension functions.
229 lines (228 loc) • 6.67 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
VERSION: () => VERSION,
addExt: () => addExt,
basename: () => basename,
changeExt: () => changeExt,
default: () => index_default,
defaultExt: () => defaultExt,
delimiter: () => delimiter,
dirname: () => dirname,
extname: () => extname,
format: () => format,
isAbsolute: () => isAbsolute,
join: () => join,
joinSafe: () => joinSafe,
matchesGlob: () => matchesGlob,
normalize: () => normalize,
normalizeSafe: () => normalizeSafe,
normalizeTrim: () => normalizeTrim,
parse: () => parse,
posix: () => posix,
relative: () => relative,
removeExt: () => removeExt,
resolve: () => resolve,
sep: () => sep,
toNamespacedPath: () => toNamespacedPath,
toUnix: () => toUnix,
trimExt: () => trimExt,
win32: () => win32
});
module.exports = __toCommonJS(index_exports);
var path = __toESM(require("path"), 1);
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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
VERSION,
addExt,
basename,
changeExt,
defaultExt,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
joinSafe,
matchesGlob,
normalize,
normalizeSafe,
normalizeTrim,
parse,
posix,
relative,
removeExt,
resolve,
sep,
toNamespacedPath,
toUnix,
trimExt,
win32
});
//# sourceMappingURL=index.cjs.map