UNPKG

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