-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: pull array chunking into utils (#409)
- Loading branch information
1 parent
553a817
commit 45cdcc4
Showing
4 changed files
with
97 additions
and
47 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
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,41 @@ | ||
/** | ||
* Chunk an array based on the value returned by a mapping function. | ||
* | ||
* Returns an iterable, that yields pairs with first value being the | ||
* return of the mapper function, and the second value being the chunk | ||
* | ||
* @param arr array to be chunked | ||
* @param mapper mapping function designating the chunk "key" | ||
* @returns an iterable with pairs of chunk "keys" and chunks themselves. | ||
*/ | ||
export function chunkBy<T, K>( | ||
arr: T[], | ||
mapper: (elem: T, idx: number, arr: T[]) => K | ||
): Iterable<[K, T[]]> { | ||
return { | ||
*[Symbol.iterator]() { | ||
if (arr.length === 0) return | ||
|
||
let currentChunkValue: K = mapper(arr[0], 0, arr) | ||
let newChunkValue: K | ||
let currentChunk: T[] = [arr[0]] | ||
|
||
for (let idx = 1; idx < arr.length; ++idx) { | ||
newChunkValue = mapper(arr[idx], idx, arr) | ||
if (currentChunkValue === newChunkValue) { | ||
// Still the same chunk, expand it | ||
currentChunk.push(arr[idx]) | ||
currentChunkValue = newChunkValue | ||
} else { | ||
// Chunk boundary crossed, yield the current chunk and start the new one | ||
yield [currentChunkValue, currentChunk] | ||
currentChunkValue = newChunkValue | ||
currentChunk = [arr[idx]] | ||
} | ||
} | ||
|
||
// Yield the last chunk we've been building up in the loop | ||
yield [currentChunkValue, currentChunk] | ||
}, | ||
} | ||
} |
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,45 @@ | ||
import test from 'ava' | ||
|
||
import { chunkBy } from '../../src/util/arrays' | ||
|
||
test('chunkBy: correctly chunks an array based on a predicate', (t) => { | ||
const source = ['a', 'b', '', 'aa', 'bb', 'a'] | ||
|
||
const result = [...chunkBy(source, (x) => x.length)] | ||
|
||
t.deepEqual(result, [ | ||
[1, ['a', 'b']], | ||
[0, ['']], | ||
[2, ['aa', 'bb']], | ||
[1, ['a']], | ||
]) | ||
}) | ||
|
||
test('chunkBy: correctly chunks an array based on a false-ish predicate', (t) => { | ||
const source = ['a', 'b', '', 'bb', 'aa', 'a', 'b'] | ||
|
||
const result = [...chunkBy(source, (x) => x.includes('a'))] | ||
|
||
t.deepEqual(result, [ | ||
[true, ['a']], | ||
[false, ['b', '', 'bb']], | ||
[true, ['aa', 'a']], | ||
[false, ['b']], | ||
]) | ||
}) | ||
|
||
test('chunkBy: returns an empty iterator on empty source', (t) => { | ||
const source: string[] = [] | ||
|
||
const result = [...chunkBy(source, (x) => x.includes('a'))] | ||
|
||
t.deepEqual(result, []) | ||
}) | ||
|
||
test('chunkBy: works on a single element', (t) => { | ||
const source: string[] = ['a'] | ||
|
||
const result = [...chunkBy(source, () => undefined)] | ||
|
||
t.deepEqual(result, [[undefined, ['a']]]) | ||
}) |