Sha256: f034961b3bdd750dd00842233887fc0c21ed6e9f9fa74d0afea062b334607cef

Contents?: true

Size: 1.18 KB

Versions: 211

Compression:

Stored size: 1.18 KB

Contents

export default class CustomSet {
  constructor(data = []) {
    this.data = {};
    data.forEach(el => this.put(el));
    this.comparator = typeof data[0] === 'number' ?
      (a, b) => a - b
      : (a, b) => b <= a
      ? 1 : -1;
  }

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

  eql(other) {
    return 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(this.difference(other).toList()));
  }

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

  toList() {
    return Object.keys(this.data).map(el =>  +el);
  }

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

  put(value) {
    this.data[value] = value;
    return this;
  }

  sort() {
    this.data.sort(this.comparator);
  }

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

  empty() {
    this.data = {};
    return this;
  }
}

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/custom-set/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/custom-set/example.js