Front end of the Slack clone application.

polyfill.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. var clear = require('es5-ext/array/#/clear')
  3. , eIndexOf = require('es5-ext/array/#/e-index-of')
  4. , setPrototypeOf = require('es5-ext/object/set-prototype-of')
  5. , callable = require('es5-ext/object/valid-callable')
  6. , d = require('d')
  7. , ee = require('event-emitter')
  8. , Symbol = require('es6-symbol')
  9. , iterator = require('es6-iterator/valid-iterable')
  10. , forOf = require('es6-iterator/for-of')
  11. , Iterator = require('./lib/iterator')
  12. , isNative = require('./is-native-implemented')
  13. , call = Function.prototype.call
  14. , defineProperty = Object.defineProperty, getPrototypeOf = Object.getPrototypeOf
  15. , SetPoly, getValues, NativeSet;
  16. if (isNative) NativeSet = Set;
  17. module.exports = SetPoly = function Set(/*iterable*/) {
  18. var iterable = arguments[0], self;
  19. if (!(this instanceof SetPoly)) throw new TypeError('Constructor requires \'new\'');
  20. if (isNative && setPrototypeOf) self = setPrototypeOf(new NativeSet(), getPrototypeOf(this));
  21. else self = this;
  22. if (iterable != null) iterator(iterable);
  23. defineProperty(self, '__setData__', d('c', []));
  24. if (!iterable) return self;
  25. forOf(iterable, function (value) {
  26. if (eIndexOf.call(this, value) !== -1) return;
  27. this.push(value);
  28. }, self.__setData__);
  29. return self;
  30. };
  31. if (isNative) {
  32. if (setPrototypeOf) setPrototypeOf(SetPoly, NativeSet);
  33. SetPoly.prototype = Object.create(NativeSet.prototype, { constructor: d(SetPoly) });
  34. }
  35. ee(Object.defineProperties(SetPoly.prototype, {
  36. add: d(function (value) {
  37. if (this.has(value)) return this;
  38. this.emit('_add', this.__setData__.push(value) - 1, value);
  39. return this;
  40. }),
  41. clear: d(function () {
  42. if (!this.__setData__.length) return;
  43. clear.call(this.__setData__);
  44. this.emit('_clear');
  45. }),
  46. delete: d(function (value) {
  47. var index = eIndexOf.call(this.__setData__, value);
  48. if (index === -1) return false;
  49. this.__setData__.splice(index, 1);
  50. this.emit('_delete', index, value);
  51. return true;
  52. }),
  53. entries: d(function () { return new Iterator(this, 'key+value'); }),
  54. forEach: d(function (cb/*, thisArg*/) {
  55. var thisArg = arguments[1], iterator, result, value;
  56. callable(cb);
  57. iterator = this.values();
  58. result = iterator._next();
  59. while (result !== undefined) {
  60. value = iterator._resolve(result);
  61. call.call(cb, thisArg, value, value, this);
  62. result = iterator._next();
  63. }
  64. }),
  65. has: d(function (value) {
  66. return (eIndexOf.call(this.__setData__, value) !== -1);
  67. }),
  68. keys: d(getValues = function () { return this.values(); }),
  69. size: d.gs(function () { return this.__setData__.length; }),
  70. values: d(function () { return new Iterator(this); }),
  71. toString: d(function () { return '[object Set]'; })
  72. }));
  73. defineProperty(SetPoly.prototype, Symbol.iterator, d(getValues));
  74. defineProperty(SetPoly.prototype, Symbol.toStringTag, d('c', 'Set'));