locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
40 lines (39 loc) • 1.14 kB
JavaScript
export function Clean(path) {
// discuss at: https://locutus.io/golang/path/Clean
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: Clean('/a//b/../c/.')
// returns 1: '/a/c'
// example 2: Clean('a/../../b')
// returns 2: '../b'
// example 3: Clean('')
// returns 3: '.'
const raw = String(path);
if (raw === '') {
return '.';
}
const rooted = raw.startsWith('/');
const segments = raw.split('/');
const stack = [];
for (const segment of segments) {
if (segment === '' || segment === '.') {
continue;
}
if (segment === '..') {
const top = stack.at(-1);
if (top && top !== '..') {
stack.pop();
}
else if (!rooted) {
stack.push('..');
}
continue;
}
stack.push(segment);
}
const cleaned = `${rooted ? '/' : ''}${stack.join('/')}`;
if (cleaned === '') {
return rooted ? '/' : '.';
}
return cleaned;
}