locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
34 lines (33 loc) • 934 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base = Base;
function Base(path) {
// discuss at: https://locutus.io/golang/path/Base
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: Base('/a/b/c.txt')
// returns 1: 'c.txt'
// example 2: Base('/a/b/')
// returns 2: 'b'
// example 3: Base('')
// returns 3: '.'
// example 4: Base('////')
// returns 4: '/'
const raw = String(path);
if (raw === '') {
return '.';
}
let end = raw.length;
while (end > 0 && raw[end - 1] === '/') {
end -= 1;
}
if (end === 0) {
return '/';
}
const trimmed = raw.slice(0, end);
const slash = trimmed.lastIndexOf('/');
if (slash < 0) {
return trimmed;
}
return trimmed.slice(slash + 1);
}