UI for Zipcoin Blue

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. var callable = require('es5-ext/object/valid-callable')
  3. , clear = require('es5-ext/object/clear')
  4. , setPrototypeOf = require('es5-ext/object/set-prototype-of')
  5. , d = require('d')
  6. , iterator = require('es6-iterator/valid-iterable')
  7. , forOf = require('es6-iterator/for-of')
  8. , Set = require('../polyfill')
  9. , Iterator = require('../lib/primitive-iterator')
  10. , isNative = require('../is-native-implemented')
  11. , create = Object.create, defineProperties = Object.defineProperties
  12. , defineProperty = Object.defineProperty, getPrototypeOf = Object.getPrototypeOf
  13. , hasOwnProperty = Object.prototype.hasOwnProperty
  14. , PrimitiveSet;
  15. module.exports = PrimitiveSet = function (/*iterable, serialize*/) {
  16. var iterable = arguments[0], serialize = arguments[1], self;
  17. if (!(this instanceof PrimitiveSet)) throw new TypeError('Constructor requires \'new\'');
  18. if (isNative && setPrototypeOf) self = setPrototypeOf(new Set(), getPrototypeOf(this));
  19. else self = this;
  20. if (iterable != null) iterator(iterable);
  21. if (serialize !== undefined) {
  22. callable(serialize);
  23. defineProperty(self, '_serialize', d('', serialize));
  24. }
  25. defineProperties(self, {
  26. __setData__: d('c', create(null)),
  27. __size__: d('w', 0)
  28. });
  29. if (!iterable) return self;
  30. forOf(iterable, function (value) {
  31. var key = self._serialize(value);
  32. if (key == null) throw new TypeError(value + " cannot be serialized");
  33. if (hasOwnProperty.call(self.__setData__, key)) return;
  34. self.__setData__[key] = value;
  35. ++self.__size__;
  36. });
  37. return self;
  38. };
  39. if (setPrototypeOf) setPrototypeOf(PrimitiveSet, Set);
  40. PrimitiveSet.prototype = create(Set.prototype, {
  41. constructor: d(PrimitiveSet),
  42. _serialize: d(function (value) {
  43. if (value && (typeof value.toString !== 'function')) return null;
  44. return String(value);
  45. }),
  46. add: d(function (value) {
  47. var key = this._serialize(value);
  48. if (key == null) throw new TypeError(value + " cannot be serialized");
  49. if (hasOwnProperty.call(this.__setData__, key)) return this;
  50. this.__setData__[key] = value;
  51. ++this.__size__;
  52. this.emit('_add', key);
  53. return this;
  54. }),
  55. clear: d(function () {
  56. if (!this.__size__) return;
  57. clear(this.__setData__);
  58. this.__size__ = 0;
  59. this.emit('_clear');
  60. }),
  61. delete: d(function (value) {
  62. var key = this._serialize(value);
  63. if (key == null) return false;
  64. if (!hasOwnProperty.call(this.__setData__, key)) return false;
  65. delete this.__setData__[key];
  66. --this.__size__;
  67. this.emit('_delete', key);
  68. return true;
  69. }),
  70. entries: d(function () { return new Iterator(this, 'key+value'); }),
  71. get: d(function (key) {
  72. key = this._serialize(key);
  73. if (key == null) return;
  74. return this.__setData__[key];
  75. }),
  76. has: d(function (value) {
  77. var key = this._serialize(value);
  78. if (key == null) return false;
  79. return hasOwnProperty.call(this.__setData__, key);
  80. }),
  81. size: d.gs(function () { return this.__size__; }),
  82. values: d(function () { return new Iterator(this); })
  83. });