Skip to content

Commit f1850ca

Browse files
committed
Исправлена логика склеивания с пустыми строками
1 parent f5e0d38 commit f1850ca

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/Stuff/concatUrlFromPathes.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ test("Множественные / тоже убираются", () => {
3131

3232
test("Много путей на всякий случай", () => {
3333
expect(concatUrlFromPathes("foo", "bar/" , "one", "more")).toBe("foo/bar/one/more")
34+
})
35+
36+
test("Пустой путь в начале не добавляет /", () => {
37+
expect(concatUrlFromPathes("", "tour.json")).toBe("tour.json")
38+
})
39+
40+
test("Пустота в конце не добавляет /", () => {
41+
expect(concatUrlFromPathes("some/base", "")).toBe("some/base")
42+
})
43+
44+
test("Пустота в середине не добавляет /", () => {
45+
expect(concatUrlFromPathes("some/base", "", "", "some", "end")).toBe("some/base/some/end")
3446
})

src/Stuff/concatUrlFromPathes.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
export function concatUrlFromPathes(...pathes: string[]) {
2-
return pathes.map((path, i) => {
3-
if (i > 0) {
4-
while (path.startsWith('/')) {
5-
path = path.substring(1, path.length);
6-
}
7-
}
8-
if (i < pathes.length - 1) {
9-
while (path.endsWith('/')) {
10-
path = path.substring(0, path.length - 1);
11-
}
12-
}
13-
return path;
14-
}).join("/");
15-
}
2+
return pathes
3+
.map((path, i) => {
4+
if (i > 0) {
5+
while (path.startsWith("/")) {
6+
path = path.substring(1, path.length);
7+
}
8+
}
9+
if (i < pathes.length - 1) {
10+
while (path.endsWith("/")) {
11+
path = path.substring(0, path.length - 1);
12+
}
13+
}
14+
return path;
15+
})
16+
.filter((s) => !!s)
17+
.join("/");
18+
}

0 commit comments

Comments
 (0)