Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
db18c010aa | |||
b292d5ed17 | |||
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.has(['data/Inventory:n', 'logs:c'], `logs/client:n`));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.21.3",
|
||||
"version": "0.21.6",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -180,7 +180,6 @@ export class GatewayTimeoutError extends CustomError {
|
||||
* @return {CustomError} The proper error type
|
||||
*/
|
||||
export function errorFromCode(code: number, message?: string) {
|
||||
if(code >= 200 && code < 300) return null;
|
||||
switch(code) {
|
||||
case 400:
|
||||
return new BadRequestError(message);
|
||||
|
@ -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
|
||||
*
|
||||
@ -145,7 +162,7 @@ export class PathEvent {
|
||||
const filtered = parsedTarget.filter(p => r.fullPath.startsWith(p.fullPath));
|
||||
if(!filtered.length) return false;
|
||||
const combined = PathEvent.combine(...filtered);
|
||||
return !combined.none && (combined.all || new ASet(combined.methods).intersection(new ASet(r.methods)).length);
|
||||
return (!combined.none && (combined.all || r.all)) || combined.methods.intersection(r.methods).length;
|
||||
});
|
||||
}
|
||||
|
||||
@ -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
|
||||
*
|
||||
|
22
src/time.ts
22
src/time.ts
@ -1,3 +1,25 @@
|
||||
/**
|
||||
* Like setInterval but will adjust the timeout value to account for runtime
|
||||
* @param {Function} cb Callback function that will be ran
|
||||
* @param {number} ms Run function ever x seconds
|
||||
* @return {() => void}
|
||||
*/
|
||||
export function adjustedInterval(cb: Function, ms: number) {
|
||||
let cancel = false, timeout: any = null;
|
||||
const p = async () => {
|
||||
if (cancel) return;
|
||||
const start = new Date().getTime();
|
||||
await cb();
|
||||
const end = new Date().getTime();
|
||||
timeout = setTimeout(() => p(), ms - (end - start) || 1);
|
||||
};
|
||||
p();
|
||||
return () => {
|
||||
cancel = true;
|
||||
if(timeout) clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return date formated highest to lowest: YYYY-MM-DD H:mm AM
|
||||
*
|
||||
|
Reference in New Issue
Block a user