1 | var __defProp = Object.defineProperty;
|
2 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
3 | var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
4 | var __publicField = (obj, key, value) => {
|
5 | __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
6 | return value;
|
7 | };
|
8 |
|
9 |
|
10 | var _Collection = class extends Map {
|
11 | ensure(key, defaultValueGenerator) {
|
12 | if (this.has(key))
|
13 | return this.get(key);
|
14 | const defaultValue = defaultValueGenerator(key, this);
|
15 | this.set(key, defaultValue);
|
16 | return defaultValue;
|
17 | }
|
18 | hasAll(...keys) {
|
19 | return keys.every((k) => super.has(k));
|
20 | }
|
21 | hasAny(...keys) {
|
22 | return keys.some((k) => super.has(k));
|
23 | }
|
24 | first(amount) {
|
25 | if (typeof amount === "undefined")
|
26 | return this.values().next().value;
|
27 | if (amount < 0)
|
28 | return this.last(amount * -1);
|
29 | amount = Math.min(this.size, amount);
|
30 | const iter = this.values();
|
31 | return Array.from({ length: amount }, () => iter.next().value);
|
32 | }
|
33 | firstKey(amount) {
|
34 | if (typeof amount === "undefined")
|
35 | return this.keys().next().value;
|
36 | if (amount < 0)
|
37 | return this.lastKey(amount * -1);
|
38 | amount = Math.min(this.size, amount);
|
39 | const iter = this.keys();
|
40 | return Array.from({ length: amount }, () => iter.next().value);
|
41 | }
|
42 | last(amount) {
|
43 | const arr = [...this.values()];
|
44 | if (typeof amount === "undefined")
|
45 | return arr[arr.length - 1];
|
46 | if (amount < 0)
|
47 | return this.first(amount * -1);
|
48 | if (!amount)
|
49 | return [];
|
50 | return arr.slice(-amount);
|
51 | }
|
52 | lastKey(amount) {
|
53 | const arr = [...this.keys()];
|
54 | if (typeof amount === "undefined")
|
55 | return arr[arr.length - 1];
|
56 | if (amount < 0)
|
57 | return this.firstKey(amount * -1);
|
58 | if (!amount)
|
59 | return [];
|
60 | return arr.slice(-amount);
|
61 | }
|
62 | at(index) {
|
63 | index = Math.floor(index);
|
64 | const arr = [...this.values()];
|
65 | return arr.at(index);
|
66 | }
|
67 | keyAt(index) {
|
68 | index = Math.floor(index);
|
69 | const arr = [...this.keys()];
|
70 | return arr.at(index);
|
71 | }
|
72 | random(amount) {
|
73 | const arr = [...this.values()];
|
74 | if (typeof amount === "undefined")
|
75 | return arr[Math.floor(Math.random() * arr.length)];
|
76 | if (!arr.length || !amount)
|
77 | return [];
|
78 | return Array.from({ length: Math.min(amount, arr.length) }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
|
79 | }
|
80 | randomKey(amount) {
|
81 | const arr = [...this.keys()];
|
82 | if (typeof amount === "undefined")
|
83 | return arr[Math.floor(Math.random() * arr.length)];
|
84 | if (!arr.length || !amount)
|
85 | return [];
|
86 | return Array.from({ length: Math.min(amount, arr.length) }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
|
87 | }
|
88 | reverse() {
|
89 | const entries = [...this.entries()].reverse();
|
90 | this.clear();
|
91 | for (const [key, value] of entries)
|
92 | this.set(key, value);
|
93 | return this;
|
94 | }
|
95 | find(fn, thisArg) {
|
96 | if (typeof thisArg !== "undefined")
|
97 | fn = fn.bind(thisArg);
|
98 | for (const [key, val] of this) {
|
99 | if (fn(val, key, this))
|
100 | return val;
|
101 | }
|
102 | return void 0;
|
103 | }
|
104 | findKey(fn, thisArg) {
|
105 | if (typeof thisArg !== "undefined")
|
106 | fn = fn.bind(thisArg);
|
107 | for (const [key, val] of this) {
|
108 | if (fn(val, key, this))
|
109 | return key;
|
110 | }
|
111 | return void 0;
|
112 | }
|
113 | sweep(fn, thisArg) {
|
114 | if (typeof thisArg !== "undefined")
|
115 | fn = fn.bind(thisArg);
|
116 | const previousSize = this.size;
|
117 | for (const [key, val] of this) {
|
118 | if (fn(val, key, this))
|
119 | this.delete(key);
|
120 | }
|
121 | return previousSize - this.size;
|
122 | }
|
123 | filter(fn, thisArg) {
|
124 | if (typeof thisArg !== "undefined")
|
125 | fn = fn.bind(thisArg);
|
126 | const results = new this.constructor[Symbol.species]();
|
127 | for (const [key, val] of this) {
|
128 | if (fn(val, key, this))
|
129 | results.set(key, val);
|
130 | }
|
131 | return results;
|
132 | }
|
133 | partition(fn, thisArg) {
|
134 | if (typeof thisArg !== "undefined")
|
135 | fn = fn.bind(thisArg);
|
136 | const results = [
|
137 | new this.constructor[Symbol.species](),
|
138 | new this.constructor[Symbol.species]()
|
139 | ];
|
140 | for (const [key, val] of this) {
|
141 | if (fn(val, key, this)) {
|
142 | results[0].set(key, val);
|
143 | } else {
|
144 | results[1].set(key, val);
|
145 | }
|
146 | }
|
147 | return results;
|
148 | }
|
149 | flatMap(fn, thisArg) {
|
150 | const collections = this.map(fn, thisArg);
|
151 | return new this.constructor[Symbol.species]().concat(...collections);
|
152 | }
|
153 | map(fn, thisArg) {
|
154 | if (typeof thisArg !== "undefined")
|
155 | fn = fn.bind(thisArg);
|
156 | const iter = this.entries();
|
157 | return Array.from({ length: this.size }, () => {
|
158 | const [key, value] = iter.next().value;
|
159 | return fn(value, key, this);
|
160 | });
|
161 | }
|
162 | mapValues(fn, thisArg) {
|
163 | if (typeof thisArg !== "undefined")
|
164 | fn = fn.bind(thisArg);
|
165 | const coll = new this.constructor[Symbol.species]();
|
166 | for (const [key, val] of this)
|
167 | coll.set(key, fn(val, key, this));
|
168 | return coll;
|
169 | }
|
170 | some(fn, thisArg) {
|
171 | if (typeof thisArg !== "undefined")
|
172 | fn = fn.bind(thisArg);
|
173 | for (const [key, val] of this) {
|
174 | if (fn(val, key, this))
|
175 | return true;
|
176 | }
|
177 | return false;
|
178 | }
|
179 | every(fn, thisArg) {
|
180 | if (typeof thisArg !== "undefined")
|
181 | fn = fn.bind(thisArg);
|
182 | for (const [key, val] of this) {
|
183 | if (!fn(val, key, this))
|
184 | return false;
|
185 | }
|
186 | return true;
|
187 | }
|
188 | reduce(fn, initialValue) {
|
189 | let accumulator;
|
190 | if (typeof initialValue !== "undefined") {
|
191 | accumulator = initialValue;
|
192 | for (const [key, val] of this)
|
193 | accumulator = fn(accumulator, val, key, this);
|
194 | return accumulator;
|
195 | }
|
196 | let first = true;
|
197 | for (const [key, val] of this) {
|
198 | if (first) {
|
199 | accumulator = val;
|
200 | first = false;
|
201 | continue;
|
202 | }
|
203 | accumulator = fn(accumulator, val, key, this);
|
204 | }
|
205 | if (first) {
|
206 | throw new TypeError("Reduce of empty collection with no initial value");
|
207 | }
|
208 | return accumulator;
|
209 | }
|
210 | each(fn, thisArg) {
|
211 | this.forEach(fn, thisArg);
|
212 | return this;
|
213 | }
|
214 | tap(fn, thisArg) {
|
215 | if (typeof thisArg !== "undefined")
|
216 | fn = fn.bind(thisArg);
|
217 | fn(this);
|
218 | return this;
|
219 | }
|
220 | clone() {
|
221 | return new this.constructor[Symbol.species](this);
|
222 | }
|
223 | concat(...collections) {
|
224 | const newColl = this.clone();
|
225 | for (const coll of collections) {
|
226 | for (const [key, val] of coll)
|
227 | newColl.set(key, val);
|
228 | }
|
229 | return newColl;
|
230 | }
|
231 | equals(collection) {
|
232 | if (!collection)
|
233 | return false;
|
234 | if (this === collection)
|
235 | return true;
|
236 | if (this.size !== collection.size)
|
237 | return false;
|
238 | for (const [key, value] of this) {
|
239 | if (!collection.has(key) || value !== collection.get(key)) {
|
240 | return false;
|
241 | }
|
242 | }
|
243 | return true;
|
244 | }
|
245 | sort(compareFunction = _Collection.defaultSort) {
|
246 | const entries = [...this.entries()];
|
247 | entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
248 | super.clear();
|
249 | for (const [k, v] of entries) {
|
250 | super.set(k, v);
|
251 | }
|
252 | return this;
|
253 | }
|
254 | intersect(other) {
|
255 | const coll = new this.constructor[Symbol.species]();
|
256 | for (const [k, v] of other) {
|
257 | if (this.has(k) && Object.is(v, this.get(k))) {
|
258 | coll.set(k, v);
|
259 | }
|
260 | }
|
261 | return coll;
|
262 | }
|
263 | difference(other) {
|
264 | const coll = new this.constructor[Symbol.species]();
|
265 | for (const [k, v] of other) {
|
266 | if (!this.has(k))
|
267 | coll.set(k, v);
|
268 | }
|
269 | for (const [k, v] of this) {
|
270 | if (!other.has(k))
|
271 | coll.set(k, v);
|
272 | }
|
273 | return coll;
|
274 | }
|
275 | merge(other, whenInSelf, whenInOther, whenInBoth) {
|
276 | const coll = new this.constructor[Symbol.species]();
|
277 | const keys = new Set([...this.keys(), ...other.keys()]);
|
278 | for (const k of keys) {
|
279 | const hasInSelf = this.has(k);
|
280 | const hasInOther = other.has(k);
|
281 | if (hasInSelf && hasInOther) {
|
282 | const r = whenInBoth(this.get(k), other.get(k), k);
|
283 | if (r.keep)
|
284 | coll.set(k, r.value);
|
285 | } else if (hasInSelf) {
|
286 | const r = whenInSelf(this.get(k), k);
|
287 | if (r.keep)
|
288 | coll.set(k, r.value);
|
289 | } else if (hasInOther) {
|
290 | const r = whenInOther(other.get(k), k);
|
291 | if (r.keep)
|
292 | coll.set(k, r.value);
|
293 | }
|
294 | }
|
295 | return coll;
|
296 | }
|
297 | sorted(compareFunction = _Collection.defaultSort) {
|
298 | return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
299 | }
|
300 | toJSON() {
|
301 | return [...this.values()];
|
302 | }
|
303 | static defaultSort(firstValue, secondValue) {
|
304 | return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
305 | }
|
306 | static combineEntries(entries, combine) {
|
307 | const coll = new _Collection();
|
308 | for (const [k, v] of entries) {
|
309 | if (coll.has(k)) {
|
310 | coll.set(k, combine(coll.get(k), v, k));
|
311 | } else {
|
312 | coll.set(k, v);
|
313 | }
|
314 | }
|
315 | return coll;
|
316 | }
|
317 | };
|
318 | var Collection = _Collection;
|
319 | __name(Collection, "Collection");
|
320 | __publicField(Collection, "default", _Collection);
|
321 | var src_default = Collection;
|
322 | export {
|
323 | Collection,
|
324 | src_default as default
|
325 | };
|
326 |
|
\ | No newline at end of file |