From e009b25cab836689effa0c68f172ed87eece0a63 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 9 Feb 2025 17:48:36 +0100 Subject: [PATCH] fix: calculate ends with slash on every iteration --- src/utils/url.test.ts | 2 ++ src/utils/url.ts | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils/url.test.ts b/src/utils/url.test.ts index 621c4d90e..f1570bb14 100644 --- a/src/utils/url.test.ts +++ b/src/utils/url.test.ts @@ -152,6 +152,8 @@ describe('url', () => { expect(mergePath('/book', 'hey', 'say')).toBe('/book/hey/say') expect(mergePath('/book', '/hey/', '/say/')).toBe('/book/hey/say/') expect(mergePath('/book', '/hey/', '/say/', '/')).toBe('/book/hey/say/') + expect(mergePath('/book', '/hey', '/say', '/')).toBe('/book/hey/say') + expect(mergePath('/', '/book', '/hey', '/say', '/')).toBe('/book/hey/say') expect(mergePath('book', '/')).toBe('/book') expect(mergePath('book/', '/')).toBe('/book/') diff --git a/src/utils/url.ts b/src/utils/url.ts index 7c87846a2..eea5b4483 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -141,10 +141,12 @@ export const mergePath = (...paths: string[]): string => { let endsWithSlash = false for (let path of paths) { + // calculate endsWithSlash at the start of each iteration + endsWithSlash = p.at(-1) === '/' + /* ['/hey/','/say'] => ['/hey', '/say'] */ - if (p.at(-1) === '/') { + if (endsWithSlash) { p = p.slice(0, -1) - endsWithSlash = true } /* ['/hey','say'] => ['/hey', '/say'] */