Added filter function to path events
This commit is contained in:
parent
fbbe3c99ef
commit
a1ea8cdf67
@ -6,13 +6,9 @@
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import {PathEvent, PE} from './dist/index.mjs';
|
||||
import {PathEvent} from './dist/index.mjs';
|
||||
|
||||
const test = PathEvent.combine(PE`storage:n`);
|
||||
debugger;
|
||||
console.log(test.methods.indexOf('n'));
|
||||
test.methods.delete('n').add('c');
|
||||
console.log(test);
|
||||
console.log(PathEvent.filter(['storage/inventory:c', 'data:*'], `storage`));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.21.3",
|
||||
"version": "0.21.4",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -1,5 +1,3 @@
|
||||
import {PE} from './path-events.ts';
|
||||
|
||||
export * from './array';
|
||||
export * from './aset';
|
||||
export * from './cache';
|
||||
|
@ -130,6 +130,23 @@ export class PathEvent {
|
||||
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
|
||||
*
|
||||
@ -194,6 +211,16 @@ export class PathEvent {
|
||||
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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user