source: node_modules/ramda/es/internal/_Set.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1import _includes from "./_includes.js";
2
3var _Set =
4/*#__PURE__*/
5function () {
6 function _Set() {
7 /* globals Set */
8 this._nativeSet = typeof Set === 'function' ? new Set() : null;
9 this._items = {};
10 }
11
12 // until we figure out why jsdoc chokes on this
13 // @param item The item to add to the Set
14 // @returns {boolean} true if the item did not exist prior, otherwise false
15 //
16 _Set.prototype.add = function (item) {
17 return !hasOrAdd(item, true, this);
18 }; //
19 // @param item The item to check for existence in the Set
20 // @returns {boolean} true if the item exists in the Set, otherwise false
21 //
22
23
24 _Set.prototype.has = function (item) {
25 return hasOrAdd(item, false, this);
26 }; //
27 // Combines the logic for checking whether an item is a member of the set and
28 // for adding a new item to the set.
29 //
30 // @param item The item to check or add to the Set instance.
31 // @param shouldAdd If true, the item will be added to the set if it doesn't
32 // already exist.
33 // @param set The set instance to check or add to.
34 // @return {boolean} true if the item already existed, otherwise false.
35 //
36
37
38 return _Set;
39}();
40
41function hasOrAdd(item, shouldAdd, set) {
42 var type = typeof item;
43 var prevSize, newSize;
44
45 switch (type) {
46 case 'string':
47 case 'number':
48 // distinguish between +0 and -0
49 if (item === 0 && 1 / item === -Infinity) {
50 if (set._items['-0']) {
51 return true;
52 } else {
53 if (shouldAdd) {
54 set._items['-0'] = true;
55 }
56
57 return false;
58 }
59 } // these types can all utilise the native Set
60
61
62 if (set._nativeSet !== null) {
63 if (shouldAdd) {
64 prevSize = set._nativeSet.size;
65
66 set._nativeSet.add(item);
67
68 newSize = set._nativeSet.size;
69 return newSize === prevSize;
70 } else {
71 return set._nativeSet.has(item);
72 }
73 } else {
74 if (!(type in set._items)) {
75 if (shouldAdd) {
76 set._items[type] = {};
77 set._items[type][item] = true;
78 }
79
80 return false;
81 } else if (item in set._items[type]) {
82 return true;
83 } else {
84 if (shouldAdd) {
85 set._items[type][item] = true;
86 }
87
88 return false;
89 }
90 }
91
92 case 'boolean':
93 // set._items['boolean'] holds a two element array
94 // representing [ falseExists, trueExists ]
95 if (type in set._items) {
96 var bIdx = item ? 1 : 0;
97
98 if (set._items[type][bIdx]) {
99 return true;
100 } else {
101 if (shouldAdd) {
102 set._items[type][bIdx] = true;
103 }
104
105 return false;
106 }
107 } else {
108 if (shouldAdd) {
109 set._items[type] = item ? [false, true] : [true, false];
110 }
111
112 return false;
113 }
114
115 case 'function':
116 // compare functions for reference equality
117 if (set._nativeSet !== null) {
118 if (shouldAdd) {
119 prevSize = set._nativeSet.size;
120
121 set._nativeSet.add(item);
122
123 newSize = set._nativeSet.size;
124 return newSize === prevSize;
125 } else {
126 return set._nativeSet.has(item);
127 }
128 } else {
129 if (!(type in set._items)) {
130 if (shouldAdd) {
131 set._items[type] = [item];
132 }
133
134 return false;
135 }
136
137 if (!_includes(item, set._items[type])) {
138 if (shouldAdd) {
139 set._items[type].push(item);
140 }
141
142 return false;
143 }
144
145 return true;
146 }
147
148 case 'undefined':
149 if (set._items[type]) {
150 return true;
151 } else {
152 if (shouldAdd) {
153 set._items[type] = true;
154 }
155
156 return false;
157 }
158
159 case 'object':
160 if (item === null) {
161 if (!set._items['null']) {
162 if (shouldAdd) {
163 set._items['null'] = true;
164 }
165
166 return false;
167 }
168
169 return true;
170 }
171
172 /* falls through */
173
174 default:
175 // reduce the search size of heterogeneous sets by creating buckets
176 // for each type.
177 type = Object.prototype.toString.call(item);
178
179 if (!(type in set._items)) {
180 if (shouldAdd) {
181 set._items[type] = [item];
182 }
183
184 return false;
185 } // scan through all previously applied items
186
187
188 if (!_includes(item, set._items[type])) {
189 if (shouldAdd) {
190 set._items[type].push(item);
191 }
192
193 return false;
194 }
195
196 return true;
197 }
198} // A simple Set type that honours R.equals semantics
199
200
201export default _Set;
Note: See TracBrowser for help on using the repository browser.