Skip to content

Commit bd38cea

Browse files
authored
Merge pull request #58 from Sykander/async-sort
Async sort
2 parents 8679d80 + 249f2c2 commit bd38cea

File tree

13 files changed

+188
-52
lines changed

13 files changed

+188
-52
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AsyncArray extends Array {...}
2727
* Async Find Index
2828
* Async For Each
2929
* Async Map
30+
* Async Sort
3031

3132
### [Async Filter](https://github.com/Sykander/iterable-async/wiki/Async-Filter)
3233

@@ -63,6 +64,13 @@ Map an iterable object asynchronously
6364
async function asyncMap(callback, [thisArg]) {...}
6465
```
6566

67+
### [Async Sort](https://github.com/Sykander/iterable-async/wiki/Async-Sort)
68+
69+
Sort an iterable object asynchronously
70+
```
71+
async function asyncSort(callback) {...}
72+
```
73+
6674

6775
## Development
6876

docs/playground.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ const {
44
asyncFindIndex,
55
asyncFilter,
66
asyncForEach,
7-
asyncMap
7+
asyncMap,
8+
asyncSort
89
} = require('iterable-async');

src/async-array.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ const asyncMap = require('./async-map'),
22
asyncForEach = require('./async-for-each'),
33
asyncFilter = require('./async-filter'),
44
asyncFind = require('./async-find'),
5-
asyncFindIndex = require('./async-find-index');
5+
asyncFindIndex = require('./async-find-index'),
6+
asyncSort = require('./async-sort');
67

78
/**
8-
* Create new constructor for Async Array
9+
* Async Array
10+
* ===========
11+
* Array like object with access to async array methods
912
* @type {AsyncArray}
1013
*/
1114
class AsyncArray extends Array {}
@@ -15,6 +18,7 @@ class AsyncArray extends Array {}
1518
(AsyncArray.prototype.asyncFindIndex = asyncFindIndex),
1619
(AsyncArray.prototype.asyncFilter = asyncFilter),
1720
(AsyncArray.prototype.asyncForEach = asyncForEach),
18-
(AsyncArray.prototype.asyncMap = asyncMap);
21+
(AsyncArray.prototype.asyncMap = asyncMap),
22+
(AsyncArray.prototype.asyncSort = asyncSort);
1923

2024
module.exports = AsyncArray;

src/async-filter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { mapIterable, filterIterable } = require('./helpers'),
44

55
/**
66
* Async Filter
7+
* ============
78
* Filter an iterable object asynchronously and resolve when all callbacks are resolved
89
* @async
910
* @param {Function} callback - callback(currentValue, index, array)

src/async-find-index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { mapIterable } = require('./helpers'),
44

55
/**
66
* Async Find Index
7+
* ================
78
* Find an item's index in an iterable object asynchronously and resolve when found or all callbacks resolve
89
* @async
910
* @param {Function} callback - callback(currentValue, index, array)

src/async-find.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { mapIterable } = require('./helpers'),
44

55
/**
66
* Async Find
7+
* ==========
78
* Find an item in an iterable object asynchronously and resolve when found or all callbacks resolve
89
* @async
910
* @param {Function} callback - callback(currentValue, index, array)

src/async-for-each.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { mapIterable } = require('./helpers'),
44

55
/**
66
* Async For Each
7+
* ==============
78
* Loop over an iterable object asynchronously and resolve when all callbacks are resolved
89
* @async
910
* @param {Function} callback - callback(currentValue, index, array)

src/async-map.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { mapIterable } = require('./helpers'),
44

55
/**
66
* Async Map
7+
* =========
78
* Map an iterable object asynchronously and resolve when all callbacks are resolved
89
* @async
910
* @param {Function} callback - callback(currentValue, index, array)

src/async-quick-sort.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { asyncPartition } = require('./helpers');
2+
3+
/**
4+
* Async Quick Sort
5+
* ================
6+
* Asynchronously sorts an iterable using quick-sort algorithm
7+
* @param {Object} iterable
8+
* @param {Number} leftIndex
9+
* @param {Number} rightIndex
10+
* @param {Function} compareFunc
11+
*/
12+
module.exports = async function asyncQuickSort(
13+
iterable,
14+
leftIndex,
15+
rightIndex,
16+
compareFunc
17+
) {
18+
let index;
19+
20+
if (iterable.length > 1) {
21+
index = await asyncPartition(
22+
iterable,
23+
leftIndex,
24+
rightIndex,
25+
compareFunc
26+
);
27+
28+
if (leftIndex < index - 1) {
29+
await asyncQuickSort(iterable, leftIndex, index - 1, compareFunc);
30+
}
31+
32+
if (index < rightIndex) {
33+
await asyncQuickSort(iterable, index, rightIndex, compareFunc);
34+
}
35+
}
36+
37+
return iterable;
38+
};

src/async-sort.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { noParam } = require('./constants'),
2+
{ validateIsFunction, validateIsIterable } = require('./validation'),
3+
asyncQuickSort = require('./async-quick-sort'),
4+
{ compareByUnicode } = require('./helpers');
5+
6+
/**
7+
* Async Sort
8+
* ==========
9+
* Asynchronously sorts and an iterable object and resolves when fully sorted
10+
* note that the object is sorted in place and no copy is made
11+
* @async
12+
* @param {Function} [compareFunc] - default is sort by item's unicode value
13+
* @return {Object}
14+
*/
15+
module.exports = async function asyncSort(compareFunc = noParam) {
16+
validateIsIterable(this);
17+
18+
const compare = compareFunc !== noParam ? compareFunc : compareByUnicode;
19+
20+
validateIsFunction(compare);
21+
22+
await asyncQuickSort(this, 0, this.length - 1, compare);
23+
24+
return this;
25+
};

0 commit comments

Comments
 (0)