Skip to content

Commit

Permalink
feat: implement from array
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Nov 26, 2023
1 parent 86c0bb1 commit 346b93d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"license": "ISC",
"devDependencies": {
"@biomejs/biome": "^1.3.3",
"@edge-runtime/vm": "^3.1.7",
"happy-dom": "^12.10.3",
"vitest": "^0.34.6"
}
}
72 changes: 70 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/fromArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const fromArray = <T>(array: T[]): ReadableStream<T> => {
const arrayLength = array.length
return new ReadableStream<T>({
start(controller) {
for (let i = 0; i < arrayLength; i++) {
controller.enqueue(array[i])
}
controller.close()
}
})
}
18 changes: 18 additions & 0 deletions test/fromArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest'
import { fromArray } from '../src/fromArray'

describe('fromArray', () => {
it('should create a readable stream from an array', async () => {
const array = [1, 2, 3]

const reader = fromArray(array).getReader()

for (let i = 0; i < array.length; i++) {
const result = await reader.read()
expect(result.value).toEqual(array[i])
expect(result.done).toEqual(false)
}

expect(await reader.read()).toEqual({ done: true, value: undefined })
})
})

0 comments on commit 346b93d

Please sign in to comment.