Skip to content

Commit 1df2e0c

Browse files
authored
Merge pull request #91 from Sykander/typescript-def
Typescript def
2 parents c326e50 + 246cae1 commit 1df2e0c

31 files changed

+383
-401
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.4",
44
"description": "Async Array Class and methods. Use for asynchronous array looping methods.",
55
"main": "src/index.js",
6-
"typings": "src/index.d.ts",
6+
"types": "types/index.d.ts",
77
"scripts": {
88
"test:type-definitions": "./node_modules/.bin/tsc test/typings.ts && node test/typings.js",
99
"test:code-style": "npm run lint:check",

src/async-array.js

Lines changed: 82 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,88 @@
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');
1210

1311
/**
1412
* Async Array
1513
* ===========
1614
* Array like object with access to async array methods
17-
* @type {AsyncArray}
15+
* @class
16+
* @extends Array
1817
*/
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 };

src/async-filter.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,25 @@ const { mapIterable, filterIterable } = require('./helpers'),
66
* ============
77
* Filter asynchronously and resolve when all callbacks are resolved
88
* @async
9+
* @param {Object} iterable
910
* @param {Function} callback - callback(currentValue, index, array)
1011
* @param {Object} [thisArg=undefined]
1112
* @return {Array}
1213
* @throws {TypeError}
1314
*/
14-
const asyncFilter = (module.exports.asyncFilter = async function asyncFilter(
15-
callback,
16-
thisArg = undefined
17-
) {
15+
async function asyncFilter(iterable, callback, thisArg = undefined) {
16+
validateIsIterable(iterable);
1817
validateIsFunction(callback);
1918

2019
return filterIterable(
21-
this,
20+
iterable,
2221
await Promise.all(
23-
mapIterable(this, callback.bind(thisArg), {
22+
mapIterable(iterable, callback.bind(thisArg), {
2423
useEmptyElements: false,
2524
newlyAddedElements: false
2625
})
2726
)
2827
);
29-
});
30-
31-
/**
32-
* Async Filter Iterable
33-
* =====================
34-
* Filter an iterable object asynchronously and resolve when all callbacks are resolved
35-
* @async
36-
* @param {Object} iterable
37-
* @param {Function} callback - callback(currentValue, index, array)
38-
* @param {Object} [thisArg=undefined]
39-
* @return {Array}
40-
* @throws {TypeError}
41-
*/
42-
module.exports.asyncFilterIterable = async function asyncFilterIterable(
43-
iterable,
44-
callback,
45-
thisArg = undefined
46-
) {
47-
validateIsIterable(iterable);
28+
}
4829

49-
return asyncFilter.call(iterable, callback, thisArg);
50-
};
30+
module.exports = { asyncFilter };

src/async-find-index.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ const { mapIterable } = require('./helpers'),
66
* ================
77
* Find an item's index asynchronously and resolve when found or all callbacks resolve
88
* @async
9+
* @param {Object} iterable
910
* @param {Function} callback - callback(currentValue, index, array)
1011
* @param {Object} [thisArg=undefined]
1112
* @return {Number} - an integer index, -1 if not found
1213
* @throws {TypeError}
1314
*/
14-
const asyncFindIndex = (module.exports.asyncFindIndex = async function asyncFindIndex(
15-
callback,
16-
thisArg = undefined
17-
) {
15+
async function asyncFindIndex(iterable, callback, thisArg = undefined) {
16+
validateIsIterable(iterable);
1817
validateIsFunction(callback);
1918

20-
const tasks = mapIterable(this, callback.bind(thisArg), {
19+
const tasks = mapIterable(iterable, callback.bind(thisArg), {
2120
useEmptyElements: true,
2221
newlyAddedElements: false
2322
});
@@ -34,25 +33,6 @@ const asyncFindIndex = (module.exports.asyncFindIndex = async function asyncFind
3433
taskResults.findIndex(result => result)
3534
)
3635
]);
37-
});
38-
39-
/**
40-
* Async Find Index On Iterable
41-
* ============================
42-
* Find an item's index in an iterable object asynchronously and resolve when found or all callbacks resolve
43-
* @async
44-
* @param {Object} iterable
45-
* @param {Function} callback - callback(currentValue, index, array)
46-
* @param {Object} [thisArg=undefined]
47-
* @return {Number} - an integer index, -1 if not found
48-
* @throws {TypeError}
49-
*/
50-
module.exports.asyncFindIndexOnIterable = async function asyncFindIndexOnIterable(
51-
iterable,
52-
callback,
53-
thisArg = undefined
54-
) {
55-
validateIsIterable(iterable);
36+
}
5637

57-
return asyncFindIndex.call(iterable, callback, thisArg);
58-
};
38+
module.exports = { asyncFindIndex };

src/async-find.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ const { mapIterable } = require('./helpers'),
66
* ==========
77
* Find an item asynchronously and resolve when found or all callbacks resolve
88
* @async
9+
* @param {Object} iterable
910
* @param {Function} callback - callback(currentValue, index, array)
1011
* @param {Object} [thisArg=undefined]
1112
* @return {any}
1213
* @throws {TypeError}
1314
*/
14-
const asyncFind = (module.exports.asyncFind = async function asyncFind(
15-
callback,
16-
thisArg = undefined
17-
) {
15+
async function asyncFind(iterable, callback, thisArg = undefined) {
16+
validateIsIterable(iterable);
1817
validateIsFunction(callback);
1918

20-
const tasks = mapIterable(this, callback.bind(thisArg), {
19+
const tasks = mapIterable(iterable, callback.bind(thisArg), {
2120
useEmptyElements: true,
2221
newlyAddedElements: false
2322
});
2423

25-
return this[
24+
return iterable[
2625
await Promise.race([
2726
Promise.race(
2827
tasks.map(async (task, index) => {
@@ -38,25 +37,6 @@ const asyncFind = (module.exports.asyncFind = async function asyncFind(
3837
)
3938
])
4039
];
41-
});
42-
43-
/**
44-
* Async Find In Iterable
45-
* ======================
46-
* Find an item in an iterable object asynchronously and resolve when found or all callbacks resolve
47-
* @async
48-
* @param {Object} iterable
49-
* @param {Function} callback - callback(currentValue, index, array)
50-
* @param {Object} [thisArg=undefined]
51-
* @return {any}
52-
* @throws {TypeError}
53-
*/
54-
module.exports.asyncFindInIterable = async function asyncFindInIterable(
55-
iterable,
56-
callback,
57-
thisArg = undefined
58-
) {
59-
validateIsIterable(iterable);
40+
}
6041

61-
return asyncFind.call(iterable, callback, thisArg);
62-
};
42+
module.exports = { asyncFind };

src/async-for-each.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,21 @@ const { mapIterable } = require('./helpers'),
66
* ==============
77
* Loop asynchronously and resolve when all callbacks are resolved
88
* @async
9+
* @param {Object} iterable
910
* @param {Function} callback - callback(currentValue, index, array)
1011
* @param {Object} [thisArg=undefined]
1112
* @throws {TypeError}
1213
*/
13-
const asyncForEach = (module.exports.asyncForEach = async function asyncForEach(
14-
callback,
15-
thisArg = undefined
16-
) {
14+
async function asyncForEach(iterable, callback, thisArg = undefined) {
15+
validateIsIterable(iterable);
1716
validateIsFunction(callback);
1817

1918
await Promise.all(
20-
mapIterable(this, callback.bind(thisArg), {
19+
mapIterable(iterable, callback.bind(thisArg), {
2120
useEmptyElements: false,
2221
newlyAddedElements: false
2322
})
2423
);
25-
});
26-
27-
/**
28-
* Async For Each Of Iterable
29-
* ============================
30-
* Loop over an iterable object asynchronously and resolve when all callbacks are resolved
31-
* @async
32-
* @param {Object} iterable
33-
* @param {Function} callback - callback(currentValue, index, array)
34-
* @param {Object} [thisArg=undefined]
35-
* @throws {TypeError}
36-
*/
37-
module.exports.asyncForEachOfIterable = async function asyncForEachOfIterable(
38-
iterable,
39-
callback,
40-
thisArg = undefined
41-
) {
42-
validateIsIterable(iterable);
24+
}
4325

44-
return asyncForEach.call(iterable, callback, thisArg);
45-
};
26+
module.exports = { asyncForEach };

0 commit comments

Comments
 (0)