JAVASCRIPT   117

Set js

Guest on 21st June 2022 05:51:10 PM

  1. "use strict";
  2.  
  3. function Set(array) {
  4.         if (!this) throw TypeError("Constructor Set requires 'new'");
  5.         var _set = this._set = Object.create(null);
  6.         if (array)
  7.                 array.forEach(function (value) { _set[value] = value; });
  8. }
  9.  
  10. Set.prototype.clear = function (value) {
  11.         self._set = Object.create(null);
  12.         return undefined;
  13. }
  14.  
  15. Set.prototype.add = function (value) {
  16.         this._set[value] = value;
  17.         return this;
  18. }
  19.  
  20. Set.prototype.has = function (value) {
  21.         return value in this._set;
  22. }
  23.  
  24. Set.prototype.delete = function (value) {
  25.         if (value in this._set) {
  26.                 delete this._set[value];
  27.                 return true;
  28.         }
  29.         return false;
  30. }
  31.  
  32. Set.prototype.keys = function () {
  33.         var _set = this._set;
  34.         return Object.keys(this._set).map(function (value) { return _set[value]; });
  35. }
  36.  
  37. Set.prototype.values = function () {
  38.         return this.keys();
  39. }
  40.  
  41. Set.prototype.forEach = function (cb,ta) {
  42.         return this.keys().forEach(cb, ta);
  43. }
  44.  
  45. Set.prototype.entries = function () {
  46.         return this.keys().map(function (value) { return [value, value]; });
  47. }
  48.  
  49. Set.prototype.toString = function () {
  50.         return "Set { " + this.values().map(repr).join(", ") + " }";
  51. }

Raw Paste


Login or Register to edit or fork this paste. It's free.