Sha256: bc891b8bf3531157a4817e1476bcd1e718727c4393ab90d4da709ac2ef0091e1

Contents?: true

Size: 1 KB

Versions: 149

Compression:

Stored size: 1 KB

Contents

export default class CustomSet {
  constructor(data = []) {
    this.data = {};
    data.forEach(el => this.add(el));
  }

  add(el) {
    this.data[el] = el;
    return this;
  }

  delete(el) {
    delete this.data[el];
    return this;
  }

  size() {
    return Object.keys(this.data).length;
  }

  empty() {
    return this.size() === 0;
  }

  contains(el) {
    return this.data[el] !== undefined;
  }

  eql(other) {
    return this.size() === other.size() && this.difference(other).size() === 0;
  }

  difference(other) {
    return new CustomSet(Object.keys(this.data).filter(el => other.data[el] === undefined));
  }

  disjoint(other) {
    return this.size === 0 || this.difference(other).size() === this.size();
  }

  intersection(other) {
    return this.difference(this.difference(other));
  }

  union(other) {
    return new CustomSet(this.toList().concat(other.toList()));
  }

  subset(other) {
    return this.eql(this.intersection(other));
  }

  toList() {
    return Object.values(this.data);
  }
}

Version data entries

149 entries across 149 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/custom-set/example.js