diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..c19db7e --- /dev/null +++ b/.npmignore @@ -0,0 +1,12 @@ +src +tests +.editorconfig +.gitignore +.gitmodules +.npmignore +CODEOWNERS +Dockerfile +index.html +jest.config.js +tsconfig.json +vite.config.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..dc11201 --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + + + + @ztimson/utils sandbox + + + + + diff --git a/package.json b/package.json index f348b13..ade230a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.21.0", + "version": "0.21.3", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/aset.ts b/src/aset.ts index dd89fb0..6e2faee 100644 --- a/src/aset.ts +++ b/src/aset.ts @@ -1,3 +1,5 @@ +import {isEqual} from './objects.ts'; + /** * An array which functions as a set. It guarantees unique elements * and provides set functions for comparisons @@ -34,7 +36,7 @@ export class ASet extends Array { delete(...items: T[]) { items.forEach(el => { const index = this.indexOf(el); - if(index != -1) this.slice(index, 1); + if(index != -1) this.splice(index, 1); }) return this; } @@ -57,6 +59,17 @@ export class ASet extends Array { return this.indexOf(el) != -1; } + /** + * Find index number of element, or -1 if it doesn't exist. Matches by equality not reference + * + * @param {T} search Element to find + * @param {number} fromIndex Starting index position + * @return {number} Element index number or -1 if missing + */ + indexOf(search: T, fromIndex?: number): number { + return super.findIndex((el: T) => isEqual(el, search), fromIndex); + } + /** * Create list of elements this set has in common with the comparison set * @param {ASet} set Set to compare against diff --git a/src/index.ts b/src/index.ts index ebf8a22..ee540ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import {PE} from './path-events.ts'; + export * from './array'; export * from './aset'; export * from './cache'; diff --git a/src/path-events.ts b/src/path-events.ts index b8b9ff0..f5c5c83 100644 --- a/src/path-events.ts +++ b/src/path-events.ts @@ -113,7 +113,7 @@ export class PathEvent { * @param {string | PathEvent} paths Events as strings or pre-parsed * @return {PathEvent} Final combined permission */ - static combine(paths: (string | PathEvent)[]): PathEvent { + static combine(...paths: (string | PathEvent)[]): PathEvent { let hitNone = false; const combined = paths.map(p => new PathEvent(p)) .toSorted((p1, p2) => { @@ -123,18 +123,10 @@ export class PathEvent { if(p.none) hitNone = true; if(!acc) return p; if(hitNone) return acc; - if(p.all) acc.all = true; - if(p.all || p.create) acc.create = true; - if(p.all || p.read) acc.read = true; - if(p.all || p.update) acc.update = true; - if(p.all || p.delete) acc.delete = true; acc.methods = [...acc.methods, ...p.methods]; return acc; }, null); - if(combined.all) combined.methods = ['*']; - if(combined.none) combined.methods = ['n']; - combined.methods = new ASet(combined.methods); // Make unique - combined.raw = PES`${combined.fullPath}:${combined.methods}`; + combined.methods = new ASet(combined.methods); return combined; } @@ -152,7 +144,7 @@ export class PathEvent { if(!r.fullPath && r.all) return true; const filtered = parsedTarget.filter(p => r.fullPath.startsWith(p.fullPath)); if(!filtered.length) return false; - const combined = PathEvent.combine(filtered); + const combined = PathEvent.combine(...filtered); return !combined.none && (combined.all || new ASet(combined.methods).intersection(new ASet(r.methods)).length); }); } @@ -197,8 +189,9 @@ export class PathEvent { */ static toString(path: string | string[], methods: Method | Method[]): string { let p = makeArray(path).filter(p => p != null).join('/'); + p = p?.trim().replaceAll(/\/{2,}/g, '/').replaceAll(/(^\/|\/$)/g, ''); if(methods?.length) p += `:${makeArray(methods).map(m => m.toLowerCase()).join('')}`; - return p?.trim().replaceAll(/\/{2,}/g, '/').replaceAll(/(^\/|\/$)/g, ''); + return p; } /** diff --git a/vite.config.ts b/vite.config.ts index 3fece4a..9a4f49c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -13,7 +13,7 @@ export default defineConfig({ } }, emptyOutDir: true, - minify: true, + minify: false, sourcemap: true }, plugins: [dts()],