Updated ASet & 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 1m43s

This commit is contained in:
Zakary Timson 2024-10-17 10:24:18 -04:00
parent 1c1a3f6a6e
commit fbbe3c99ef
7 changed files with 53 additions and 15 deletions

12
.npmignore Normal file
View File

@ -0,0 +1,12 @@
src
tests
.editorconfig
.gitignore
.gitmodules
.npmignore
CODEOWNERS
Dockerfile
index.html
jest.config.js
tsconfig.json
vite.config.js

18
index.html Normal file
View File

@ -0,0 +1,18 @@
<!Doctype html>
<html>
<head>
<title>@ztimson/utils sandbox</title>
</head>
<body>
<script type="module">
import {PathEvent, PE} 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);
</script>
</body>
</html>

View File

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

View File

@ -1,3 +1,5 @@
import {isEqual} from './objects.ts';
/** /**
* An array which functions as a set. It guarantees unique elements * An array which functions as a set. It guarantees unique elements
* and provides set functions for comparisons * and provides set functions for comparisons
@ -34,7 +36,7 @@ export class ASet<T> extends Array {
delete(...items: T[]) { delete(...items: T[]) {
items.forEach(el => { items.forEach(el => {
const index = this.indexOf(el); const index = this.indexOf(el);
if(index != -1) this.slice(index, 1); if(index != -1) this.splice(index, 1);
}) })
return this; return this;
} }
@ -57,6 +59,17 @@ export class ASet<T> extends Array {
return this.indexOf(el) != -1; 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 * Create list of elements this set has in common with the comparison set
* @param {ASet<T>} set Set to compare against * @param {ASet<T>} set Set to compare against

View File

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

View File

@ -113,7 +113,7 @@ export class PathEvent {
* @param {string | PathEvent} paths Events as strings or pre-parsed * @param {string | PathEvent} paths Events as strings or pre-parsed
* @return {PathEvent} Final combined permission * @return {PathEvent} Final combined permission
*/ */
static combine(paths: (string | PathEvent)[]): PathEvent { static combine(...paths: (string | PathEvent)[]): PathEvent {
let hitNone = false; let hitNone = false;
const combined = paths.map(p => new PathEvent(p)) const combined = paths.map(p => new PathEvent(p))
.toSorted((p1, p2) => { .toSorted((p1, p2) => {
@ -123,18 +123,10 @@ export class PathEvent {
if(p.none) hitNone = true; if(p.none) hitNone = true;
if(!acc) return p; if(!acc) return p;
if(hitNone) return acc; 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]; acc.methods = [...acc.methods, ...p.methods];
return acc; return acc;
}, <any>null); }, <any>null);
if(combined.all) combined.methods = ['*']; combined.methods = new ASet<Method>(combined.methods);
if(combined.none) combined.methods = ['n'];
combined.methods = new ASet(combined.methods); // Make unique
combined.raw = PES`${combined.fullPath}:${combined.methods}`;
return combined; return combined;
} }
@ -152,7 +144,7 @@ export class PathEvent {
if(!r.fullPath && r.all) return true; if(!r.fullPath && r.all) return true;
const filtered = parsedTarget.filter(p => r.fullPath.startsWith(p.fullPath)); const filtered = parsedTarget.filter(p => r.fullPath.startsWith(p.fullPath));
if(!filtered.length) return false; 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); 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 { static toString(path: string | string[], methods: Method | Method[]): string {
let p = makeArray(path).filter(p => p != null).join('/'); 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('')}`; if(methods?.length) p += `:${makeArray(methods).map(m => m.toLowerCase()).join('')}`;
return p?.trim().replaceAll(/\/{2,}/g, '/').replaceAll(/(^\/|\/$)/g, ''); return p;
} }
/** /**

View File

@ -13,7 +13,7 @@ export default defineConfig({
} }
}, },
emptyOutDir: true, emptyOutDir: true,
minify: true, minify: false,
sourcemap: true sourcemap: true
}, },
plugins: [dts()], plugins: [dts()],