From a1ea8cdf6736eb13bc95724e4a9d891f7067c9bf Mon Sep 17 00:00:00 2001 From: ztimson Date: Wed, 23 Oct 2024 14:13:47 -0400 Subject: [PATCH] Added filter function to path events --- index.html | 8 ++------ package.json | 2 +- src/index.ts | 2 -- src/path-events.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index dc11201..f60a8dc 100644 --- a/index.html +++ b/index.html @@ -6,13 +6,9 @@ diff --git a/package.json b/package.json index ade230a..7efe6ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.21.3", + "version": "0.21.4", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index ee540ff..ebf8a22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -import {PE} from './path-events.ts'; - export * from './array'; export * from './aset'; export * from './cache'; diff --git a/src/path-events.ts b/src/path-events.ts index f5c5c83..3ff28b9 100644 --- a/src/path-events.ts +++ b/src/path-events.ts @@ -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 *