-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
9a8b86d
commit 34179be
Showing
5 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @file Type Tests - groupAsync | ||
* @module tutils/utils/tests/unit-d/groupAsync | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type testSubject from '../group-async' | ||
|
||
describe('unit-d:utils/groupAsync', () => { | ||
it('should return Promise<Record<K, T[number][]>>', () => { | ||
// Arrange | ||
type T = Vehicle[] | ||
type K = Vehicle['year'] | ||
type Expect = Promise<Record<K, T[number][]>> | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, K>>().returns.toEqualTypeOf<Expect>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @file Unit Tests - groupAsync | ||
* @module tutils/utils/tests/unit/groupAsync | ||
*/ | ||
|
||
import testSubject from '../group-async' | ||
|
||
describe('unit:utils/groupAsync', () => { | ||
it('should return groups object', async () => { | ||
expect(await testSubject([3.1, 4.2, 3.3], Math.floor)).to.eql({ | ||
3: [3.1, 3.3], | ||
4: [4.2] | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @file Utilities - groupAsync | ||
* @module tutils/utils/groupAsync | ||
*/ | ||
|
||
import type { Mapper, PropertyKey } from '#src/types' | ||
import cast from './cast' | ||
import isUndefined from './is-undefined' | ||
import reduceAsync from './reduce-async' | ||
|
||
/** | ||
* Group each item in an array. | ||
* | ||
* The return value is a plain object with a key for each group, where each key | ||
* value is an array containing group items. | ||
* | ||
* A `key` function is used to map array items to group keys. | ||
* | ||
* @see {@linkcode Mapper} | ||
* | ||
* @todo examples | ||
* | ||
* @async | ||
* | ||
* @template T - Array to group | ||
* @template K - Identity key type | ||
* | ||
* @param {T} arr - Array to group | ||
* @param {Mapper<T, K | Promise<K>>} key - Group key mapper | ||
* @return {Promise<Record<K, T[number][]>>} Groups object | ||
*/ | ||
const groupAsync = async < | ||
T extends readonly unknown[], | ||
K extends PropertyKey = PropertyKey | ||
>( | ||
arr: T, | ||
key: Mapper<T, K | Promise<K>> | ||
): Promise<{ [H in K]: T[number][] }> => { | ||
return reduceAsync(arr, async (acc, item, i) => { | ||
/** | ||
* Group key. | ||
* | ||
* @const {K} k | ||
*/ | ||
const k: K = await key(item, i, arr) | ||
|
||
// initialize group | ||
isUndefined(acc[k]) && (acc[k] = []) | ||
|
||
// add group item | ||
acc[k].push(item) | ||
|
||
return acc | ||
}, cast({})) | ||
} | ||
|
||
export default groupAsync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters