import { assertEquals } from "jsr:@std/assert"
// aliasing our functions for brevity
const eq = assertEquals, fn = commonPathTransform
const subpath_map_fn = ([common_dir, subpath]: [string, string]) => (subpath)
eq(fn([
"C:/Hello/World/This/Is/An/Example/Bla.cs",
"C:\\Hello\\World\\This\\Is\\Not/An/Example/",
"C:/Hello/Earth/Bla/Bla/Bla",
], subpath_map_fn), [
"World/This/Is/An/Example/Bla.cs",
"World/This/Is/Not/An/Example/",
"Earth/Bla/Bla/Bla",
])
eq(fn([
"./../././home/Hello/World/This/Used/to-be-an/example/../../../Is/An/Example/Bla.cs",
"./././../home/Hello/World/This/Is/an/example/bla.cs",
"./../home/Hello/World/This/Is/Not/An/Example/",
], subpath_map_fn), [
"An/Example/Bla.cs",
"an/example/bla.cs",
"Not/An/Example/",
])
eq(fn([
"/C:/Hello///World/Users/This/Is/An/Example/Bla.cs",
"/C:\\Hello\\World Users\\This\\Is/An\\example/bla.cs",
"/C:/./.\\.\\././Hello/World-Users/./././././This/Is/Not/An/Example/",
], subpath_map_fn), [
"//World/Users/This/Is/An/Example/Bla.cs",
"World Users/This/Is/An/example/bla.cs",
"World-Users/This/Is/Not/An/Example/",
])
replace the common path among all provided
paths
by transforming it with a custommap_fn
function. allpaths
are initially normalized and converted into posix-style (so that no "\" windows separator is prevelent).the
map_fn
function's first argument (path_info
), is a 2-tuple of the form[common_dir: string, subpath: string]
, wherecommon_dir
represents the directory common to all of the inputpaths
, and thesubpath
represents the remaining relative path that comes after common_dir.common_dir
always ends with a trailing slash ("/"), unless there is absolutely no common directory among thepaths
at all.subpath
never begins with any slash (nor any dot-slashes), unless of course, you had initially provided a path containing two or more consecutive slashes.