@shgysk8zer0/polyfills 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/set.js CHANGED
@@ -1,73 +1,87 @@
1
- (function() {
2
- 'use strict';
1
+ /**
2
+ * @see https://github.com/tc39/proposal-set-methods
3
+ */
4
+ 'use strict';
3
5
 
4
- /**
5
- * @see https://github.com/tc39/proposal-set-methods
6
- */
6
+ const TYPE_ERR_MSG = 'Expected a set-like object.';
7
+ const SET_METHODS = ['has', 'keys'];
8
+ const SET_PROPS = ['size'];
9
+ const isSet = thing => thing instanceof Set;
10
+ const isSetLike = thing => isSet(thing) || (
11
+ typeof thing === 'object'
12
+ && thing !== null
13
+ && SET_METHODS.every(method => thing[method] instanceof Function)
14
+ && SET_PROPS.every(prop => prop in thing)
15
+ );
7
16
 
8
- if (! (Set.prototype.intersection instanceof Function)) {
9
- Set.prototype.intersection = function intersection(iterable) {
10
- if (! (iterable instanceof Set)) {
11
- iterable = new Set(iterable);
12
- }
17
+ if (! (Set.prototype.intersection instanceof Function)) {
18
+ Set.prototype.intersection = function intersection(set) {
19
+ if (! isSetLike(set)) {
20
+ throw new TypeError(TYPE_ERR_MSG);
21
+ }
13
22
 
14
- return new Set([...this].filter(item => iterable.has(item)));
15
- };
16
- }
23
+ return new Set([...this].filter(item => set.has(item)));
24
+ };
25
+ }
17
26
 
18
- if (! (Set.prototype.difference instanceof Function)) {
19
- Set.prototype.difference = function difference(iterable) {
20
- if (! (iterable instanceof Set)) {
21
- iterable = new Set(iterable);
22
- }
27
+ if (! (Set.prototype.difference instanceof Function)) {
28
+ Set.prototype.difference = function difference(set) {
29
+ if (! isSetLike(set)) {
30
+ throw new TypeError(TYPE_ERR_MSG);
31
+ }
23
32
 
24
- return new Set([...this].filter(item => ! iterable.has(item)));
25
- };
26
- }
33
+ return new Set([...this].filter(item => ! set.has(item)));
34
+ };
35
+ }
27
36
 
28
- if (! (Set.prototype.union instanceof Function)) {
29
- Set.prototype.union = function union(iterable) {
30
- return new Set([...this, ...iterable]);
31
- };
32
- }
37
+ if (! (Set.prototype.union instanceof Function)) {
38
+ Set.prototype.union = function union(set) {
39
+ return new Set([...this, ...set]);
40
+ };
41
+ }
33
42
 
34
- if (! (Set.prototype.isSubsetOf instanceof Function)) {
35
- Set.prototype.isSubsetOf = function isSubsetOf(iterable) {
36
- if (! (iterable instanceof Set)) {
37
- iterable = new Set(iterable);
38
- }
43
+ if (! (Set.prototype.isSubsetOf instanceof Function)) {
44
+ Set.prototype.isSubsetOf = function isSubsetOf(set) {
45
+ if (! isSetLike(set)) {
46
+ throw new TypeError(TYPE_ERR_MSG);
47
+ }
39
48
 
40
- return [...this].every(item => iterable.has(item));
41
- };
42
- }
49
+ return [...this].every(item => set.has(item));
50
+ };
51
+ }
43
52
 
44
- if (! (Set.prototype.isSupersetOf instanceof Function)) {
45
- Set.prototype.isSupersetOf = function isSupersetOf(iterable) {
46
- if (! (iterable instanceof Set)) {
47
- iterable = new Set(iterable);
48
- }
53
+ if (! (Set.prototype.isSupersetOf instanceof Function)) {
54
+ Set.prototype.isSupersetOf = function isSupersetOf(set) {
55
+ if (! isSetLike(set)) {
56
+ throw new TypeError(TYPE_ERR_MSG);
57
+ }
49
58
 
50
- return iterable.isSubsetOf(this);
51
- };
52
- }
59
+ // @TODO Handle when `set` is set-like
60
+ return isSet(set)
61
+ ? set.isSubsetOf(this)
62
+ : new Set(set.keys()).isSubsetOf(this);
63
+ };
64
+ }
53
65
 
54
- if (! (Set.prototype.symmetricDifference instanceof Function)) {
55
- Set.prototype.symmetricDifference = function symmetricDifference(iterable) {
56
- if (! (iterable instanceof Set)) {
57
- iterable = new Set(iterable);
58
- }
66
+ if (! (Set.prototype.symmetricDifference instanceof Function)) {
67
+ Set.prototype.symmetricDifference = function symmetricDifference(set) {
68
+ if (! isSetLike(set)) {
69
+ throw new TypeError(TYPE_ERR_MSG);
70
+ }
59
71
 
60
- return new Set([...this.difference(iterable), ...iterable.difference(this)]);
61
- };
62
- }
72
+ // @TODO Handle when `set` is set-like
73
+ return isSet(set)
74
+ ? new Set([...this.difference(set), ...set.difference(this)])
75
+ : new Set([...this.difference(set), ...new Set(set.keys()).difference(this)]);
76
+ };
77
+ }
63
78
 
64
- if (! (Set.prototype.isDisjointFrom instanceof Function)) {
65
- Set.prototype.isDisjointFrom = function isDisjointFrom(iterable) {
66
- if (! (iterable instanceof Set)) {
67
- iterable = new Set(iterable);
68
- }
79
+ if (! (Set.prototype.isDisjointFrom instanceof Function)) {
80
+ Set.prototype.isDisjointFrom = function isDisjointFrom(set) {
81
+ if (! isSetLike(set)) {
82
+ throw new TypeError(TYPE_ERR_MSG);
83
+ }
69
84
 
70
- return new Set([...this, ...iterable]).size === this.size + iterable.size;
71
- };
72
- }
73
- })();
85
+ return new Set([...this, ...set]).size === this.size + set.size;
86
+ };
87
+ }