Added ASet
This commit is contained in:
parent
d9844797ec
commit
18c4366866
4
package-lock.json
generated
4
package-lock.json
generated
@ -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",
|
||||||
|
@ -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
58
src/aset.ts
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
@ -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';
|
||||||
|
Loading…
Reference in New Issue
Block a user