Skip to content

Commit

Permalink
matchPath - Improved matching with ending wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
thim81 committed Nov 3, 2024
1 parent 20012d6 commit f01a51e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/utils/matchPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ describe('matchPath', () => {
const operationPath = '/messages/123/'
expect(matchPath(targetPath, operationPath)).toBe(true)
})

it('should handle paths with ending wildcards', () => {
const targetPath = '/messages*'
const operationPath = '/messages/{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 @@ -12,6 +12,16 @@ export const matchPath = (targetPath: string | RegExp, operationPath: string): b
const targetSegments = typeof targetPath === 'string' ? targetPath.split('/') : []
const operationSegments = operationPath.split('/')

// 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
}
}

// Ensure the segment lengths match when there is no wildcard in targetPath
if (targetSegments.length > 0 && operationSegments.length > 0) {
const lastTargetSegment = targetSegments[targetSegments.length - 1]
Expand Down

0 comments on commit f01a51e

Please sign in to comment.