Added ASet
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 8s
Build / Publish (push) Successful in 15s

This commit is contained in:
Zakary Timson 2024-04-16 14:05:41 -04:00
parent d9844797ec
commit 18c4366866
4 changed files with 62 additions and 3 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.3.0", "version": "0.4.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.3.0", "version": "0.4.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",

View File

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

58
src/aset.ts Normal file
View File

@ -0,0 +1,58 @@
export class ASet<T> extends Array {
get size() {
return this.length;
}
constructor(elements: T[] = []) {
super();
if(!!elements?.['forEach'])
elements.forEach(el => this.add(el));
}
add(el: T) {
if(!this.has(el)) this.push(el);
}
delete(el: T) {
const index = this.indexOf(el);
if(index != -1) this.slice(index, 1);
}
difference(set: ASet<T>) {
return new ASet<T>(this.reduce((acc, el) => {
if(!set.has(el)) acc.push(el);
return acc;
}, []));
}
has(el: T) {
return this.indexOf(el) != -1;
}
intersection(set: ASet<T>) {
return new ASet<T>(this.reduce((acc, el) => {
if(set.has(el)) acc.push(el);
return acc;
}, []));
}
isDisjointFrom(set: ASet<T>) {
return this.intersection(set).size == 0;
}
isSubsetOf(set: ASet<T>) {
return this.findIndex(el => !set.has(el)) == -1;
}
isSuperset(set: ASet<T>) {
return set.findIndex(el => !this.has(el)) == -1;
}
symmetricDifference(set: ASet<T>) {
return new ASet([...this.difference(set), ...set.difference(this)]);
}
union(set: ASet<T> | Array<T>) {
return new ASet([...this, ...set]);
}
}

View File

@ -1,4 +1,5 @@
export * from './array'; export * from './array';
export * from './aset.ts';
export * from './emitter'; export * from './emitter';
export * from './errors'; export * from './errors';
export * from './logger'; export * from './logger';