Added filter function to path events
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 1m46s

This commit is contained in:
Zakary Timson 2024-10-23 14:13:47 -04:00
parent fbbe3c99ef
commit a1ea8cdf67
4 changed files with 30 additions and 9 deletions

View File

@ -6,13 +6,9 @@
</head> </head>
<body> <body>
<script type="module"> <script type="module">
import {PathEvent, PE} from './dist/index.mjs'; import {PathEvent} from './dist/index.mjs';
const test = PathEvent.combine(PE`storage:n`); console.log(PathEvent.filter(['storage/inventory:c', 'data:*'], `storage`));
debugger;
console.log(test.methods.indexOf('n'));
test.methods.delete('n').add('c');
console.log(test);
</script> </script>
</body> </body>
</html> </html>

View File

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

View File

@ -1,5 +1,3 @@
import {PE} from './path-events.ts';
export * from './array'; export * from './array';
export * from './aset'; export * from './aset';
export * from './cache'; export * from './cache';

View File

@ -130,6 +130,23 @@ export class PathEvent {
return combined; return combined;
} }
/**
* Filter a set of paths based on the target
*
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
* @param filter {...PathEvent} Must container one of
* @return {boolean} Whether there is any overlap
*/
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
const parsedFind = makeArray(filter).map(pe => new PathEvent(pe));
return parsedTarget.filter(t => {
if(!t.fullPath && t.all) return true;
return !!parsedFind.find(f => t.fullPath.startsWith(f.fullPath)
&& (f.all || t.all || t.methods.intersection(f.methods).length));
});
}
/** /**
* Squash 2 sets of paths & return true if any overlap is found * Squash 2 sets of paths & return true if any overlap is found
* *
@ -194,6 +211,16 @@ export class PathEvent {
return p; return p;
} }
/**
* Filter a set of paths based on this event
*
* @param {string | PathEvent | (string | PathEvent)[]} target Array of events that will filtered
* @return {boolean} Whether there is any overlap
*/
filter(target: string | PathEvent | (string | PathEvent)[]): PathEvent[] {
return PathEvent.filter(target, this);
}
/** /**
* Create event string from its components * Create event string from its components
* *