get-set-record.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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, size, has, keys) {
  12. this.set = set;
  13. this.size = size;
  14. this.has = has;
  15. this.keys = 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(
  36. obj,
  37. max(intSize, 0),
  38. aCallable(obj.has),
  39. aCallable(obj.keys)
  40. );
  41. };