-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
54 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
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,119 @@ | ||
import { chunked, groupByKey, withIndex, withNext } from "../iterator"; | ||
import { Readable } from "stream"; | ||
|
||
const arrayToAsyncIter = <T = unknown>(arr: T[]): AsyncIterableIterator<T> => | ||
Readable.from(arr)[Symbol.asyncIterator](); | ||
|
||
describe("chunked", () => { | ||
it("can separate data source by chunk", async () => { | ||
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
const expected = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]; | ||
const actual = []; | ||
for await (const chunk of chunked(arrayToAsyncIter(source), 3)) { | ||
actual.push(chunk); | ||
} | ||
expect(actual).toStrictEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("groupByKey", () => { | ||
it("can separate date source by group", async () => { | ||
const source = [ | ||
{ name: "banana", type: "fruit" }, | ||
{ name: "orange", type: "fruit" }, | ||
{ name: "tomato", type: "vegetables" }, | ||
{ name: "carrot", type: "vegetables" }, | ||
{ name: "asparagus", type: "vegetables" }, | ||
{ name: "beef", type: "meat" }, | ||
{ name: "cherries", type: "fruit" }, | ||
{ name: "pork", type: "meat" }, | ||
{ name: "chicken", type: "meat" }, | ||
]; | ||
const expected = [ | ||
{ | ||
key: "fruit", | ||
data: [ | ||
{ name: "banana", type: "fruit" }, | ||
{ name: "orange", type: "fruit" }, | ||
], | ||
}, | ||
{ | ||
key: "vegetables", | ||
data: [ | ||
{ name: "tomato", type: "vegetables" }, | ||
{ name: "carrot", type: "vegetables" }, | ||
{ name: "asparagus", type: "vegetables" }, | ||
], | ||
}, | ||
{ | ||
key: "meat", | ||
data: [{ name: "beef", type: "meat" }], | ||
}, | ||
{ | ||
key: "fruit", | ||
data: [{ name: "cherries", type: "fruit" }], | ||
}, | ||
{ | ||
key: "meat", | ||
data: [ | ||
{ name: "pork", type: "meat" }, | ||
{ name: "chicken", type: "meat" }, | ||
], | ||
}, | ||
]; | ||
const actual = []; | ||
for await (const chunk of groupByKey( | ||
arrayToAsyncIter(source), | ||
(el) => el.type | ||
)) { | ||
actual.push(chunk); | ||
} | ||
expect(actual).toStrictEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("withNextIterator", () => { | ||
it("can iterate from data source with next value", async () => { | ||
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
const expected = [ | ||
{ current: 1, next: 2 }, | ||
{ current: 2, next: 3 }, | ||
{ current: 3, next: 4 }, | ||
{ current: 4, next: 5 }, | ||
{ current: 5, next: 6 }, | ||
{ current: 6, next: 7 }, | ||
{ current: 7, next: 8 }, | ||
{ current: 8, next: 9 }, | ||
{ current: 9, next: 10 }, | ||
{ current: 10, next: undefined }, | ||
]; | ||
const actual = []; | ||
for await (const data of withNext(arrayToAsyncIter(source))) { | ||
actual.push(data); | ||
} | ||
expect(actual).toStrictEqual(expected); | ||
}); | ||
}); | ||
|
||
describe("withIndexIterator", () => { | ||
it("can iterate from data source with index", async () => { | ||
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
const expected = [ | ||
{ data: 1, index: 0 }, | ||
{ data: 2, index: 1 }, | ||
{ data: 3, index: 2 }, | ||
{ data: 4, index: 3 }, | ||
{ data: 5, index: 4 }, | ||
{ data: 6, index: 5 }, | ||
{ data: 7, index: 6 }, | ||
{ data: 8, index: 7 }, | ||
{ data: 9, index: 8 }, | ||
{ data: 10, index: 9 }, | ||
]; | ||
const actual = []; | ||
for await (const data of withIndex(arrayToAsyncIter(source))) { | ||
actual.push(data); | ||
} | ||
expect(actual).toStrictEqual(expected); | ||
}); | ||
}); |
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,59 @@ | ||
// eslint-disable-next-line func-style | ||
export async function* chunked<T = unknown>( | ||
source: AsyncIterable<T> | AsyncIterableIterator<T> | AsyncGenerator<T>, | ||
size: number | ||
): AsyncGenerator<T[], void, undefined> { | ||
let chunk = []; | ||
for await (const element of source) { | ||
chunk.push(element); | ||
if (chunk.length >= size) { | ||
yield chunk; | ||
chunk = []; | ||
} | ||
} | ||
if (chunk.length > 0) { | ||
yield chunk; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line func-style | ||
export async function* groupByKey<T = unknown, K = unknown>( | ||
source: AsyncIterableIterator<T> | AsyncGenerator<T>, | ||
keyFn: (element: T) => K | ||
): AsyncGenerator<{ key: K; data: T[] }, void, undefined> { | ||
let array = []; | ||
for await (const { current, next } of withNext(source)) { | ||
array.push(current); | ||
const currentKey = keyFn(current); | ||
if (next === undefined || currentKey !== keyFn(next)) { | ||
yield { key: currentKey, data: array }; | ||
array = []; | ||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line func-style | ||
export async function* withNext<T = unknown>( | ||
source: AsyncIterableIterator<T> | AsyncGenerator<T> | ||
): AsyncGenerator<{ current: T; next?: T }> { | ||
let { value: prev, done } = await source.next(); | ||
if (done) { | ||
return; | ||
} | ||
for await (const value of source) { | ||
yield { current: prev, next: value }; | ||
prev = value; | ||
} | ||
yield { current: prev, next: undefined }; | ||
} | ||
|
||
// eslint-disable-next-line func-style | ||
export async function* withIndex<T = unknown>( | ||
source: AsyncIterable<T> | AsyncIterableIterator<T> | AsyncGenerator<T> | ||
): AsyncGenerator<{ data: T; index: number }> { | ||
let index = 0; | ||
for await (const value of source) { | ||
yield { data: value, index }; | ||
index++; | ||
} | ||
} |