|
1 | | -const { asyncFind, asyncFindInIterable } = require('./async-find'), |
2 | | - { |
3 | | - asyncFindIndex, |
4 | | - asyncFindIndexOnIterable |
5 | | - } = require('./async-find-index'), |
6 | | - { asyncFilter, asyncFilterIterable } = require('./async-filter'), |
7 | | - { asyncForEach, asyncForEachOfIterable } = require('./async-for-each'), |
8 | | - { asyncMap, asyncMapOverIterable } = require('./async-map'), |
9 | | - { asyncMapSort, asyncMapSortIterable } = require('./async-map-sort'), |
10 | | - { asyncReduce, asyncReduceIterable } = require('./async-reduce'), |
11 | | - { asyncSort, asyncSortIterable } = require('./async-sort'); |
| 1 | +const { asyncFind } = require('./async-find'), |
| 2 | + { asyncFindIndex } = require('./async-find-index'), |
| 3 | + { asyncFilter } = require('./async-filter'), |
| 4 | + { asyncForEach } = require('./async-for-each'), |
| 5 | + { asyncMap } = require('./async-map'), |
| 6 | + { asyncMapSort } = require('./async-map-sort'), |
| 7 | + { asyncReduce } = require('./async-reduce'), |
| 8 | + { asyncSort } = require('./async-sort'), |
| 9 | + { compareByUnicode } = require('./helpers'); |
12 | 10 |
|
13 | 11 | /** |
14 | 12 | * Async Array |
15 | 13 | * =========== |
16 | 14 | * Array like object with access to async array methods |
17 | | - * @type {AsyncArray} |
| 15 | + * @class |
| 16 | + * @extends Array |
18 | 17 | */ |
19 | | -class AsyncArray extends Array {} |
20 | | - |
21 | | -// Add static methods |
22 | | -Object.assign(AsyncArray, { |
23 | | - asyncFind: asyncFindInIterable, |
24 | | - asyncFindIndex: asyncFindIndexOnIterable, |
25 | | - asyncFilter: asyncFilterIterable, |
26 | | - asyncForEach: asyncForEachOfIterable, |
27 | | - asyncMap: asyncMapOverIterable, |
28 | | - asyncMapSort: asyncMapSortIterable, |
29 | | - asyncReduce: asyncReduceIterable, |
30 | | - asyncSort: asyncSortIterable |
31 | | -}); |
32 | | - |
33 | | -// Create prototype methods |
34 | | -Object.assign(AsyncArray.prototype, { |
35 | | - asyncFind, |
36 | | - asyncFindIndex, |
37 | | - asyncFilter, |
38 | | - asyncForEach, |
39 | | - asyncMap, |
40 | | - asyncMapSort, |
41 | | - asyncReduce, |
42 | | - asyncSort |
43 | | -}); |
44 | | - |
45 | | -module.exports = AsyncArray; |
| 18 | +class AsyncArray extends Array { |
| 19 | + asyncFilter(callback, thisArg = undefined) { |
| 20 | + return asyncFilter(this, callback, thisArg); |
| 21 | + } |
| 22 | + |
| 23 | + static asyncFilter(iterable, callback, thisArg = undefined) { |
| 24 | + return asyncFilter(iterable, callback, thisArg); |
| 25 | + } |
| 26 | + |
| 27 | + asyncFind(callback, thisArg = undefined) { |
| 28 | + return asyncFind(this, callback, thisArg); |
| 29 | + } |
| 30 | + |
| 31 | + static asyncFind(iterable, callback, thisArg = undefined) { |
| 32 | + return asyncFind(iterable, callback, thisArg); |
| 33 | + } |
| 34 | + |
| 35 | + asyncFindIndex(callback, thisArg = undefined) { |
| 36 | + return asyncFindIndex(this, callback, thisArg); |
| 37 | + } |
| 38 | + |
| 39 | + static asyncFindIndex(iterable, callback, thisArg = undefined) { |
| 40 | + return asyncFindIndex(iterable, callback, thisArg); |
| 41 | + } |
| 42 | + |
| 43 | + asyncForEach(callback, thisArg = undefined) { |
| 44 | + return asyncForEach(this, callback, thisArg); |
| 45 | + } |
| 46 | + |
| 47 | + static asyncForEach(iterable, callback, thisArg = undefined) { |
| 48 | + return asyncForEach(iterable, callback, thisArg); |
| 49 | + } |
| 50 | + |
| 51 | + asyncMap(callback, thisArg = undefined) { |
| 52 | + return asyncMap(this, callback, thisArg); |
| 53 | + } |
| 54 | + |
| 55 | + static asyncMap(iterable, callback, thisArg = undefined) { |
| 56 | + return asyncMap(iterable, callback, thisArg); |
| 57 | + } |
| 58 | + |
| 59 | + asyncMapSort(mappingCallback, comparisonCallback = compareByUnicode) { |
| 60 | + return asyncMapSort(this, mappingCallback, comparisonCallback); |
| 61 | + } |
| 62 | + |
| 63 | + static asyncMapSort( |
| 64 | + iterable, |
| 65 | + mappingCallback, |
| 66 | + comparisonCallback = compareByUnicode |
| 67 | + ) { |
| 68 | + return asyncMapSort(iterable, mappingCallback, comparisonCallback); |
| 69 | + } |
| 70 | + |
| 71 | + asyncReduce(callback, accumulator = undefined) { |
| 72 | + return asyncReduce(this, callback, accumulator); |
| 73 | + } |
| 74 | + |
| 75 | + static asyncReduce(iterable, callback, accumulator = undefined) { |
| 76 | + return asyncReduce(iterable, callback, accumulator); |
| 77 | + } |
| 78 | + |
| 79 | + asyncSort(callback = compareByUnicode) { |
| 80 | + return asyncSort(this, callback); |
| 81 | + } |
| 82 | + |
| 83 | + static asyncSort(iterable, callback = compareByUnicode) { |
| 84 | + return asyncSort(iterable, callback); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = { AsyncArray }; |
0 commit comments