-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test-d.ts
More file actions
69 lines (54 loc) · 2.43 KB
/
index.test-d.ts
File metadata and controls
69 lines (54 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {Readable} from 'node:stream';
import {expectType, expectAssignable} from 'tsd';
import {chunk, chunkFrom, chunkFromAsync} from './index.js';
// Chunk tests
const uint8Array = new Uint8Array(1000);
const uint16Array = new Uint16Array(500);
const int32Array = new Int32Array(250);
// Returns Generator<Uint8Array>
expectType<Generator<Uint8Array, void, undefined>>(chunk(uint8Array, 65_536));
expectType<Generator<Uint8Array, void, undefined>>(chunk(uint8Array, 100));
expectType<Generator<Uint8Array, void, undefined>>(chunk(uint16Array, 1000));
expectType<Generator<Uint8Array, void, undefined>>(chunk(int32Array, 1000));
// Can be used in for...of
for (const piece of chunk(uint8Array, 65_536)) {
expectType<Uint8Array>(piece);
}
// Can spread to array
expectAssignable<Uint8Array[]>([...chunk(uint8Array, 65_536)]);
// ChunkFrom tests
const bufferArray = [new Uint8Array(100), new Uint8Array(200)];
expectType<Generator<Uint8Array, void, undefined>>(chunkFrom(bufferArray, 100));
function * bufferGenerator(): Generator<Uint8Array> {
yield new Uint8Array(100);
}
expectType<Generator<Uint8Array, void, undefined>>(chunkFrom(bufferGenerator(), 100));
// Can be used in for...of with iterable
for (const piece of chunkFrom(bufferArray, 100)) {
expectType<Uint8Array>(piece);
}
// ChunkFromAsync tests
const readableStream = Readable.from(uint8Array) as AsyncIterable<Uint8Array>;
async function * asyncGenerator(): AsyncGenerator<Uint8Array> {
yield new Uint8Array(100);
}
function * syncGenerator(): Generator<Uint8Array> {
yield new Uint8Array(100);
}
// Returns AsyncGenerator<Uint8Array>
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(readableStream, 65_536));
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(readableStream, 100));
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(asyncGenerator(), 65_536));
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(syncGenerator(), 65_536));
// Can be used in for await...of
const testAsyncIteration = async () => {
for await (const piece of chunkFromAsync(readableStream, 65_536)) {
expectType<Uint8Array>(piece);
}
};
// Generic async iterable
const asyncIter = asyncGenerator();
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(asyncIter, 65_536));
// Generic iterable
const syncIter = syncGenerator();
expectType<AsyncGenerator<Uint8Array, void, undefined>>(chunkFromAsync(syncIter, 65_536));