UNPKG

1.57 kBJavaScriptView Raw
1import { AbstractSet } from "../interface/AbstractSet";
2export class ArraySet extends AbstractSet {
3 constructor() {
4 super();
5 this.set = [];
6 this.count = 0;
7 }
8 get Size() {
9 return this.count;
10 }
11 has(item) {
12 if (item === undefined) {
13 return this.set.indexOf(undefined) > -1;
14 }
15 if (!isNaN(item)) {
16 return this.set.findIndex(model => JSON.stringify(model) === JSON.stringify(item) && !isNaN(model)) > -1;
17 }
18 return this.set.findIndex(model => JSON.stringify(model) === JSON.stringify(item)) > -1;
19 }
20 findIndex(item) {
21 if (item === undefined) {
22 return this.set.indexOf(item);
23 }
24 if (!isNaN(item)) {
25 return this.set.findIndex(model => JSON.stringify(model) === JSON.stringify(item)
26 && !isNaN(model));
27 }
28 return this.set.findIndex(model => JSON.stringify(model) === JSON.stringify(item));
29 }
30 add(item) {
31 if (!this.has(item)) {
32 this.set.push(item);
33 this.count++;
34 }
35 return this;
36 }
37 entries() {
38 return this.set;
39 }
40 remove(item) {
41 const index = this.findIndex(item);
42 if (index > -1) {
43 this.set.splice(index, 1);
44 this.count--;
45 return true;
46 }
47 return false;
48 }
49 union(set) {
50 return super.union(set);
51 }
52 intersect(set) {
53 return super.intersect(set);
54 }
55 diff(set) {
56 return super.diff(set);
57 }
58}