-
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.
- Loading branch information
Showing
5 changed files
with
94 additions
and
36 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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
export const fromArray = <T>(sourceArray: T[], strategy?: QueuingStrategy): ReadableStream<T> => { | ||
export const fromArray = <T>( | ||
sourceArray: T[], | ||
strategy?: QueuingStrategy, | ||
): ReadableStream<T> => { | ||
const arrayLength = sourceArray.length; | ||
return new ReadableStream<T>({ | ||
start(controller) { | ||
for (let i = 0; i < arrayLength; i++) { | ||
controller.enqueue(sourceArray[i]); | ||
} | ||
controller.close(); | ||
return new ReadableStream<T>( | ||
{ | ||
start(controller) { | ||
for (let i = 0; i < arrayLength; i++) { | ||
controller.enqueue(sourceArray[i]); | ||
} | ||
controller.close(); | ||
}, | ||
}, | ||
}, strategy); | ||
strategy, | ||
); | ||
}; |
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,10 @@ | ||
type CallbackFn<T, U> = (value: T, index: number) => U; | ||
|
||
export const map = <T, U>(callbackfn: CallbackFn<T, U>) => { | ||
let index = 0; | ||
return new TransformStream<T, U>({ | ||
transform(chunk, controller) { | ||
controller.enqueue(callbackfn(chunk, index++)); | ||
}, | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export const toArray = <T>(destinationArray: T[]): WritableStream<T> => { | ||
return new WritableStream<T>({ | ||
write(chunk) { | ||
destinationArray.push(chunk); | ||
}, | ||
}) | ||
return new WritableStream<T>({ | ||
write(chunk) { | ||
destinationArray.push(chunk); | ||
}, | ||
}); | ||
}; |
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,42 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { fromArray } from "../src/fromArray"; | ||
import { map } from "../src/map"; | ||
import { toArray } from "../src/toArray"; | ||
|
||
describe("map", () => { | ||
it("should map a stream", async () => { | ||
const sourceArray = [1, 2, 3]; | ||
const destinationArray = []; | ||
|
||
const mapStream = map<number, number>((value) => value * 2); | ||
|
||
await fromArray(sourceArray) | ||
.pipeThrough(mapStream) | ||
.pipeTo(toArray(destinationArray)); | ||
|
||
expect(destinationArray).toEqual([2, 4, 6]); | ||
}); | ||
|
||
it("should not break for an empty array", async () => { | ||
const sourceArray = []; | ||
const destinationArray = []; | ||
|
||
const mapStream = map<number, number>((value) => value); | ||
await fromArray(sourceArray) | ||
.pipeThrough(mapStream) | ||
.pipeTo(toArray(destinationArray)); | ||
|
||
expect(destinationArray).toEqual([]); | ||
}); | ||
|
||
it("should throw if parameter is undefined", async () => { | ||
// @ts-expect-error undefined parameter for test | ||
const mapStream = map<number, number>(undefined); | ||
|
||
expect( | ||
fromArray([1]) | ||
.pipeThrough(mapStream) | ||
.pipeTo(toArray([] as number[])), | ||
).rejects.toThrow("callbackfn is not a function"); | ||
}); | ||
}); |
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