Compare commits

...

2 Commits

Author SHA1 Message Date
2240c93db5 Fixed path events
All checks were successful
Build / Publish Docs (push) Successful in 48s
Build / Build NPM Project (push) Successful in 1m6s
Build / Tag Version (push) Successful in 9s
2025-11-29 20:03:45 -05:00
3e9052c4a7 Fixed path event has checks
All checks were successful
Build / Publish Docs (push) Successful in 1m1s
Build / Build NPM Project (push) Successful in 1m16s
Build / Tag Version (push) Successful in 8s
2025-11-29 11:27:47 -05:00
3 changed files with 42 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.27.12", "version": "0.27.14",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -40,6 +40,7 @@ export function PE(str: TemplateStringsArray, ...args: any[]) {
* @param {TemplateStringsArray} str * @param {TemplateStringsArray} str
* @param {string} args * @param {string} args
* @return {string} * @return {string}
* @constructor
*/ */
export function PES(str: TemplateStringsArray, ...args: any[]) { export function PES(str: TemplateStringsArray, ...args: any[]) {
let combined = []; let combined = [];
@@ -118,25 +119,13 @@ export class PathEvent {
if(!method) method = '*'; if(!method) method = '*';
// Handle special cases // Handle special cases
if(p === '' || p === undefined) { if(p === '' || p === undefined || p === '*') {
// Empty string matches nothing // Empty string with methods (e.g., ":*") matches the root level/everything
this.module = '';
this.path = '';
this.fullPath = '';
this.name = '';
this.methods = new ASet<Method>(['n']);
this.hasGlob = false;
PathEvent.pathEventCache.set(e, this);
return;
}
if(p === '*') {
// Wildcard means any path any event
this.module = ''; this.module = '';
this.path = ''; this.path = '';
this.fullPath = '**'; this.fullPath = '**';
this.name = ''; this.name = '';
this.methods = new ASet<Method>(['*']); this.methods = new ASet<Method>(p === '*' ? ['*'] : <any>method.split(''));
this.hasGlob = true; this.hasGlob = true;
PathEvent.pathEventCache.set(e, this); PathEvent.pathEventCache.set(e, this);
return; return;
@@ -292,11 +281,7 @@ export class PathEvent {
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] { static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe)); const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
const parsedFilter = makeArray(filter).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe)); const parsedFilter = makeArray(filter).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
return parsedTarget.filter(t => !!parsedFilter.find(r => PathEvent.matches(r, t)));
return parsedTarget.filter(t => {
const combined = PathEvent.combine(t);
return !!parsedFilter.find(r => PathEvent.matches(r, combined));
});
} }
/** /**
@@ -306,7 +291,8 @@ export class PathEvent {
private static matches(pattern: PathEvent, target: PathEvent): boolean { private static matches(pattern: PathEvent, target: PathEvent): boolean {
// Handle special cases // Handle special cases
if(pattern.fullPath === '' || target.fullPath === '') return false; if(pattern.fullPath === '' || target.fullPath === '') return false;
if (pattern.fullPath === '*' || target.fullPath === '*') return pattern.methods.has('*') || target.methods.has('*') || pattern.methods.intersection(target.methods).length > 0; if(pattern.fullPath === '**') return pattern.methods.has('*') || pattern.methods.intersection(target.methods).length > 0;
if(target.fullPath === '**') return pattern.methods.has('*') || target.methods.has('*') || pattern.methods.intersection(target.methods).length > 0;
// Check methods // Check methods
const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0; const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
@@ -338,10 +324,8 @@ export class PathEvent {
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe)); const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
const parsedRequired = makeArray(has).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe)); const parsedRequired = makeArray(has).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
// If target is a single item, check directly; if multiple, combine first // Check if any target permission matches any required permission
const effectiveTarget = parsedTarget.length === 1 ? parsedTarget[0] : PathEvent.combine(...parsedTarget); return !!parsedRequired.find(r => !!parsedTarget.find(t => PathEvent.matches(r, t)));
return !!parsedRequired.find(r => PathEvent.matches(r, effectiveTarget));
} }
/** /**

View File

@@ -48,10 +48,9 @@ describe('Path Events', () => {
expect(pe.hasAll('module:c', 'some/path:ud')).toBeTruthy(); expect(pe.hasAll('module:c', 'some/path:ud')).toBeTruthy();
}); });
it('parses empty string as none', () => { it('parses empty string as wildcard', () => {
const pe = new PathEvent(''); const pe = new PathEvent('');
expect(pe.none).toBe(true); expect(pe.has('any/path:c')).toBe(true);
expect(pe.fullPath).toBe('');
}); });
it('parses none method', () => { it('parses none method', () => {
@@ -81,6 +80,7 @@ describe('Path Events', () => {
expect(c.methods.has('c')).toBe(true); expect(c.methods.has('c')).toBe(true);
expect(c.methods.has('r')).toBe(true); expect(c.methods.has('r')).toBe(true);
expect(c.methods.has('u')).toBe(true); expect(c.methods.has('u')).toBe(true);
expect(c.delete).toBe(false);
}); });
it('combine stops at none', () => { it('combine stops at none', () => {
@@ -95,6 +95,17 @@ describe('Path Events', () => {
expect(d.none).toBe(false); expect(d.none).toBe(false);
}); });
it('combine works with wildcards', () => {
expect(PathEvent.has([
'payments/anonymous:d',
'payments/anonymous:c',
'payments/system:cr',
'logs/Momentum:c',
'products:r',
'*'
], 'actions/692a92d18afa11e6722e9f2e:r')).toBe(true);
});
it('filter finds overlap by path and methods', () => { it('filter finds overlap by path and methods', () => {
const events = [ const events = [
new PathEvent('users/sys:cr'), new PathEvent('users/sys:cr'),