@ocap/util
Version:
utils shared across multiple forge js libs, works in both node.js and browser
24 lines (23 loc) • 767 B
JavaScript
//#region src/scope-match.ts
/**
* Check if a scope matches a pattern.
*
* Matching rules:
* - Exact match: 'fg:t:transfer' matches 'fg:t:transfer'
* - Wildcard: 'fg:t:transfer' matches 'fg:t:*' (pattern ends with ':*')
* - Prefix with segment boundary: 'fg:x:connect:authPrincipal' matches 'fg:x:connect'
* but 'fg:x:connectFoo' does NOT match 'fg:x:connect'
*/
function scopeMatch(scope, pattern) {
if (scope === pattern) return true;
if (pattern.endsWith(":*")) return scope.startsWith(pattern.slice(0, -1));
return scope.startsWith(`${pattern}:`);
}
/**
* Check if a scope matches any of the given patterns.
*/
function scopeMatchAny(scope, patterns) {
return patterns.some((p) => scopeMatch(scope, p));
}
//#endregion
export { scopeMatch, scopeMatchAny };