Compare commits

..

3 Commits

Author SHA1 Message Date
db18c010aa Added adjusted interval & set errorFromCode to always return an error even on 200's
All checks were successful
Build / Build NPM Project (push) Successful in 1m16s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 2m1s
2024-10-29 10:41:25 -04:00
b292d5ed17 PathEvents has none fix
All checks were successful
Build / Build NPM Project (push) Successful in 36s
Build / Tag Version (push) Successful in 10s
Build / Publish Documentation (push) Successful in 1m47s
2024-10-23 21:39:42 -04:00
a1ea8cdf67 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
2024-10-23 14:13:47 -04:00
6 changed files with 53 additions and 11 deletions

View File

@ -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>

View File

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

View File

@ -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);

View File

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

View File

@ -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
*

View File

@ -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
*