Compare commits

...

6 Commits

Author SHA1 Message Date
10ac6101f4 Fixed cache handling of undefined
All checks were successful
Build / Build NPM Project (push) Successful in 47s
Build / Tag Version (push) Successful in 13s
Build / Publish Documentation (push) Successful in 53s
2025-06-30 11:44:32 -04:00
b5d534dc8c Added hook to wait for cache loading
All checks were successful
Build / Build NPM Project (push) Successful in 39s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 34s
2025-06-29 23:11:21 -04:00
d140b2edd1 Revert: Restoring cache from DB gets complete status
All checks were successful
Build / Build NPM Project (push) Successful in 41s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 32s
2025-06-29 22:41:39 -04:00
56f6681492 Restoring cache from DB gets complete status
All checks were successful
Build / Build NPM Project (push) Successful in 47s
Build / Tag Version (push) Successful in 15s
Build / Publish Documentation (push) Successful in 52s
2025-06-29 22:29:16 -04:00
64945a647c Fixed path-events
All checks were successful
Build / Build NPM Project (push) Successful in 43s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 48s
2025-06-28 22:25:11 -04:00
adafd61c83 Added IPv6 to v4 converter
All checks were successful
Build / Build NPM Project (push) Successful in 1m17s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 52s
2025-06-25 11:48:49 -04:00
4 changed files with 92 additions and 23 deletions

View File

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

View File

@ -24,6 +24,8 @@ export class Cache<K extends string | number | symbol, T> {
[key: string | number | symbol]: CachedValue<T> | any;
/** Whether cache is complete */
complete = false;
/** Await initial loading */
loading!: Promise<void>;
/**
* Create new cache
@ -31,20 +33,21 @@ export class Cache<K extends string | number | symbol, T> {
* @param options
*/
constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
let resolve: any;
this.loading = new Promise(r => resolve = r);
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
if(options.storage) {
if(options.storage instanceof Table) {
(async () => (await options.storage?.getAll()).forEach((v: any) => {
if(v) {
try { this.add(v) }
catch { }
}
}))()
(async () => {
this.addAll(await options.storage?.getAll(), false);
resolve();
})()
} else if(options.storageKey) {
const stored = options.storage?.getItem(options.storageKey);
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
resolve();
}
}
} else resolve();
return new Proxy(this, {
get: (target: this, prop: string | symbol) => {
if(prop in target) return (target as any)[prop];
@ -64,10 +67,18 @@ export class Cache<K extends string | number | symbol, T> {
return <K>value[this.key];
}
private save(key: K) {
private save(key?: K) {
if(this.options.storage) {
if(this.options.storage instanceof Table) {
this.options.storage.put(key, this.store[key]);
if(key == null) {
const rows = this.entries();
rows.forEach(([k, v]) => this.options.storage?.put(k, v));
this.options.storage.getAllKeys().then(keys => {
rows.map(([k]) => k).filter(k => !keys.includes(k))
.forEach(k => this.options.storage?.delete(k));
})
} else if(this.store[key] === undefined) this.options.storage.delete(key);
else this.options.storage.put(key, this.store[key]);
} else if(this.options.storageKey) {
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
}
@ -114,6 +125,7 @@ export class Cache<K extends string | number | symbol, T> {
clear(): this {
this.complete = false;
this.store = <any>{};
this.save();
return this;
}

View File

@ -1,6 +1,16 @@
import {PathEvent} from './path-events.ts';
import {md5} from './string';
/**
* Escape any regex special characters to avoid misinterpretation during search
*
* @param {string} value String which should be escaped
* @return {string} New escaped sequence
*/
export function escapeRegex(value: string) {
return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
}
/**
* Run a stringified function with arguments asynchronously
* @param {object} args Map of key/value arguments
@ -26,13 +36,15 @@ export function gravatar(email: string, def='mp') {
}
/**
* Escape any regex special characters to avoid misinterpretation during search
*
* @param {string} value String which should be escaped
* @return {string} New escaped sequence
* Convert IPv6 to v4 because who uses that, NAT4Life
* @param {string} ip IPv6 address, e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334
* @returns {string | null} IPv4 address, e.g. 172.16.58.3
*/
export function escapeRegex(value: string) {
return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
export function ipV6ToV4(ip: string) {
if(!ip) return null;
const ipv4 = ip.split(':').splice(-1)[0];
if(ipv4 == '1') return '127.0.0.1';
return ipv4;
}
/**

View File

@ -141,9 +141,13 @@ export class PathEvent {
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
const parsedFilter = makeArray(filter).map(pe => new PathEvent(pe));
return parsedTarget.filter(t => !!parsedFilter.find(f =>
(t.fullPath == '*' || f.fullPath == '*' || t.fullPath.startsWith(f.fullPath) || f.fullPath.startsWith(t.fullPath)) &&
(f.all || t.all || t.methods.intersection(f.methods).length)));
return parsedTarget.filter(t => !!parsedFilter.find(r => {
const wildcard = r.fullPath == '*' || t.fullPath == '*';
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 {
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
const parsedRequired = makeArray(has).map(pe => new PathEvent(pe));
return !!parsedRequired.find(r => !!parsedTarget.find(t =>
(r.fullPath == '*' || t.fullPath == '*' || r.fullPath.startsWith(t.fullPath)) &&
(r.all || t.all || r.methods.intersection(t.methods).length)
));
return !!parsedRequired.find(r => !!parsedTarget.find(t => {
const wildcard = r.fullPath == '*' || t.fullPath == '*';
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;
}
/**
* 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
*