@pixi/utils
Version:
Collection of utilities used by PixiJS
395 lines (390 loc) • 10.9 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var settings = require('@pixi/settings');
function assertPath(path2) {
if (typeof path2 !== "string") {
throw new TypeError(`Path must be a string. Received ${JSON.stringify(path2)}`);
}
}
function removeUrlParams(url) {
const re = url.split("?")[0];
return re.split("#")[0];
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}
function normalizeStringPosix(path2, allowAboveRoot) {
let res = "";
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code = -1;
for (let i = 0; i <= path2.length; ++i) {
if (i < path2.length) {
code = path2.charCodeAt(i);
} else if (code === 47) {
break;
} else {
code = 47;
}
if (code === 47) {
if (lastSlash === i - 1 || dots === 1) {
} else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 || res.charCodeAt(res.length - 2) !== 46) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf("/");
if (lastSlashIndex !== res.length - 1) {
if (lastSlashIndex === -1) {
res = "";
lastSegmentLength = 0;
} else {
res = res.slice(0, lastSlashIndex);
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
}
lastSlash = i;
dots = 0;
continue;
}
} else if (res.length === 2 || res.length === 1) {
res = "";
lastSegmentLength = 0;
lastSlash = i;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0) {
res += "/..";
} else {
res = "..";
}
lastSegmentLength = 2;
}
} else {
if (res.length > 0) {
res += `/${path2.slice(lastSlash + 1, i)}`;
} else {
res = path2.slice(lastSlash + 1, i);
}
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
dots = 0;
} else if (code === 46 && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return res;
}
const path = {
toPosix(path2) {
return replaceAll(path2, "\\", "/");
},
isUrl(path2) {
return /^https?:/.test(this.toPosix(path2));
},
isDataUrl(path2) {
return /^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s<>]*?)$/i.test(path2);
},
hasProtocol(path2) {
return /^[^/:]+:\//.test(this.toPosix(path2));
},
getProtocol(path2) {
assertPath(path2);
path2 = this.toPosix(path2);
let protocol = "";
const isFile = /^file:\/\/\//.exec(path2);
const isHttp = /^[^/:]+:\/\//.exec(path2);
const isWindows = /^[^/:]+:\//.exec(path2);
if (isFile || isHttp || isWindows) {
const arr = isFile?.[0] || isHttp?.[0] || isWindows?.[0];
protocol = arr;
path2 = path2.slice(arr.length);
}
return protocol;
},
toAbsolute(url, customBaseUrl, customRootUrl) {
if (this.isDataUrl(url))
return url;
const baseUrl = removeUrlParams(this.toPosix(customBaseUrl ?? settings.settings.ADAPTER.getBaseUrl()));
const rootUrl = removeUrlParams(this.toPosix(customRootUrl ?? this.rootname(baseUrl)));
assertPath(url);
url = this.toPosix(url);
if (url.startsWith("/")) {
return path.join(rootUrl, url.slice(1));
}
const absolutePath = this.isAbsolute(url) ? url : this.join(baseUrl, url);
return absolutePath;
},
normalize(path2) {
path2 = this.toPosix(path2);
assertPath(path2);
if (path2.length === 0)
return ".";
let protocol = "";
const isAbsolute = path2.startsWith("/");
if (this.hasProtocol(path2)) {
protocol = this.rootname(path2);
path2 = path2.slice(protocol.length);
}
const trailingSeparator = path2.endsWith("/");
path2 = normalizeStringPosix(path2, false);
if (path2.length > 0 && trailingSeparator)
path2 += "/";
if (isAbsolute)
return `/${path2}`;
return protocol + path2;
},
isAbsolute(path2) {
assertPath(path2);
path2 = this.toPosix(path2);
if (this.hasProtocol(path2))
return true;
return path2.startsWith("/");
},
join(...segments) {
if (segments.length === 0) {
return ".";
}
let joined;
for (let i = 0; i < segments.length; ++i) {
const arg = segments[i];
assertPath(arg);
if (arg.length > 0) {
if (joined === void 0)
joined = arg;
else {
const prevArg = segments[i - 1] ?? "";
if (this.extname(prevArg)) {
joined += `/../${arg}`;
} else {
joined += `/${arg}`;
}
}
}
}
if (joined === void 0) {
return ".";
}
return this.normalize(joined);
},
dirname(path2) {
assertPath(path2);
if (path2.length === 0)
return ".";
path2 = this.toPosix(path2);
let code = path2.charCodeAt(0);
const hasRoot = code === 47;
let end = -1;
let matchedSlash = true;
const proto = this.getProtocol(path2);
const origpath = path2;
path2 = path2.slice(proto.length);
for (let i = path2.length - 1; i >= 1; --i) {
code = path2.charCodeAt(i);
if (code === 47) {
if (!matchedSlash) {
end = i;
break;
}
} else {
matchedSlash = false;
}
}
if (end === -1)
return hasRoot ? "/" : this.isUrl(origpath) ? proto + path2 : proto;
if (hasRoot && end === 1)
return "//";
return proto + path2.slice(0, end);
},
rootname(path2) {
assertPath(path2);
path2 = this.toPosix(path2);
let root = "";
if (path2.startsWith("/"))
root = "/";
else {
root = this.getProtocol(path2);
}
if (this.isUrl(path2)) {
const index = path2.indexOf("/", root.length);
if (index !== -1) {
root = path2.slice(0, index);
} else
root = path2;
if (!root.endsWith("/"))
root += "/";
}
return root;
},
basename(path2, ext) {
assertPath(path2);
if (ext)
assertPath(ext);
path2 = removeUrlParams(this.toPosix(path2));
let start = 0;
let end = -1;
let matchedSlash = true;
let i;
if (ext !== void 0 && ext.length > 0 && ext.length <= path2.length) {
if (ext.length === path2.length && ext === path2)
return "";
let extIdx = ext.length - 1;
let firstNonSlashEnd = -1;
for (i = path2.length - 1; i >= 0; --i) {
const code = path2.charCodeAt(i);
if (code === 47) {
if (!matchedSlash) {
start = i + 1;
break;
}
} else {
if (firstNonSlashEnd === -1) {
matchedSlash = false;
firstNonSlashEnd = i + 1;
}
if (extIdx >= 0) {
if (code === ext.charCodeAt(extIdx)) {
if (--extIdx === -1) {
end = i;
}
} else {
extIdx = -1;
end = firstNonSlashEnd;
}
}
}
}
if (start === end)
end = firstNonSlashEnd;
else if (end === -1)
end = path2.length;
return path2.slice(start, end);
}
for (i = path2.length - 1; i >= 0; --i) {
if (path2.charCodeAt(i) === 47) {
if (!matchedSlash) {
start = i + 1;
break;
}
} else if (end === -1) {
matchedSlash = false;
end = i + 1;
}
}
if (end === -1)
return "";
return path2.slice(start, end);
},
extname(path2) {
assertPath(path2);
path2 = removeUrlParams(this.toPosix(path2));
let startDot = -1;
let startPart = 0;
let end = -1;
let matchedSlash = true;
let preDotState = 0;
for (let i = path2.length - 1; i >= 0; --i) {
const code = path2.charCodeAt(i);
if (code === 47) {
if (!matchedSlash) {
startPart = i + 1;
break;
}
continue;
}
if (end === -1) {
matchedSlash = false;
end = i + 1;
}
if (code === 46) {
if (startDot === -1)
startDot = i;
else if (preDotState !== 1)
preDotState = 1;
} else if (startDot !== -1) {
preDotState = -1;
}
}
if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
return "";
}
return path2.slice(startDot, end);
},
parse(path2) {
assertPath(path2);
const ret = { root: "", dir: "", base: "", ext: "", name: "" };
if (path2.length === 0)
return ret;
path2 = removeUrlParams(this.toPosix(path2));
let code = path2.charCodeAt(0);
const isAbsolute = this.isAbsolute(path2);
let start;
const protocol = "";
ret.root = this.rootname(path2);
if (isAbsolute || this.hasProtocol(path2)) {
start = 1;
} else {
start = 0;
}
let startDot = -1;
let startPart = 0;
let end = -1;
let matchedSlash = true;
let i = path2.length - 1;
let preDotState = 0;
for (; i >= start; --i) {
code = path2.charCodeAt(i);
if (code === 47) {
if (!matchedSlash) {
startPart = i + 1;
break;
}
continue;
}
if (end === -1) {
matchedSlash = false;
end = i + 1;
}
if (code === 46) {
if (startDot === -1)
startDot = i;
else if (preDotState !== 1)
preDotState = 1;
} else if (startDot !== -1) {
preDotState = -1;
}
}
if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
if (end !== -1) {
if (startPart === 0 && isAbsolute)
ret.base = ret.name = path2.slice(1, end);
else
ret.base = ret.name = path2.slice(startPart, end);
}
} else {
if (startPart === 0 && isAbsolute) {
ret.name = path2.slice(1, startDot);
ret.base = path2.slice(1, end);
} else {
ret.name = path2.slice(startPart, startDot);
ret.base = path2.slice(startPart, end);
}
ret.ext = path2.slice(startDot, end);
}
ret.dir = this.dirname(path2);
if (protocol)
ret.dir = protocol + ret.dir;
return ret;
},
sep: "/",
delimiter: ":"
};
exports.path = path;
//# sourceMappingURL=path.js.map