@r01al/array-polyfills 1.0.5 → 1.0.7
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/README.md +474 -2
- package/dist/auto.cjs +262 -0
- package/dist/auto.cjs.map +1 -1
- package/dist/auto.mjs +262 -0
- package/dist/auto.mjs.map +1 -1
- package/dist/auto.umd.js +530 -0
- package/dist/auto.umd.js.map +1 -0
- package/dist/functions/compact.d.ts +7 -0
- package/dist/functions/compact.js +7 -0
- package/dist/functions/compactMap.d.ts +6 -0
- package/dist/functions/compactMap.js +15 -0
- package/dist/functions/countBy.d.ts +6 -0
- package/dist/functions/countBy.js +17 -0
- package/dist/functions/difference.d.ts +6 -0
- package/dist/functions/difference.js +15 -0
- package/dist/functions/flatten.d.ts +5 -0
- package/dist/functions/flatten.js +14 -0
- package/dist/functions/groupBy.d.ts +6 -0
- package/dist/functions/groupBy.js +19 -0
- package/dist/functions/intersection.d.ts +6 -0
- package/dist/functions/intersection.js +11 -0
- package/dist/functions/pad.d.ts +7 -0
- package/dist/functions/pad.js +15 -0
- package/dist/functions/partition.d.ts +6 -0
- package/dist/functions/partition.js +17 -0
- package/dist/functions/pluck.d.ts +6 -0
- package/dist/functions/pluck.js +8 -0
- package/dist/functions/sample.d.ts +6 -0
- package/dist/functions/sample.js +16 -0
- package/dist/functions/sortBy.d.ts +6 -0
- package/dist/functions/sortBy.js +20 -0
- package/dist/functions/union.d.ts +6 -0
- package/dist/functions/union.js +22 -0
- package/dist/functions/uniqBy.d.ts +6 -0
- package/dist/functions/uniqBy.js +21 -0
- package/dist/functions/zip.d.ts +6 -0
- package/dist/functions/zip.js +15 -0
- package/dist/index.cjs +289 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +30 -0
- package/dist/index.js +42 -11
- package/dist/index.mjs +275 -12
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +580 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/polyfills/array.d.ts +15 -0
- package/dist/polyfills/array.js +30 -0
- package/package.json +2 -1
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ArrayPolyfills = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/******************************************************************************
|
|
8
|
+
Copyright (c) Microsoft Corporation.
|
|
9
|
+
|
|
10
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
+
purpose with or without fee is hereby granted.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
+
***************************************************************************** */
|
|
21
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function __spreadArray(to, from, pack) {
|
|
25
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
26
|
+
if (ar || !(i in from)) {
|
|
27
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
28
|
+
ar[i] = from[i];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
35
|
+
var e = new Error(message);
|
|
36
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @returns the first element of the array, or undefined if the array is empty.
|
|
41
|
+
*/
|
|
42
|
+
function _first () { return this.length ? this[0] : undefined; }
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @returns the last element of the array, or undefined if the array is empty.
|
|
46
|
+
*/
|
|
47
|
+
function _last () { return this.length ? this[this.length - 1] : undefined; }
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @returns a new array with only unique elements from the original array.
|
|
51
|
+
* Supports deep comparison for objects.
|
|
52
|
+
* Uses a map for improved performance with primitives.
|
|
53
|
+
*/
|
|
54
|
+
function deepEqual(a, b) {
|
|
55
|
+
if (a === b)
|
|
56
|
+
return true;
|
|
57
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
58
|
+
return false;
|
|
59
|
+
var keysA = Object.keys(a), keysB = Object.keys(b);
|
|
60
|
+
if (keysA.length !== keysB.length)
|
|
61
|
+
return false;
|
|
62
|
+
for (var _i = 0, keysA_1 = keysA; _i < keysA_1.length; _i++) {
|
|
63
|
+
var key = keysA_1[_i];
|
|
64
|
+
if (!keysB.includes(key) || !deepEqual(a[key], b[key]))
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
function _unique () {
|
|
70
|
+
var result = [];
|
|
71
|
+
var primitiveMap = new Map();
|
|
72
|
+
var _loop_1 = function (item) {
|
|
73
|
+
if (item === null || typeof item !== "object") {
|
|
74
|
+
if (!primitiveMap.has(item)) {
|
|
75
|
+
primitiveMap.set(item, true);
|
|
76
|
+
result.push(item);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
if (!result.some(function (existing) { return deepEqual(existing, item); })) {
|
|
81
|
+
result.push(item);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
for (var _i = 0, _a = this; _i < _a.length; _i++) {
|
|
86
|
+
var item = _a[_i];
|
|
87
|
+
_loop_1(item);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Splits the array into smaller arrays of the given size.
|
|
94
|
+
* @param size Number of items per chunk
|
|
95
|
+
* @returns Array of chunks
|
|
96
|
+
*/
|
|
97
|
+
function _chunk (size) {
|
|
98
|
+
if (!Number.isInteger(size) || size <= 0)
|
|
99
|
+
throw new Error("size must be a positive integer");
|
|
100
|
+
var out = [];
|
|
101
|
+
for (var i = 0; i < this.length; i += size)
|
|
102
|
+
out.push(this.slice(i, i + size));
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Removes falsy values from the array.
|
|
108
|
+
* @returns A new array without falsy values.
|
|
109
|
+
*/
|
|
110
|
+
function _compact () {
|
|
111
|
+
return this.filter(function (value) { return Boolean(value); });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Maps items and removes null/undefined results.
|
|
116
|
+
* @param mapper Function to map items.
|
|
117
|
+
* @returns A new array of mapped values without null/undefined.
|
|
118
|
+
*/
|
|
119
|
+
function _compactMap (mapper) {
|
|
120
|
+
var out = [];
|
|
121
|
+
for (var index = 0; index < this.length; index++) {
|
|
122
|
+
var value = mapper(this[index], index, this);
|
|
123
|
+
if (value !== null && value !== undefined) {
|
|
124
|
+
out.push(value);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @returns a random element from the array, or undefined if the array is empty.
|
|
132
|
+
*/
|
|
133
|
+
function _random () {
|
|
134
|
+
return this.length ? this[Math.floor(Math.random() * this.length)] : undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Creates an object mapping each value of the given key to the corresponding value from the array of objects.
|
|
139
|
+
* @param key The property name to use as keys in the result object.
|
|
140
|
+
* @param value The property name to use as values in the result object.
|
|
141
|
+
* @returns An object mapping key values to value values.
|
|
142
|
+
*/
|
|
143
|
+
function _keyValueMap (key, value) {
|
|
144
|
+
if (!key) {
|
|
145
|
+
throw new Error('keyValueMap: key is required');
|
|
146
|
+
}
|
|
147
|
+
if (!value) {
|
|
148
|
+
throw new Error("keyValueMap: value is required");
|
|
149
|
+
}
|
|
150
|
+
if (this.some(function (el) { return !el; })) {
|
|
151
|
+
throw new Error('keyValueMap: Array contains falsy values');
|
|
152
|
+
}
|
|
153
|
+
if (this.some(function (el) { return typeof el !== 'object'; })) {
|
|
154
|
+
throw new Error('keyValueMap: Array contains non-object values');
|
|
155
|
+
}
|
|
156
|
+
if (this.some(function (el) { return !el.hasOwnProperty(key); })) {
|
|
157
|
+
throw new Error("keyValueMap: key \"".concat(key, "\" does not exist on all objects"));
|
|
158
|
+
}
|
|
159
|
+
var map = {};
|
|
160
|
+
for (var index = 0; index < this.length; index++) {
|
|
161
|
+
var element = this[index];
|
|
162
|
+
map[element[key]] = element[value];
|
|
163
|
+
}
|
|
164
|
+
return map;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Calculates the sum of all numbers in an array.
|
|
169
|
+
* @returns The sum of all numbers in the array.
|
|
170
|
+
* @throws Will throw an error if any element in the array is not a number.
|
|
171
|
+
* @example
|
|
172
|
+
* [1, 2, 3].sum() // returns 6
|
|
173
|
+
* [10, -2, 5].sum() // returns 13
|
|
174
|
+
* [1, '2', 3].sum() // throws Error
|
|
175
|
+
*/
|
|
176
|
+
function _sum () {
|
|
177
|
+
if (this.some(function (el) { return typeof el !== 'number'; })) {
|
|
178
|
+
throw new Error('All elements must be numbers');
|
|
179
|
+
}
|
|
180
|
+
var sum = 0;
|
|
181
|
+
for (var _i = 0, _a = this; _i < _a.length; _i++) {
|
|
182
|
+
var num = _a[_i];
|
|
183
|
+
sum += num;
|
|
184
|
+
}
|
|
185
|
+
return sum;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Calculates the average of all numbers in an array.
|
|
190
|
+
* @returns The average value.
|
|
191
|
+
* @throws If any element is not a number.
|
|
192
|
+
*/
|
|
193
|
+
function _avg () {
|
|
194
|
+
if (this.length === 0)
|
|
195
|
+
return undefined;
|
|
196
|
+
if (this.some(function (el) { return typeof el !== 'number'; })) {
|
|
197
|
+
throw new Error('All elements must be numbers');
|
|
198
|
+
}
|
|
199
|
+
return this.reduce(function (a, b) { return a + b; }, 0) / this.length;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Returns the maximum number in the array.
|
|
204
|
+
* @returns The maximum value.
|
|
205
|
+
* @throws If any element is not a number.
|
|
206
|
+
*/
|
|
207
|
+
function _max () {
|
|
208
|
+
if (this.length === 0)
|
|
209
|
+
return undefined;
|
|
210
|
+
if (this.some(function (el) { return typeof el !== 'number'; })) {
|
|
211
|
+
throw new Error('All elements must be numbers');
|
|
212
|
+
}
|
|
213
|
+
return Math.max.apply(Math, this);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Returns the minimum number in the array.
|
|
218
|
+
* @returns The minimum value.
|
|
219
|
+
* @throws If any element is not a number.
|
|
220
|
+
*/
|
|
221
|
+
function _min () {
|
|
222
|
+
if (this.length === 0)
|
|
223
|
+
return undefined;
|
|
224
|
+
if (this.some(function (el) { return typeof el !== 'number'; })) {
|
|
225
|
+
throw new Error('All elements must be numbers');
|
|
226
|
+
}
|
|
227
|
+
return Math.min.apply(Math, this);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function shuffle$1() {
|
|
231
|
+
var _a;
|
|
232
|
+
for (var i = this.length - 1; i > 0; i--) {
|
|
233
|
+
var j = Math.floor(Math.random() * (i + 1));
|
|
234
|
+
_a = [this[j], this[i]], this[i] = _a[0], this[j] = _a[1];
|
|
235
|
+
}
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Groups array items by a key or mapper function.
|
|
241
|
+
* @param key The property name or mapper function to group by.
|
|
242
|
+
* @returns An object where keys are group identifiers and values are arrays of items.
|
|
243
|
+
*/
|
|
244
|
+
function _groupBy (key) {
|
|
245
|
+
var result = {};
|
|
246
|
+
var getKey = typeof key === "function"
|
|
247
|
+
? key
|
|
248
|
+
: function (item) { return item[key]; };
|
|
249
|
+
for (var index = 0; index < this.length; index++) {
|
|
250
|
+
var item = this[index];
|
|
251
|
+
var groupKey = getKey(item, index, this);
|
|
252
|
+
if (!result[groupKey])
|
|
253
|
+
result[groupKey] = [];
|
|
254
|
+
result[groupKey].push(item);
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Flattens the array by one level.
|
|
261
|
+
* @returns A new flattened array.
|
|
262
|
+
*/
|
|
263
|
+
function _flatten () {
|
|
264
|
+
var out = [];
|
|
265
|
+
for (var _i = 0, _a = this; _i < _a.length; _i++) {
|
|
266
|
+
var item = _a[_i];
|
|
267
|
+
if (Array.isArray(item))
|
|
268
|
+
out.push.apply(out, item);
|
|
269
|
+
else
|
|
270
|
+
out.push(item);
|
|
271
|
+
}
|
|
272
|
+
return out;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Zips the array with other arrays.
|
|
277
|
+
* @param arrays Arrays to zip with.
|
|
278
|
+
* @returns An array of tuples, truncated to the shortest length.
|
|
279
|
+
*/
|
|
280
|
+
function _zip () {
|
|
281
|
+
var arrays = [];
|
|
282
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
283
|
+
arrays[_i] = arguments[_i];
|
|
284
|
+
}
|
|
285
|
+
if (arrays.length === 0)
|
|
286
|
+
return this.map(function (item) { return [item]; });
|
|
287
|
+
var minLength = Math.min.apply(Math, __spreadArray([this.length], arrays.map(function (arr) { return arr.length; }), false));
|
|
288
|
+
var out = [];
|
|
289
|
+
var _loop_1 = function (index) {
|
|
290
|
+
out.push(__spreadArray([this_1[index]], arrays.map(function (arr) { return arr[index]; }), true));
|
|
291
|
+
};
|
|
292
|
+
var this_1 = this;
|
|
293
|
+
for (var index = 0; index < minLength; index++) {
|
|
294
|
+
_loop_1(index);
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Splits the array into two arrays based on a predicate.
|
|
301
|
+
* @param predicate Function to test each element.
|
|
302
|
+
* @returns A tuple: [items that pass, items that fail].
|
|
303
|
+
*/
|
|
304
|
+
function _partition (predicate) {
|
|
305
|
+
var pass = [];
|
|
306
|
+
var fail = [];
|
|
307
|
+
for (var index = 0; index < this.length; index++) {
|
|
308
|
+
var item = this[index];
|
|
309
|
+
if (predicate(item, index, this))
|
|
310
|
+
pass.push(item);
|
|
311
|
+
else
|
|
312
|
+
fail.push(item);
|
|
313
|
+
}
|
|
314
|
+
return [pass, fail];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Plucks a property from each item in the array.
|
|
319
|
+
* @param key Property name to pluck.
|
|
320
|
+
* @returns A new array of property values.
|
|
321
|
+
*/
|
|
322
|
+
function _pluck (key) {
|
|
323
|
+
return this.map(function (item) { return item[key]; });
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Counts items by a key or mapper function.
|
|
328
|
+
* @param key The property name or mapper function to count by.
|
|
329
|
+
* @returns An object where keys are group identifiers and values are counts.
|
|
330
|
+
*/
|
|
331
|
+
function _countBy (key) {
|
|
332
|
+
var _a;
|
|
333
|
+
var result = {};
|
|
334
|
+
var getKey = typeof key === "function"
|
|
335
|
+
? key
|
|
336
|
+
: function (item) { return item[key]; };
|
|
337
|
+
for (var index = 0; index < this.length; index++) {
|
|
338
|
+
var item = this[index];
|
|
339
|
+
var groupKey = getKey(item, index, this);
|
|
340
|
+
result[groupKey] = ((_a = result[groupKey]) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Returns items that are not present in the other arrays.
|
|
347
|
+
* @param arrays Arrays to exclude.
|
|
348
|
+
* @returns A new array of values not found in other arrays.
|
|
349
|
+
*/
|
|
350
|
+
function _difference () {
|
|
351
|
+
var arrays = [];
|
|
352
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
353
|
+
arrays[_i] = arguments[_i];
|
|
354
|
+
}
|
|
355
|
+
if (arrays.length === 0)
|
|
356
|
+
return this.slice();
|
|
357
|
+
var other = new Set();
|
|
358
|
+
for (var _a = 0, arrays_1 = arrays; _a < arrays_1.length; _a++) {
|
|
359
|
+
var arr = arrays_1[_a];
|
|
360
|
+
for (var _b = 0, arr_1 = arr; _b < arr_1.length; _b++) {
|
|
361
|
+
var item = arr_1[_b];
|
|
362
|
+
other.add(item);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return this.filter(function (item) { return !other.has(item); });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Returns items present in all arrays.
|
|
370
|
+
* @param arrays Arrays to intersect with.
|
|
371
|
+
* @returns A new array of items found in every array.
|
|
372
|
+
*/
|
|
373
|
+
function _intersection () {
|
|
374
|
+
var arrays = [];
|
|
375
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
376
|
+
arrays[_i] = arguments[_i];
|
|
377
|
+
}
|
|
378
|
+
if (arrays.length === 0)
|
|
379
|
+
return this.slice();
|
|
380
|
+
var sets = arrays.map(function (arr) { return new Set(arr); });
|
|
381
|
+
return this.filter(function (item) { return sets.every(function (set) { return set.has(item); }); });
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Returns a unique merge of the array and the other arrays.
|
|
386
|
+
* @param arrays Arrays to merge.
|
|
387
|
+
* @returns A new array with unique values.
|
|
388
|
+
*/
|
|
389
|
+
function _union () {
|
|
390
|
+
var arrays = [];
|
|
391
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
392
|
+
arrays[_i] = arguments[_i];
|
|
393
|
+
}
|
|
394
|
+
var out = [];
|
|
395
|
+
var seen = new Set();
|
|
396
|
+
var push = function (item) {
|
|
397
|
+
if (seen.has(item))
|
|
398
|
+
return;
|
|
399
|
+
seen.add(item);
|
|
400
|
+
out.push(item);
|
|
401
|
+
};
|
|
402
|
+
for (var _a = 0, _b = this; _a < _b.length; _a++) {
|
|
403
|
+
var item = _b[_a];
|
|
404
|
+
push(item);
|
|
405
|
+
}
|
|
406
|
+
for (var _c = 0, arrays_1 = arrays; _c < arrays_1.length; _c++) {
|
|
407
|
+
var arr = arrays_1[_c];
|
|
408
|
+
for (var _d = 0, arr_1 = arr; _d < arr_1.length; _d++) {
|
|
409
|
+
var item = arr_1[_d];
|
|
410
|
+
push(item);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Returns unique items by a key or mapper function.
|
|
418
|
+
* @param key The property name or mapper function to determine uniqueness.
|
|
419
|
+
* @returns A new array with unique items.
|
|
420
|
+
*/
|
|
421
|
+
function _uniqBy (key) {
|
|
422
|
+
var result = [];
|
|
423
|
+
var seen = new Set();
|
|
424
|
+
var getKey = typeof key === "function"
|
|
425
|
+
? key
|
|
426
|
+
: function (item) { return item[key]; };
|
|
427
|
+
for (var index = 0; index < this.length; index++) {
|
|
428
|
+
var item = this[index];
|
|
429
|
+
var keyValue = getKey(item, index, this);
|
|
430
|
+
if (!seen.has(keyValue)) {
|
|
431
|
+
seen.add(keyValue);
|
|
432
|
+
result.push(item);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return result;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Sorts items by a key or mapper function (stable).
|
|
440
|
+
* @param key The property name or mapper function to sort by.
|
|
441
|
+
* @returns A new array sorted by the key.
|
|
442
|
+
*/
|
|
443
|
+
function _sortBy (key) {
|
|
444
|
+
var _this = this;
|
|
445
|
+
var getKey = typeof key === "function"
|
|
446
|
+
? key
|
|
447
|
+
: function (item) { return item[key]; };
|
|
448
|
+
return this
|
|
449
|
+
.map(function (item, index) { return ({ item: item, index: index, key: getKey(item, index, _this) }); })
|
|
450
|
+
.sort(function (a, b) {
|
|
451
|
+
if (a.key < b.key)
|
|
452
|
+
return -1;
|
|
453
|
+
if (a.key > b.key)
|
|
454
|
+
return 1;
|
|
455
|
+
return a.index - b.index;
|
|
456
|
+
})
|
|
457
|
+
.map(function (entry) { return entry.item; });
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Returns a random sample of items without replacement.
|
|
462
|
+
* @param count Number of items to sample.
|
|
463
|
+
* @returns A new array containing sampled items.
|
|
464
|
+
*/
|
|
465
|
+
function _sample (count) {
|
|
466
|
+
var _a;
|
|
467
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
468
|
+
throw new Error("count must be a positive integer");
|
|
469
|
+
}
|
|
470
|
+
var out = this.slice();
|
|
471
|
+
for (var i = out.length - 1; i > 0; i--) {
|
|
472
|
+
var j = Math.floor(Math.random() * (i + 1));
|
|
473
|
+
_a = [out[j], out[i]], out[i] = _a[0], out[j] = _a[1];
|
|
474
|
+
}
|
|
475
|
+
return out.slice(0, Math.min(count, out.length));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Pads the array to the given length with the provided value.
|
|
480
|
+
* @param length Target length.
|
|
481
|
+
* @param value Value to pad with.
|
|
482
|
+
* @returns A new padded array.
|
|
483
|
+
*/
|
|
484
|
+
function _pad (length, value) {
|
|
485
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
486
|
+
throw new Error("length must be a non-negative integer");
|
|
487
|
+
}
|
|
488
|
+
var out = this.slice();
|
|
489
|
+
while (out.length < length)
|
|
490
|
+
out.push(value);
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
var call = function (fn, thisArg) {
|
|
495
|
+
var args = [];
|
|
496
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
497
|
+
args[_i - 2] = arguments[_i];
|
|
498
|
+
}
|
|
499
|
+
return fn.apply(thisArg, args);
|
|
500
|
+
};
|
|
501
|
+
var first = function (arr) { return call(_first, arr); };
|
|
502
|
+
var last = function (arr) { return call(_last, arr); };
|
|
503
|
+
var unique = function (arr) { return call(_unique, arr); };
|
|
504
|
+
var chunk = function (arr, size) { return call(_chunk, arr, size); };
|
|
505
|
+
var compact = function (arr) { return call(_compact, arr); };
|
|
506
|
+
var compactMap = function (arr, mapper) { return call(_compactMap, arr, mapper); };
|
|
507
|
+
var random = function (arr) { return call(_random, arr); };
|
|
508
|
+
var keyValueMap = function (arr, key, value) { return call(_keyValueMap, arr, key, value); };
|
|
509
|
+
var sum = function (arr) { return call(_sum, arr); };
|
|
510
|
+
var avg = function (arr) { return call(_avg, arr); };
|
|
511
|
+
var max = function (arr) { return call(_max, arr); };
|
|
512
|
+
var min = function (arr) { return call(_min, arr); };
|
|
513
|
+
var shuffle = function (arr) { return call(shuffle$1, arr); };
|
|
514
|
+
var groupBy = function (arr, key) { return call(_groupBy, arr, key); };
|
|
515
|
+
var flatten = function (arr) { return call(_flatten, arr); };
|
|
516
|
+
var zip = function (arr) {
|
|
517
|
+
var arrays = [];
|
|
518
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
519
|
+
arrays[_i - 1] = arguments[_i];
|
|
520
|
+
}
|
|
521
|
+
return call.apply(void 0, __spreadArray([_zip, arr], arrays, false));
|
|
522
|
+
};
|
|
523
|
+
var partition = function (arr, predicate) { return call(_partition, arr, predicate); };
|
|
524
|
+
var pluck = function (arr, key) { return call(_pluck, arr, key); };
|
|
525
|
+
var countBy = function (arr, key) { return call(_countBy, arr, key); };
|
|
526
|
+
var difference = function (arr) {
|
|
527
|
+
var arrays = [];
|
|
528
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
529
|
+
arrays[_i - 1] = arguments[_i];
|
|
530
|
+
}
|
|
531
|
+
return call.apply(void 0, __spreadArray([_difference, arr], arrays, false));
|
|
532
|
+
};
|
|
533
|
+
var intersection = function (arr) {
|
|
534
|
+
var arrays = [];
|
|
535
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
536
|
+
arrays[_i - 1] = arguments[_i];
|
|
537
|
+
}
|
|
538
|
+
return call.apply(void 0, __spreadArray([_intersection, arr], arrays, false));
|
|
539
|
+
};
|
|
540
|
+
var union = function (arr) {
|
|
541
|
+
var arrays = [];
|
|
542
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
543
|
+
arrays[_i - 1] = arguments[_i];
|
|
544
|
+
}
|
|
545
|
+
return call.apply(void 0, __spreadArray([_union, arr], arrays, false));
|
|
546
|
+
};
|
|
547
|
+
var uniqBy = function (arr, key) { return call(_uniqBy, arr, key); };
|
|
548
|
+
var sortBy = function (arr, key) { return call(_sortBy, arr, key); };
|
|
549
|
+
var sample = function (arr, count) { return call(_sample, arr, count); };
|
|
550
|
+
var pad = function (arr, length, value) { return call(_pad, arr, length, value); };
|
|
551
|
+
|
|
552
|
+
exports.avg = avg;
|
|
553
|
+
exports.chunk = chunk;
|
|
554
|
+
exports.compact = compact;
|
|
555
|
+
exports.compactMap = compactMap;
|
|
556
|
+
exports.countBy = countBy;
|
|
557
|
+
exports.difference = difference;
|
|
558
|
+
exports.first = first;
|
|
559
|
+
exports.flatten = flatten;
|
|
560
|
+
exports.groupBy = groupBy;
|
|
561
|
+
exports.intersection = intersection;
|
|
562
|
+
exports.keyValueMap = keyValueMap;
|
|
563
|
+
exports.last = last;
|
|
564
|
+
exports.max = max;
|
|
565
|
+
exports.min = min;
|
|
566
|
+
exports.pad = pad;
|
|
567
|
+
exports.partition = partition;
|
|
568
|
+
exports.pluck = pluck;
|
|
569
|
+
exports.random = random;
|
|
570
|
+
exports.sample = sample;
|
|
571
|
+
exports.shuffle = shuffle;
|
|
572
|
+
exports.sortBy = sortBy;
|
|
573
|
+
exports.sum = sum;
|
|
574
|
+
exports.union = union;
|
|
575
|
+
exports.uniqBy = uniqBy;
|
|
576
|
+
exports.unique = unique;
|
|
577
|
+
exports.zip = zip;
|
|
578
|
+
|
|
579
|
+
}));
|
|
580
|
+
//# sourceMappingURL=index.umd.js.map
|