Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchPath - Improved matching with ending wildcard #674

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading