Front end of the Slack clone application.

primitive-iterator.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var clear = require('es5-ext/array/#/clear')
  3. , assign = require('es5-ext/object/assign')
  4. , setPrototypeOf = require('es5-ext/object/set-prototype-of')
  5. , contains = require('es5-ext/string/#/contains')
  6. , d = require('d')
  7. , autoBind = require('d/auto-bind')
  8. , Iterator = require('es6-iterator')
  9. , toStringTagSymbol = require('es6-symbol').toStringTag
  10. , defineProperties = Object.defineProperties, keys = Object.keys
  11. , unBind = Iterator.prototype._unBind
  12. , PrimitiveSetIterator;
  13. PrimitiveSetIterator = module.exports = function (set, kind) {
  14. if (!(this instanceof PrimitiveSetIterator)) {
  15. return new PrimitiveSetIterator(set, kind);
  16. }
  17. Iterator.call(this, keys(set.__setData__), set);
  18. kind = (!kind || !contains.call(kind, 'key+value')) ? 'value' : 'key+value';
  19. defineProperties(this, {
  20. __kind__: d('', kind),
  21. __data__: d('w', set.__setData__)
  22. });
  23. };
  24. if (setPrototypeOf) setPrototypeOf(PrimitiveSetIterator, Iterator);
  25. PrimitiveSetIterator.prototype = Object.create(Iterator.prototype, assign({
  26. constructor: d(PrimitiveSetIterator),
  27. _resolve: d(function (i) {
  28. var value = this.__data__[this.__list__[i]];
  29. return (this.__kind__ === 'value') ? value : [value, value];
  30. }),
  31. _unBind: d(function () {
  32. this.__data__ = null;
  33. unBind.call(this);
  34. }),
  35. toString: d(function () { return '[object Set Iterator]'; })
  36. }, autoBind({
  37. _onAdd: d(function (key) { this.__list__.push(key); }),
  38. _onDelete: d(function (key) {
  39. var index = this.__list__.lastIndexOf(key);
  40. if (index < this.__nextIndex__) return;
  41. this.__list__.splice(index, 1);
  42. }),
  43. _onClear: d(function () {
  44. clear.call(this.__list__);
  45. this.__nextIndex__ = 0;
  46. })
  47. })));
  48. Object.defineProperty(PrimitiveSetIterator.prototype, toStringTagSymbol,
  49. d('c', 'Set Iterator'));