@citrineos/util
Version:
The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.
44 lines • 1.44 kB
JavaScript
// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
/**
* Utility for matching URLs against patterns
*/
export class UrlMatcher {
/**
* Check if a URL matches a pattern
* Supports exact matches, wildcards, and path parameters
*
* @param url URL to check
* @param pattern Pattern to match against
* @returns True if URL matches pattern
*/
static match(url, pattern) {
// Handle exact matches
if (url === pattern) {
return true;
}
// Handle wildcard patterns
if (pattern.endsWith('/*')) {
const basePattern = pattern.slice(0, -1);
return url === basePattern || url.startsWith(basePattern);
}
// Handle path parameters (e.g., /users/:id)
const patternSegments = pattern.split('/').filter(Boolean);
const urlSegments = url.split('/').filter(Boolean);
if (patternSegments.length !== urlSegments.length) {
return false;
}
for (let i = 0; i < patternSegments.length; i++) {
// Skip path parameters (starting with :)
if (patternSegments[i].startsWith(':')) {
continue;
}
if (patternSegments[i] !== urlSegments[i]) {
return false;
}
}
return true;
}
}
//# sourceMappingURL=UrlMatcher.js.map