Skip to content

Commit

Permalink
fix(utils/url): calculate ends with slash on every iteration (#3910)
Browse files Browse the repository at this point in the history
  • Loading branch information
luxass authored Feb 10, 2025
1 parent 6ceb1ab commit 8340a7b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
Expand Down
6 changes: 4 additions & 2 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] */
Expand Down

0 comments on commit 8340a7b

Please sign in to comment.