Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
2352dd25db | |||
80307b363b |
@ -1,11 +1,9 @@
|
||||
<html>
|
||||
<body>
|
||||
<script type="module">
|
||||
import {PathEventEmitter} from './dist/index.mjs';
|
||||
import {PES} from './dist/index.mjs';
|
||||
|
||||
const emitter = new PathEventEmitter('data');
|
||||
emitter.on('*', console.log);
|
||||
emitter.emit('data/asd', {});
|
||||
console.log(PES`storage${'Test/Test'}:d`);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.26.4",
|
||||
"version": "0.26.1",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -80,7 +80,7 @@ export class Cache<K extends string | number | symbol, T> {
|
||||
if(persists.storage?.constructor.name == 'Database') {
|
||||
(<Database>persists.storage).createTable({name: persists.key, key: <string>this.key}).then(table => {
|
||||
if(key) {
|
||||
table.set(this.get(key), key);
|
||||
table.set(key, this.get(key));
|
||||
} else {
|
||||
table.clear();
|
||||
this.all().forEach(row => table.add(row));
|
||||
|
@ -64,7 +64,6 @@ export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an array of objects to a CSV string
|
||||
*
|
||||
|
@ -166,11 +166,8 @@ export class Table<K extends IDBValidKey = any, T = any> {
|
||||
return this.tx(this.name, store => store.getAllKeys(), true);
|
||||
}
|
||||
|
||||
put(value: T, key?: string): Promise<void> {
|
||||
return this.tx(this.name, store => {
|
||||
if (store.keyPath) return store.put(value);
|
||||
return store.put(value, key);
|
||||
});
|
||||
put(key: K, value: T): Promise<void> {
|
||||
return this.tx(this.name, store => store.put(value, key));
|
||||
}
|
||||
|
||||
read(): Promise<T[]>;
|
||||
@ -180,9 +177,8 @@ export class Table<K extends IDBValidKey = any, T = any> {
|
||||
}
|
||||
|
||||
set(value: T, key?: K): Promise<void> {
|
||||
if(key) (<any>value)[this.key] = key;
|
||||
if(!(<any>value)[this.key]) return this.add(value);
|
||||
return this.put(value);
|
||||
if(!key && !(<any>value)[this.key]) return this.add(value);
|
||||
return this.put(key || (<any>value)[this.key], value);
|
||||
}
|
||||
|
||||
update = this.set;
|
||||
|
@ -177,51 +177,19 @@ export function includes(target: any, values: any, allowMissing = false): boolea
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep check if two items are equal.
|
||||
* Handles primitives, objects, arrays, functions, Date, RegExp, and circular references.
|
||||
* Deep check if two objects are equal
|
||||
*
|
||||
* @param {any} a - first item to compare
|
||||
* @param {any} b - second item to compare
|
||||
* @param {WeakMap<object, object>} [seen] - Internal parameter to track circular references
|
||||
* @returns {boolean} True if they match
|
||||
*/
|
||||
export function isEqual(a: any, b: any, seen = new WeakMap<object, object>()): boolean {
|
||||
// Simple cases
|
||||
if(a === b) return true;
|
||||
if(a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
||||
if(a instanceof RegExp && b instanceof RegExp) return a.source === b.source && a.flags === b.flags;
|
||||
|
||||
// Null checks
|
||||
if(typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) {
|
||||
if(Number.isNaN(a) && Number.isNaN(b)) return true;
|
||||
if(typeof a === 'function' && typeof b === 'function') return a.toString() === b.toString()
|
||||
return false;
|
||||
}
|
||||
|
||||
// Circular references
|
||||
if(seen.has(a)) return seen.get(a) === b;
|
||||
seen.set(a, b);
|
||||
const isArrayA = Array.isArray(a);
|
||||
const isArrayB = Array.isArray(b);
|
||||
|
||||
// Array checks
|
||||
if(isArrayA && isArrayB) {
|
||||
if(a.length !== b.length) return false;
|
||||
for(let i = 0; i < a.length; i++) {
|
||||
if(!isEqual(a[i], b[i], seen)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if(isArrayA !== isArrayB) return false;
|
||||
|
||||
// Key & value deep comparison
|
||||
const keysA = Object.keys(a);
|
||||
const keysB = Object.keys(b);
|
||||
if(keysA.length !== keysB.length) return false;
|
||||
for(const key of keysA) {
|
||||
if(!Object.prototype.hasOwnProperty.call(b, key) || !isEqual(a[key], b[key], seen)) return false;
|
||||
}
|
||||
return true;
|
||||
export function isEqual(a: any, b: any): boolean {
|
||||
const ta = typeof a, tb = typeof b;
|
||||
if((ta != 'object' || a == null) || (tb != 'object' || b == null))
|
||||
return ta == 'function' && tb == 'function' ? a.toString() == b.toString() : a === b;
|
||||
const keys = Object.keys(a);
|
||||
if(keys.length != Object.keys(b).length) return false;
|
||||
return Object.keys(a).every(key => isEqual(a[key], b[key]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,12 +71,9 @@ export class PathEvent {
|
||||
/** List of methods */
|
||||
methods!: ASet<Method>;
|
||||
|
||||
/** Internal cache for PathEvent instances to avoid redundant parsing */
|
||||
private static pathEventCache: Map<string, PathEvent> = new Map();
|
||||
|
||||
/** All/Wildcard specified */
|
||||
get all(): boolean { return this.methods.has('*') }
|
||||
set all(v: boolean) { v ? this.methods = new ASet<Method>(['*']) : this.methods.delete('*'); }
|
||||
set all(v: boolean) { v ? new ASet<Method>(['*']) : this.methods.delete('*'); }
|
||||
/** None specified */
|
||||
get none(): boolean { return this.methods.has('n') }
|
||||
set none(v: boolean) { v ? this.methods = new ASet<Method>(['n']) : this.methods.delete('n'); }
|
||||
@ -94,20 +91,10 @@ export class PathEvent {
|
||||
set delete(v: boolean) { v ? this.methods.delete('n').delete('*').add('d') : this.methods.delete('d'); }
|
||||
|
||||
constructor(e: string | PathEvent) {
|
||||
if(typeof e == 'object') {
|
||||
Object.assign(this, e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
if (PathEvent.pathEventCache.has(e)) {
|
||||
Object.assign(this, PathEvent.pathEventCache.get(e)!);
|
||||
return;
|
||||
}
|
||||
|
||||
if(typeof e == 'object') return Object.assign(this, e);
|
||||
let [p, scope, method] = e.replaceAll(/\/{2,}/g, '/').split(':');
|
||||
if(!method) method = scope || '*';
|
||||
if(p == '*' || (!p && method == '*')) {
|
||||
if(p == '*' || !p && method == '*') {
|
||||
p = '';
|
||||
method = '*';
|
||||
}
|
||||
@ -117,14 +104,6 @@ export class PathEvent {
|
||||
this.fullPath = `${this.module}${this.module && this.path ? '/' : ''}${this.path}`;
|
||||
this.name = temp.pop() || '';
|
||||
this.methods = new ASet(<any>method.split(''));
|
||||
|
||||
// Store in cache
|
||||
PathEvent.pathEventCache.set(e, this);
|
||||
}
|
||||
|
||||
/** Clear the cache of all PathEvents */
|
||||
static clearCache(): void {
|
||||
PathEvent.pathEventCache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,7 +115,7 @@ export class PathEvent {
|
||||
*/
|
||||
static combine(...paths: (string | PathEvent)[]): PathEvent {
|
||||
let hitNone = false;
|
||||
const combined = paths.map(p => p instanceof PathEvent ? p : new PathEvent(p))
|
||||
const combined = paths.map(p => new PathEvent(p))
|
||||
.toSorted((p1, p2) => {
|
||||
const l1 = p1.fullPath.length, l2 = p2.fullPath.length;
|
||||
return l1 < l2 ? 1 : (l1 > l2 ? -1 : 0);
|
||||
@ -145,9 +124,10 @@ export class PathEvent {
|
||||
if(p.none) hitNone = true;
|
||||
if(!acc) return p;
|
||||
if(hitNone) return acc;
|
||||
acc.methods = new ASet([...acc.methods, ...p.methods]);
|
||||
acc.methods = [...acc.methods, ...p.methods];
|
||||
return acc;
|
||||
}, <any>null);
|
||||
combined.methods = new ASet<Method>(combined.methods);
|
||||
return combined;
|
||||
}
|
||||
|
||||
@ -159,12 +139,11 @@ export class PathEvent {
|
||||
* @return {boolean} Whether there is any overlap
|
||||
*/
|
||||
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
|
||||
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||
const parsedFilter = makeArray(filter).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||
const parsedTarget = makeArray(target).map(pe => new PathEvent(pe));
|
||||
const parsedFilter = makeArray(filter).map(pe => new PathEvent(pe));
|
||||
return parsedTarget.filter(t => !!parsedFilter.find(r => {
|
||||
const wildcard = r.fullPath == '*' || t.fullPath == '*';
|
||||
const p1 = r.fullPath.includes('*') ? r.fullPath.slice(0, r.fullPath.indexOf('*')) : r.fullPath;
|
||||
const p2 = t.fullPath.includes('*') ? t.fullPath.slice(0, t.fullPath.indexOf('*')) : 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;
|
||||
@ -179,13 +158,12 @@ export class PathEvent {
|
||||
* @return {boolean} Whether there is any overlap
|
||||
*/
|
||||
static has(target: string | PathEvent | (string | PathEvent)[], ...has: (string | PathEvent)[]): boolean {
|
||||
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||
const parsedRequired = makeArray(has).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||
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 => {
|
||||
const wildcard = r.fullPath == '*' || t.fullPath == '*';
|
||||
const p1 = r.fullPath.includes('*') ? r.fullPath.slice(0, r.fullPath.indexOf('*')) : r.fullPath;
|
||||
const p2 = t.fullPath.includes('*') ? t.fullPath.slice(0, t.fullPath.indexOf('*')) : t.fullPath;
|
||||
const scope = p1.startsWith(p2); // Note: Original had || p2.startsWith(p1) here, but has implies target has required.
|
||||
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;
|
||||
}));
|
||||
@ -315,7 +293,7 @@ export class PathEventEmitter implements IPathEventEmitter{
|
||||
constructor(public readonly prefix: string = '') { }
|
||||
|
||||
emit(event: Event, ...args: any[]) {
|
||||
const parsed = event instanceof PathEvent ? event : new PathEvent(`${this.prefix}/${event}`);
|
||||
const parsed = PE`${this.prefix}/${event}`;
|
||||
this.listeners.filter(l => PathEvent.has(l[0], parsed))
|
||||
.forEach(async l => l[1](parsed, ...args));
|
||||
};
|
||||
@ -326,7 +304,7 @@ export class PathEventEmitter implements IPathEventEmitter{
|
||||
|
||||
on(event: Event | Event[], listener: PathListener): PathUnsubscribe {
|
||||
makeArray(event).forEach(e => this.listeners.push([
|
||||
e instanceof PathEvent ? e : new PathEvent(`${this.prefix}/${e}`),
|
||||
new PathEvent(`${this.prefix}/${e}`),
|
||||
listener
|
||||
]));
|
||||
return () => this.off(listener);
|
||||
|
@ -1,10 +1,6 @@
|
||||
import {PathError, PathEvent, PathEventEmitter, PE, PES} from '../src';
|
||||
|
||||
describe('Path Events', () => {
|
||||
beforeEach(() => {
|
||||
PathEvent.clearCache();
|
||||
});
|
||||
|
||||
describe('PE', () => {
|
||||
it('creates PathEvent from template string', () => {
|
||||
const e = PE`users/system:cr`;
|
||||
|
Reference in New Issue
Block a user