diff --git a/package-lock.json b/package-lock.json index 7232003..ff40e81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@ztimson/utils", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@ztimson/utils", - "version": "0.3.0", + "version": "0.4.0", "license": "MIT", "devDependencies": { "@types/jest": "^29.5.12", diff --git a/package.json b/package.json index da70d9f..2f1c400 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.3.0", + "version": "0.4.0", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/aset.ts b/src/aset.ts new file mode 100644 index 0000000..72b7b50 --- /dev/null +++ b/src/aset.ts @@ -0,0 +1,58 @@ +export class ASet 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) { + return new ASet(this.reduce((acc, el) => { + if(!set.has(el)) acc.push(el); + return acc; + }, [])); + } + + has(el: T) { + return this.indexOf(el) != -1; + } + + intersection(set: ASet) { + return new ASet(this.reduce((acc, el) => { + if(set.has(el)) acc.push(el); + return acc; + }, [])); + } + + isDisjointFrom(set: ASet) { + return this.intersection(set).size == 0; + } + + isSubsetOf(set: ASet) { + return this.findIndex(el => !set.has(el)) == -1; + } + + isSuperset(set: ASet) { + return set.findIndex(el => !this.has(el)) == -1; + } + + symmetricDifference(set: ASet) { + return new ASet([...this.difference(set), ...set.difference(this)]); + } + + union(set: ASet | Array) { + return new ASet([...this, ...set]); + } +} diff --git a/src/index.ts b/src/index.ts index c162c4f..5a705c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from './array'; +export * from './aset.ts'; export * from './emitter'; export * from './errors'; export * from './logger';