@flex-development/pathe
Version:
Universal drop-in replacement for node:path
55 lines (54 loc) • 1.59 kB
JavaScript
import canParseURL from "#internal/can-parse-url";
import { DRIVE_PATH_REGEX } from "#internal/constants";
import validateURLString from "#internal/validate-url-string";
import delimiter from "#lib/delimiter";
import dot from "#lib/dot";
import isSep from "#lib/is-sep";
import toPosix from "#lib/to-posix";
function extname(input) {
validateURLString(input, "input");
input = String(toPosix(input));
let base = 0;
let offset = 0;
let predot = 0;
let separator = true;
let start = -1;
let end = -1;
if (canParseURL(input)) {
offset = base = input.lastIndexOf(new URL(input).pathname);
if (DRIVE_PATH_REGEX.test(input.slice(offset + 1))) base = ++offset;
}
if (input.length >= 2 && DRIVE_PATH_REGEX.test(input.slice(offset))) {
offset = base = input.indexOf(delimiter, offset) + 1;
}
for (let i = input.length - 1; i >= offset; --i) {
const char = input[i];
if (isSep(char)) {
if (!separator) {
base = i + 1;
break;
}
continue;
}
if (end === -1) {
end = i + 1;
separator = false;
}
if (char === dot) {
if (start === -1) start = i;
else if (predot !== 1) predot = 1;
} else if (start !== -1) {
predot = -1;
}
}
if (start === -1 || end === -1 || // non-dot character immediately before the dot
predot === 0 || // right-most trimmed path component is exactly '..'
predot === 1 && start === end - 1 && start === base + 1) {
return "";
}
return input.slice(start, end);
}
var extname_default = extname;
export {
extname_default as default
};