UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

47 lines (46 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Join = Join; function Join(...elem) { // discuss at: https://locutus.io/golang/filepath/Join // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Mirrors path/filepath.Join for slash-separated paths on the Go parity target. // example 1: Join('a', 'b', 'c') // returns 1: 'a/b/c' // example 2: Join('/a/', '/b/', 'c') // returns 2: '/a/b/c' // example 3: Join('a', '..', 'b') // returns 3: 'b' // example 4: Join('', '') // returns 4: '' const parts = elem.map((value) => String(value)).filter((value) => value !== ''); if (parts.every((value) => value === '')) { return ''; } const raw = parts.join('/'); 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; }