Skip to content

Commit

Permalink
matchPath - Improved matching with ending wildcard (#674)
Browse files Browse the repository at this point in the history
* matchPath - Improved matching with ending wildcard
  • Loading branch information
thim81 authored Nov 5, 2024
1 parent 20012d6 commit e99b81c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## [Unreleased]

## v1.30.7 - (2024-11-05)

- matchPath - Improved matching with ending wildcard (#674)

## v1.30.6 - (2024-11-01)

- Further improved path matching (#672)
- matchPath - Further improved path matching (#672)

## v1.30.5 - (2024-11-01)

Expand Down
18 changes: 18 additions & 0 deletions src/utils/matchPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,22 @@ describe('matchPath', () => {
const operationPath = '/messages/123/'
expect(matchPath(targetPath, operationPath)).toBe(true)
})

it('should match paths with ending wildcards', () => {
const targetPath = '/messages*'
const operationPath = '/messages/{licenseKey}'
expect(matchPath(targetPath, operationPath)).toBe(true)
})

it('should not match paths with ending wildcards', () => {
const targetPath = '/messages*'
const operationPath = '/foo/{licenseKey}'
expect(matchPath(targetPath, operationPath)).toBe(false)
})

it('should match similar paths with ending wildcards', () => {
const targetPath = '/messages*'
const operationPath = '/messageses/{licenseKey}'
expect(matchPath(targetPath, operationPath)).toBe(true)
})
})
10 changes: 10 additions & 0 deletions src/utils/matchPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export const matchPath = (targetPath: string | RegExp, operationPath: string): b
}
}

// Check if targetPath ends with a wildcard (*) and handle it accordingly
if (typeof targetPath === 'string' && targetPath.endsWith('*')) {
// Remove the ending '*' from targetPath
const basePath = targetPath.slice(0, -1)
// Check if operationPath starts with basePath (prefix match)
if (operationPath.startsWith(basePath)) {
return true
}
}

// If the lengths of the paths don't match and there is no wildcard, return false
if (targetSegments.length !== operationSegments.length) {
return false
Expand Down

0 comments on commit e99b81c

Please sign in to comment.