get-set-record.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var aCallable = require('../internals/a-callable');
  3. var anObject = require('../internals/an-object');
  4. var call = require('../internals/function-call');
  5. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  6. var getIteratorDirect = require('../internals/get-iterator-direct');
  7. var INVALID_SIZE = 'Invalid size';
  8. var $RangeError = RangeError;
  9. var $TypeError = TypeError;
  10. var max = Math.max;
  11. var SetRecord = function (set, intSize) {
  12. this.set = set;
  13. this.size = max(intSize, 0);
  14. this.has = aCallable(set.has);
  15. this.keys = aCallable(set.keys);
  16. };
  17. SetRecord.prototype = {
  18. getIterator: function () {
  19. return getIteratorDirect(anObject(call(this.keys, this.set)));
  20. },
  21. includes: function (it) {
  22. return call(this.has, this.set, it);
  23. }
  24. };
  25. // `GetSetRecord` abstract operation
  26. // https://tc39.es/proposal-set-methods/#sec-getsetrecord
  27. module.exports = function (obj) {
  28. anObject(obj);
  29. var numSize = +obj.size;
  30. // NOTE: If size is undefined, then numSize will be NaN
  31. // eslint-disable-next-line no-self-compare -- NaN check
  32. if (numSize !== numSize) throw new $TypeError(INVALID_SIZE);
  33. var intSize = toIntegerOrInfinity(numSize);
  34. if (intSize < 0) throw new $RangeError(INVALID_SIZE);
  35. return new SetRecord(obj, intSize);
  36. };