UNPKG

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