get-set-record.js 1.0 KB

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