@limitlesspc/std 0.20.2
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/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/array.d.ts +209 -0
- package/dist/array.js +41 -0
- package/dist/async/index.d.ts +48 -0
- package/dist/async/index.js +155 -0
- package/dist/bytes.d.ts +38 -0
- package/dist/bytes.js +35 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-C2DS6YRJ.js +1664 -0
- package/dist/chunk-DO4NH5XG.js +523 -0
- package/dist/chunk-EWSJTMH2.js +68 -0
- package/dist/chunk-ILLWUQPY.js +139 -0
- package/dist/chunk-LJSF3QBT.js +122 -0
- package/dist/chunk-MG5VQSTV.js +89 -0
- package/dist/chunk-TMLWLR46.js +12 -0
- package/dist/chunk-WBSY6KRH.js +358 -0
- package/dist/cmath/index.d.ts +57 -0
- package/dist/cmath/index.js +187 -0
- package/dist/cmp.d.ts +11 -0
- package/dist/cmp.js +12 -0
- package/dist/csv.d.ts +3 -0
- package/dist/csv.js +16 -0
- package/dist/easing.d.ts +37 -0
- package/dist/easing.js +157 -0
- package/dist/events.d.ts +20 -0
- package/dist/events.js +67 -0
- package/dist/fn/index.d.ts +66 -0
- package/dist/fn/index.js +25 -0
- package/dist/gfx/index.d.ts +13 -0
- package/dist/gfx/index.js +68 -0
- package/dist/iter/index.d.ts +226 -0
- package/dist/iter/index.js +65 -0
- package/dist/math/index.d.ts +463 -0
- package/dist/math/index.js +129 -0
- package/dist/object.d.ts +72 -0
- package/dist/object.js +24 -0
- package/dist/random.d.ts +68 -0
- package/dist/random.js +22 -0
- package/dist/string/index.d.ts +62 -0
- package/dist/string/index.js +98 -0
- package/dist/structs/index.d.ts +184 -0
- package/dist/structs/index.js +24 -0
- package/dist/time/index.d.ts +35 -0
- package/dist/time/index.js +84 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +1 -0
- package/dist/vec3-D7Wuy2AZ.d.ts +70 -0
- package/package.json +111 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import {
|
|
2
|
+
swap
|
|
3
|
+
} from "./chunk-ILLWUQPY.js";
|
|
4
|
+
import {
|
|
5
|
+
ascend,
|
|
6
|
+
descend
|
|
7
|
+
} from "./chunk-TMLWLR46.js";
|
|
8
|
+
|
|
9
|
+
// src/structs/doubly-linked-list.ts
|
|
10
|
+
var DoublyLinkedList = class _DoublyLinkedList {
|
|
11
|
+
head;
|
|
12
|
+
tail;
|
|
13
|
+
size = 0;
|
|
14
|
+
static from(values) {
|
|
15
|
+
const list = new _DoublyLinkedList();
|
|
16
|
+
for (const value of values) {
|
|
17
|
+
list.push(value);
|
|
18
|
+
}
|
|
19
|
+
return list;
|
|
20
|
+
}
|
|
21
|
+
get length() {
|
|
22
|
+
return this.size;
|
|
23
|
+
}
|
|
24
|
+
*[Symbol.iterator]() {
|
|
25
|
+
let current = this.head;
|
|
26
|
+
while (current) {
|
|
27
|
+
yield current.value;
|
|
28
|
+
current = current.next;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
getNode(index) {
|
|
32
|
+
let current = this.head;
|
|
33
|
+
let i = 0;
|
|
34
|
+
while (current) {
|
|
35
|
+
if (i === index) {
|
|
36
|
+
return current;
|
|
37
|
+
}
|
|
38
|
+
current = current.next;
|
|
39
|
+
i++;
|
|
40
|
+
}
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
get(index) {
|
|
44
|
+
return this.getNode(index)?.value;
|
|
45
|
+
}
|
|
46
|
+
set(index, value) {
|
|
47
|
+
const node = this.getNode(index);
|
|
48
|
+
if (!node) {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
const { value: oldValue } = node;
|
|
52
|
+
node.value = value;
|
|
53
|
+
return oldValue;
|
|
54
|
+
}
|
|
55
|
+
push(value) {
|
|
56
|
+
const node = { value };
|
|
57
|
+
if (this.tail) {
|
|
58
|
+
this.tail.next = node;
|
|
59
|
+
this.tail = node;
|
|
60
|
+
} else {
|
|
61
|
+
this.head = node;
|
|
62
|
+
this.tail = node;
|
|
63
|
+
}
|
|
64
|
+
return ++this.size;
|
|
65
|
+
}
|
|
66
|
+
pop() {
|
|
67
|
+
if (!this.tail) {
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
const { value } = this.tail;
|
|
71
|
+
this.tail = this.tail.prev;
|
|
72
|
+
if (!this.tail) {
|
|
73
|
+
this.head = void 0;
|
|
74
|
+
}
|
|
75
|
+
this.size--;
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
unshift(value) {
|
|
79
|
+
const node = { value };
|
|
80
|
+
if (this.head) {
|
|
81
|
+
this.head.prev = node;
|
|
82
|
+
this.head = node;
|
|
83
|
+
} else {
|
|
84
|
+
this.head = node;
|
|
85
|
+
this.tail = node;
|
|
86
|
+
}
|
|
87
|
+
return ++this.size;
|
|
88
|
+
}
|
|
89
|
+
shift() {
|
|
90
|
+
if (!this.head) {
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
const { value } = this.head;
|
|
94
|
+
this.head = this.head.next;
|
|
95
|
+
if (!this.head) {
|
|
96
|
+
this.tail = void 0;
|
|
97
|
+
}
|
|
98
|
+
this.size--;
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
reverse() {
|
|
102
|
+
let current = this.head;
|
|
103
|
+
let previous;
|
|
104
|
+
let next;
|
|
105
|
+
while (current) {
|
|
106
|
+
({ next } = current);
|
|
107
|
+
current.next = previous;
|
|
108
|
+
current.prev = next;
|
|
109
|
+
previous = current;
|
|
110
|
+
current = next;
|
|
111
|
+
}
|
|
112
|
+
this.tail = this.head;
|
|
113
|
+
this.head = previous;
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
remove(index) {
|
|
117
|
+
const node = this.getNode(index);
|
|
118
|
+
if (!node) {
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
if (node.prev) {
|
|
122
|
+
node.prev.next = node.next;
|
|
123
|
+
}
|
|
124
|
+
if (node.next) {
|
|
125
|
+
node.next.prev = node.prev;
|
|
126
|
+
}
|
|
127
|
+
if (node === this.head) {
|
|
128
|
+
this.head = node.next;
|
|
129
|
+
}
|
|
130
|
+
if (node === this.tail) {
|
|
131
|
+
this.tail = node.prev;
|
|
132
|
+
}
|
|
133
|
+
this.size--;
|
|
134
|
+
return node.value;
|
|
135
|
+
}
|
|
136
|
+
splice(index, count, ...values) {
|
|
137
|
+
const removed = [];
|
|
138
|
+
for (let i = 0; i < count; i++) {
|
|
139
|
+
const value = this.remove(index);
|
|
140
|
+
if (value) {
|
|
141
|
+
removed.push(value);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
for (const value of values) {
|
|
145
|
+
this.set(index, value);
|
|
146
|
+
}
|
|
147
|
+
return removed;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// src/structs/heaps/heap.ts
|
|
152
|
+
var Heap = class _Heap {
|
|
153
|
+
constructor(compare, data = [], heapify = true) {
|
|
154
|
+
this.compare = compare;
|
|
155
|
+
if (heapify) {
|
|
156
|
+
this.data = [];
|
|
157
|
+
this.push(...data);
|
|
158
|
+
} else {
|
|
159
|
+
this.data = data;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
data;
|
|
163
|
+
get length() {
|
|
164
|
+
return this.data.length;
|
|
165
|
+
}
|
|
166
|
+
get height() {
|
|
167
|
+
return Math.floor(Math.log2(this.length)) + 1;
|
|
168
|
+
}
|
|
169
|
+
valueOf() {
|
|
170
|
+
return this.peek();
|
|
171
|
+
}
|
|
172
|
+
copy() {
|
|
173
|
+
return new _Heap(this.compare, [...this.data], false);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Returns the first value in the heap
|
|
177
|
+
*/
|
|
178
|
+
peek() {
|
|
179
|
+
return this.data[0];
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Removes the first value from the heap and returns it
|
|
183
|
+
*/
|
|
184
|
+
pop() {
|
|
185
|
+
const first = this.data[0];
|
|
186
|
+
swap(this.data, 0, this.length - 1);
|
|
187
|
+
this.data.pop();
|
|
188
|
+
this.heapify();
|
|
189
|
+
return first;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Inserts values into the heap and makes sure the first value is the min/max
|
|
193
|
+
* @param values
|
|
194
|
+
* @returns The new length of the heap
|
|
195
|
+
*/
|
|
196
|
+
push(...values) {
|
|
197
|
+
const { data, compare } = this;
|
|
198
|
+
for (const value of values) {
|
|
199
|
+
data.push(value);
|
|
200
|
+
let i = data.length - 1;
|
|
201
|
+
let parent = Math.floor((i - 1) / 2);
|
|
202
|
+
while (i > 0 && compare(data[i], data[parent]) < 0) {
|
|
203
|
+
swap(data, i, parent);
|
|
204
|
+
i = parent;
|
|
205
|
+
parent = Math.floor((i - 1) / 2);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return data.length;
|
|
209
|
+
}
|
|
210
|
+
*drain() {
|
|
211
|
+
while (this.length) {
|
|
212
|
+
yield this.pop();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
[Symbol.iterator]() {
|
|
216
|
+
return this.data.values();
|
|
217
|
+
}
|
|
218
|
+
heapify(i = 0) {
|
|
219
|
+
const { length, compare, data } = this;
|
|
220
|
+
const left = 2 * i + 1;
|
|
221
|
+
const right = 2 * i + 2;
|
|
222
|
+
let largest = i;
|
|
223
|
+
if (left < length && compare(data[left], data[largest]) < 0) {
|
|
224
|
+
largest = left;
|
|
225
|
+
}
|
|
226
|
+
if (right < length && compare(data[right], data[largest]) < 0) {
|
|
227
|
+
largest = right;
|
|
228
|
+
}
|
|
229
|
+
if (largest !== i) {
|
|
230
|
+
swap(data, i, largest);
|
|
231
|
+
this.heapify(largest);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// src/structs/heaps/max-heap.ts
|
|
237
|
+
var MaxHeap = class _MaxHeap extends Heap {
|
|
238
|
+
constructor(data, heapify) {
|
|
239
|
+
super(descend, data, heapify);
|
|
240
|
+
}
|
|
241
|
+
copy() {
|
|
242
|
+
return new _MaxHeap([...this.data], false);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/structs/heaps/min-heap.ts
|
|
247
|
+
var MinHeap = class _MinHeap extends Heap {
|
|
248
|
+
constructor(data, heapify) {
|
|
249
|
+
super(ascend, data, heapify);
|
|
250
|
+
}
|
|
251
|
+
copy() {
|
|
252
|
+
return new _MinHeap([...this.data], false);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// src/structs/linked-list.ts
|
|
257
|
+
var LinkedList = class _LinkedList {
|
|
258
|
+
node;
|
|
259
|
+
static from(values) {
|
|
260
|
+
const list = new _LinkedList();
|
|
261
|
+
let node;
|
|
262
|
+
for (const value of values) {
|
|
263
|
+
const child = { value };
|
|
264
|
+
if (node) {
|
|
265
|
+
node.next = child;
|
|
266
|
+
}
|
|
267
|
+
node = child;
|
|
268
|
+
}
|
|
269
|
+
list.node = node;
|
|
270
|
+
return list;
|
|
271
|
+
}
|
|
272
|
+
get length() {
|
|
273
|
+
let { node } = this;
|
|
274
|
+
let length = 0;
|
|
275
|
+
while (node) {
|
|
276
|
+
length++;
|
|
277
|
+
node = node.next;
|
|
278
|
+
}
|
|
279
|
+
return length;
|
|
280
|
+
}
|
|
281
|
+
*[Symbol.iterator]() {
|
|
282
|
+
let { node } = this;
|
|
283
|
+
while (node) {
|
|
284
|
+
yield node.value;
|
|
285
|
+
node = node.next;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
get tail() {
|
|
289
|
+
let { node } = this;
|
|
290
|
+
while (node) {
|
|
291
|
+
node = node.next;
|
|
292
|
+
}
|
|
293
|
+
return node;
|
|
294
|
+
}
|
|
295
|
+
getNode(index) {
|
|
296
|
+
let current = this.node;
|
|
297
|
+
let i = 0;
|
|
298
|
+
while (current) {
|
|
299
|
+
if (i === index) {
|
|
300
|
+
return current;
|
|
301
|
+
}
|
|
302
|
+
current = current.next;
|
|
303
|
+
i++;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
get(index) {
|
|
307
|
+
return this.getNode(index)?.value;
|
|
308
|
+
}
|
|
309
|
+
set(index, value) {
|
|
310
|
+
const node = this.getNode(index);
|
|
311
|
+
if (!node) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
const { value: oldValue } = node;
|
|
315
|
+
node.value = value;
|
|
316
|
+
return oldValue;
|
|
317
|
+
}
|
|
318
|
+
push(value) {
|
|
319
|
+
const { tail } = this;
|
|
320
|
+
const node = { value };
|
|
321
|
+
if (tail) {
|
|
322
|
+
tail.next = node;
|
|
323
|
+
} else {
|
|
324
|
+
this.node = node;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
pop() {
|
|
328
|
+
let current = this.node;
|
|
329
|
+
let parent;
|
|
330
|
+
while (current) {
|
|
331
|
+
if (!current.next) {
|
|
332
|
+
delete parent?.next;
|
|
333
|
+
return current;
|
|
334
|
+
}
|
|
335
|
+
parent = current;
|
|
336
|
+
current = current.next;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
unshift(value) {
|
|
340
|
+
const node = { value };
|
|
341
|
+
if (this.node) {
|
|
342
|
+
node.next = this.node;
|
|
343
|
+
this.node = node;
|
|
344
|
+
}
|
|
345
|
+
this.node = node;
|
|
346
|
+
}
|
|
347
|
+
shift() {
|
|
348
|
+
const { node } = this;
|
|
349
|
+
this.node = node?.next;
|
|
350
|
+
return node;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// src/structs/queue.ts
|
|
355
|
+
var Queue = class {
|
|
356
|
+
constructor(items = []) {
|
|
357
|
+
this.items = items;
|
|
358
|
+
}
|
|
359
|
+
get size() {
|
|
360
|
+
return this.items.length;
|
|
361
|
+
}
|
|
362
|
+
enqueue(item) {
|
|
363
|
+
return this.items.push(item);
|
|
364
|
+
}
|
|
365
|
+
dequeue() {
|
|
366
|
+
return this.items.shift();
|
|
367
|
+
}
|
|
368
|
+
*[Symbol.iterator]() {
|
|
369
|
+
let item;
|
|
370
|
+
while (item = this.dequeue()) {
|
|
371
|
+
yield item;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// src/structs/sorted-array.ts
|
|
377
|
+
var SortedArray = class _SortedArray {
|
|
378
|
+
constructor(data = [], compare = ascend, sort = true) {
|
|
379
|
+
this.data = data;
|
|
380
|
+
this.compare = compare;
|
|
381
|
+
if (sort) {
|
|
382
|
+
this.data.sort(compare);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
valueOf() {
|
|
386
|
+
return this.at(0);
|
|
387
|
+
}
|
|
388
|
+
keys() {
|
|
389
|
+
return this.data.keys();
|
|
390
|
+
}
|
|
391
|
+
values() {
|
|
392
|
+
return this.data.values();
|
|
393
|
+
}
|
|
394
|
+
entries() {
|
|
395
|
+
return this.data.entries();
|
|
396
|
+
}
|
|
397
|
+
[Symbol.iterator]() {
|
|
398
|
+
return this.values();
|
|
399
|
+
}
|
|
400
|
+
length() {
|
|
401
|
+
return this.data.length;
|
|
402
|
+
}
|
|
403
|
+
at(index) {
|
|
404
|
+
return this.data[index];
|
|
405
|
+
}
|
|
406
|
+
copy() {
|
|
407
|
+
return new _SortedArray([...this.data], this.compare, false);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Returns the index of the value if it exists, otherwise -1
|
|
411
|
+
* @param value
|
|
412
|
+
*/
|
|
413
|
+
indexOf(value) {
|
|
414
|
+
const { data } = this;
|
|
415
|
+
let low = 0;
|
|
416
|
+
let high = data.length - 1;
|
|
417
|
+
while (low <= high) {
|
|
418
|
+
const mid = Math.floor((low + high) / 2);
|
|
419
|
+
const cmp = this.compare(value, data[mid]);
|
|
420
|
+
if (cmp < 0) {
|
|
421
|
+
high = mid - 1;
|
|
422
|
+
} else if (cmp > 0) {
|
|
423
|
+
low = mid + 1;
|
|
424
|
+
} else {
|
|
425
|
+
return mid;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return -1;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Returns if the array has the value
|
|
432
|
+
* @param value
|
|
433
|
+
*/
|
|
434
|
+
has(value) {
|
|
435
|
+
return this.data.includes(value);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Binary inserts a value into the array while maintaining sorted order
|
|
439
|
+
* @param value
|
|
440
|
+
* @returns the index of where the value was inserted
|
|
441
|
+
*/
|
|
442
|
+
push(value) {
|
|
443
|
+
const { data, compare } = this;
|
|
444
|
+
let low = 0;
|
|
445
|
+
let high = data.length - 1;
|
|
446
|
+
while (low <= high) {
|
|
447
|
+
const mid = Math.floor((low + high) / 2);
|
|
448
|
+
const cmp = compare(value, data[mid]);
|
|
449
|
+
if (cmp < 0) {
|
|
450
|
+
high = mid - 1;
|
|
451
|
+
} else if (cmp > 0) {
|
|
452
|
+
low = mid + 1;
|
|
453
|
+
} else {
|
|
454
|
+
data.splice(mid, 0, value);
|
|
455
|
+
return mid;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
data.splice(low, 0, value);
|
|
459
|
+
return low;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// src/structs/unordered-array.ts
|
|
464
|
+
var UnorderedArray = class _UnorderedArray extends Array {
|
|
465
|
+
copy() {
|
|
466
|
+
return new _UnorderedArray(...this);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Inserts new elements at the end of an unordered array, and returns the new length of the unordered array.
|
|
470
|
+
* @param items Elements to insert at the end of the array.
|
|
471
|
+
*/
|
|
472
|
+
unshift(...items) {
|
|
473
|
+
return super.push(...items);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Removes the last element from an array and returns it.
|
|
477
|
+
* If the array is empty, undefined is returned and the array is not modified.
|
|
478
|
+
*/
|
|
479
|
+
shift() {
|
|
480
|
+
return super.pop();
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Removes an item by index
|
|
484
|
+
* @param index the index of the item to remove
|
|
485
|
+
* @returns the removed item, if it exists
|
|
486
|
+
*/
|
|
487
|
+
removeIndex(index) {
|
|
488
|
+
swap(this, index, this.length - 1);
|
|
489
|
+
return this.pop();
|
|
490
|
+
}
|
|
491
|
+
splice(start, deleteCount = this.length - start, ...items) {
|
|
492
|
+
const spliced = [];
|
|
493
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
494
|
+
const replacement = items[i];
|
|
495
|
+
let removed;
|
|
496
|
+
if (replacement) {
|
|
497
|
+
removed = this[start + i];
|
|
498
|
+
this[start + i] = replacement;
|
|
499
|
+
} else {
|
|
500
|
+
removed = this.removeIndex(start);
|
|
501
|
+
}
|
|
502
|
+
if (removed) {
|
|
503
|
+
spliced.push(removed);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return spliced;
|
|
507
|
+
}
|
|
508
|
+
toSpliced(start, deleteCount = this.length - start, ...items) {
|
|
509
|
+
const copy = this.copy();
|
|
510
|
+
copy.splice(start, deleteCount, ...items);
|
|
511
|
+
return copy;
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
export {
|
|
516
|
+
DoublyLinkedList,
|
|
517
|
+
MaxHeap,
|
|
518
|
+
MinHeap,
|
|
519
|
+
LinkedList,
|
|
520
|
+
Queue,
|
|
521
|
+
SortedArray,
|
|
522
|
+
UnorderedArray
|
|
523
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
swap,
|
|
3
|
+
unorderedRemove
|
|
4
|
+
} from "./chunk-ILLWUQPY.js";
|
|
5
|
+
|
|
6
|
+
// src/random.ts
|
|
7
|
+
function random(min, max) {
|
|
8
|
+
if (!min) {
|
|
9
|
+
return Math.random();
|
|
10
|
+
}
|
|
11
|
+
if (!max) {
|
|
12
|
+
return Math.random() * min;
|
|
13
|
+
}
|
|
14
|
+
const Min = Math.min(min, max);
|
|
15
|
+
return (Math.max(min, max) - Min) * Math.random() + Min;
|
|
16
|
+
}
|
|
17
|
+
function randomInt(min = 0, max) {
|
|
18
|
+
if (typeof max === "number") {
|
|
19
|
+
return Math.floor(random(min, max + 1));
|
|
20
|
+
}
|
|
21
|
+
return Math.floor(random(min + 1));
|
|
22
|
+
}
|
|
23
|
+
function shuffle(array) {
|
|
24
|
+
for (let { length } = array, i = length - 1; i > 0; i--) {
|
|
25
|
+
const j = randomInt(i);
|
|
26
|
+
swap(array, i, j);
|
|
27
|
+
}
|
|
28
|
+
return array;
|
|
29
|
+
}
|
|
30
|
+
function choice(array) {
|
|
31
|
+
return array[randomInt(array.length - 1)];
|
|
32
|
+
}
|
|
33
|
+
function choices(array, n) {
|
|
34
|
+
if (!array.length || !n) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
if (typeof array === "string") {
|
|
38
|
+
let result = "";
|
|
39
|
+
for (let i = 0; i < n; i++) {
|
|
40
|
+
result += choice(array);
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
return Array.from({ length: n }).map(
|
|
45
|
+
() => array[randomInt(array.length - 1)]
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
function sample(array, n) {
|
|
49
|
+
const copy = [...array];
|
|
50
|
+
const result = Array.from({ length: n });
|
|
51
|
+
for (let i = 0; i < n; i++) {
|
|
52
|
+
const index = randomInt(copy.length - 1);
|
|
53
|
+
const item = unorderedRemove(copy, index);
|
|
54
|
+
if (item) {
|
|
55
|
+
result[i] = item;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export {
|
|
62
|
+
random,
|
|
63
|
+
randomInt,
|
|
64
|
+
shuffle,
|
|
65
|
+
choice,
|
|
66
|
+
choices,
|
|
67
|
+
sample
|
|
68
|
+
};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
zip
|
|
3
|
+
} from "./chunk-WBSY6KRH.js";
|
|
4
|
+
import {
|
|
5
|
+
ascend
|
|
6
|
+
} from "./chunk-TMLWLR46.js";
|
|
7
|
+
|
|
8
|
+
// src/array.ts
|
|
9
|
+
function swap(array, i, j) {
|
|
10
|
+
const temp = array[i];
|
|
11
|
+
array[i] = array[j];
|
|
12
|
+
array[j] = temp;
|
|
13
|
+
}
|
|
14
|
+
function remove(array, item) {
|
|
15
|
+
const index = array.indexOf(item);
|
|
16
|
+
if (index === -1) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
array.splice(index, 1);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
function unorderedRemove(array, index) {
|
|
23
|
+
swap(array, index, array.length - 1);
|
|
24
|
+
return array.pop();
|
|
25
|
+
}
|
|
26
|
+
function intersection(...arrays) {
|
|
27
|
+
const [first, ...rest] = arrays;
|
|
28
|
+
if (!first) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
return first.filter((item) => rest.every((array) => array.includes(item)));
|
|
32
|
+
}
|
|
33
|
+
function added(oldArray, newArray) {
|
|
34
|
+
return newArray.filter((value) => !oldArray.includes(value));
|
|
35
|
+
}
|
|
36
|
+
function removed(oldArray, newArray) {
|
|
37
|
+
return oldArray.filter((value) => !newArray.includes(value));
|
|
38
|
+
}
|
|
39
|
+
function changes(oldArray, newArray) {
|
|
40
|
+
return [added(oldArray, newArray), removed(oldArray, newArray)];
|
|
41
|
+
}
|
|
42
|
+
function hasChange(a, b) {
|
|
43
|
+
if (a.length !== b.length) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
for (const item of a) {
|
|
47
|
+
if (!b.includes(item)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const item of b) {
|
|
52
|
+
if (!a.includes(item)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function difference(array1, array2) {
|
|
59
|
+
const [added2, removed2] = changes(array1, array2);
|
|
60
|
+
return [...added2, ...removed2];
|
|
61
|
+
}
|
|
62
|
+
function union(...arrays) {
|
|
63
|
+
const result = [];
|
|
64
|
+
const seen = /* @__PURE__ */ new Set();
|
|
65
|
+
for (const array of arrays) {
|
|
66
|
+
for (const item of array) {
|
|
67
|
+
if (!seen.has(item)) {
|
|
68
|
+
seen.add(item);
|
|
69
|
+
result.push(item);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
function equal(a, b) {
|
|
76
|
+
if (a.length !== b.length) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
for (const [item1, item2] of zip(a, b)) {
|
|
80
|
+
if (item1 !== item2) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
function includesAny(a, b) {
|
|
87
|
+
return b.some((item) => a.includes(item));
|
|
88
|
+
}
|
|
89
|
+
function includesAll(a, b) {
|
|
90
|
+
return b.every((item) => a.includes(item));
|
|
91
|
+
}
|
|
92
|
+
function dedupe(array, key) {
|
|
93
|
+
const result = [];
|
|
94
|
+
const values = /* @__PURE__ */ new Set();
|
|
95
|
+
for (const item of array) {
|
|
96
|
+
const value = item[key];
|
|
97
|
+
if (!values.has(value)) {
|
|
98
|
+
result.push(item);
|
|
99
|
+
values.add(value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
function filterByKey(array, key) {
|
|
105
|
+
return array.filter((item) => item[key]);
|
|
106
|
+
}
|
|
107
|
+
function sortByKeys(array, key, compare = ascend) {
|
|
108
|
+
return [...array].sort((a, b) => {
|
|
109
|
+
const keys = Array.isArray(key) ? key : [key];
|
|
110
|
+
for (const key2 of keys) {
|
|
111
|
+
const value1 = a[key2];
|
|
112
|
+
const value2 = b[key2];
|
|
113
|
+
const cmp = compare(value1, value2);
|
|
114
|
+
if (cmp) {
|
|
115
|
+
return cmp;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return 0;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
swap,
|
|
124
|
+
remove,
|
|
125
|
+
unorderedRemove,
|
|
126
|
+
intersection,
|
|
127
|
+
added,
|
|
128
|
+
removed,
|
|
129
|
+
changes,
|
|
130
|
+
hasChange,
|
|
131
|
+
difference,
|
|
132
|
+
union,
|
|
133
|
+
equal,
|
|
134
|
+
includesAny,
|
|
135
|
+
includesAll,
|
|
136
|
+
dedupe,
|
|
137
|
+
filterByKey,
|
|
138
|
+
sortByKeys
|
|
139
|
+
};
|