| 11 | import assertNotInfinite from './utils/assertNotInfinite'; |
| 12 | |
| 13 | export class Set extends SetCollection { |
| 14 | // @pragma Construction |
| 15 | |
| 16 | constructor(value) { |
| 17 | // eslint-disable-next-line no-constructor-return |
| 18 | return value === undefined || value === null |
| 19 | ? emptySet() |
| 20 | : isSet(value) && !isOrdered(value) |
| 21 | ? value |
| 22 | : emptySet().withMutations((set) => { |
| 23 | const iter = SetCollection(value); |
| 24 | assertNotInfinite(iter.size); |
| 25 | iter.forEach((v) => set.add(v)); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | static of(/*...values*/) { |
| 30 | return this(arguments); |
| 31 | } |
| 32 | |
| 33 | static fromKeys(value) { |
| 34 | return this(KeyedCollection(value).keySeq()); |
| 35 | } |
| 36 | |
| 37 | static intersect(sets) { |
| 38 | sets = Collection(sets).toArray(); |
| 39 | return sets.length |
| 40 | ? SetPrototype.intersect.apply(Set(sets.pop()), sets) |
| 41 | : emptySet(); |
| 42 | } |
| 43 | |
| 44 | static union(sets) { |
| 45 | sets = Collection(sets).toArray(); |
| 46 | return sets.length |
| 47 | ? SetPrototype.union.apply(Set(sets.pop()), sets) |
| 48 | : emptySet(); |
| 49 | } |
| 50 | |
| 51 | toString() { |
| 52 | return this.__toString('Set {', '}'); |
| 53 | } |
| 54 | |
| 55 | // @pragma Access |
| 56 | |
| 57 | has(value) { |
| 58 | return this._map.has(value); |
| 59 | } |
| 60 | |
| 61 | // @pragma Modification |
| 62 | |
| 63 | add(value) { |
| 64 | return updateSet(this, this._map.set(value, value)); |
| 65 | } |
| 66 | |
| 67 | remove(value) { |
| 68 | return updateSet(this, this._map.remove(value)); |
| 69 | } |
| 70 | |