Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
d140b2edd1 | |||
56f6681492 | |||
64945a647c |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.25.18",
|
"version": "0.25.21",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -34,12 +34,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
|
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
|
||||||
if(options.storage) {
|
if(options.storage) {
|
||||||
if(options.storage instanceof Table) {
|
if(options.storage instanceof Table) {
|
||||||
(async () => (await options.storage?.getAll()).forEach((v: any) => {
|
(async () => this.addAll(await options.storage?.getAll(), false))()
|
||||||
if(v) {
|
|
||||||
try { this.add(v) }
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}))()
|
|
||||||
} else if(options.storageKey) {
|
} else if(options.storageKey) {
|
||||||
const stored = options.storage?.getItem(options.storageKey);
|
const stored = options.storage?.getItem(options.storageKey);
|
||||||
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
|
||||||
|
@ -141,9 +141,13 @@ 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 => new PathEvent(pe));
|
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
|
||||||
const parsedFilter = makeArray(filter).map(pe => new PathEvent(pe));
|
const parsedFilter = makeArray(filter).map(pe => new PathEvent(pe));
|
||||||
return parsedTarget.filter(t => !!parsedFilter.find(f =>
|
return parsedTarget.filter(t => !!parsedFilter.find(r => {
|
||||||
(t.fullPath == '*' || f.fullPath == '*' || t.fullPath.startsWith(f.fullPath) || f.fullPath.startsWith(t.fullPath)) &&
|
const wildcard = r.fullPath == '*' || t.fullPath == '*';
|
||||||
(f.all || t.all || t.methods.intersection(f.methods).length)));
|
const p1 = r.fullPath.slice(0, r.fullPath.indexOf('*')), p2 = t.fullPath.slice(0, t.fullPath.indexOf('*'))
|
||||||
|
const scope = p1.startsWith(p2) || p2.startsWith(p1);
|
||||||
|
const methods = r.all || t.all || r.methods.intersection(t.methods).length;
|
||||||
|
return (wildcard || scope) && methods;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,10 +160,13 @@ export class PathEvent {
|
|||||||
static has(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): boolean {
|
static has(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): boolean {
|
||||||
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
|
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
|
||||||
const parsedRequired = makeArray(has).map(pe => new PathEvent(pe));
|
const parsedRequired = makeArray(has).map(pe => new PathEvent(pe));
|
||||||
return !!parsedRequired.find(r => !!parsedTarget.find(t =>
|
return !!parsedRequired.find(r => !!parsedTarget.find(t => {
|
||||||
(r.fullPath == '*' || t.fullPath == '*' || r.fullPath.startsWith(t.fullPath)) &&
|
const wildcard = r.fullPath == '*' || t.fullPath == '*';
|
||||||
(r.all || t.all || r.methods.intersection(t.methods).length)
|
const p1 = r.fullPath.slice(0, r.fullPath.indexOf('*')), p2 = t.fullPath.slice(0, t.fullPath.indexOf('*'))
|
||||||
));
|
const scope = p1.startsWith(p2);
|
||||||
|
const methods = r.all || t.all || r.methods.intersection(t.methods).length;
|
||||||
|
return (wildcard || scope) && methods;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -207,6 +214,44 @@ export class PathEvent {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Squash 2 sets of paths & return true if any overlap is found
|
||||||
|
*
|
||||||
|
* @param has Target must have at least one of these path
|
||||||
|
* @return {boolean} Whether there is any overlap
|
||||||
|
*/
|
||||||
|
has(...has: (string | PathEvent)[]): boolean {
|
||||||
|
return PathEvent.has(this, ...has);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Squash 2 sets of paths & return true if the target has all paths
|
||||||
|
*
|
||||||
|
* @param has Target must have all these paths
|
||||||
|
* @return {boolean} Whether there is any overlap
|
||||||
|
*/
|
||||||
|
hasAll(...has: (string | PathEvent)[]): boolean {
|
||||||
|
return PathEvent.hasAll(this, ...has);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as `has` but raises an error if there is no overlap
|
||||||
|
*
|
||||||
|
* @param has Target must have at least one of these path
|
||||||
|
*/
|
||||||
|
hasFatal(...has: (string | PathEvent)[]): void {
|
||||||
|
return PathEvent.hasFatal(this, ...has);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as `hasAll` but raises an error if the target is missing any paths
|
||||||
|
*
|
||||||
|
* @param has Target must have all these paths
|
||||||
|
*/
|
||||||
|
hasAllFatal(...has: (string | PathEvent)[]): void {
|
||||||
|
return PathEvent.hasAllFatal(this, ...has);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter a set of paths based on this event
|
* Filter a set of paths based on this event
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user