Skip to content

Commit 2cd59be

Browse files
committed
Added TS checks.
1 parent d8ddb78 commit 2cd59be

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

ts-check/defs.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import {
2+
none,
23
isFinalValue,
34
finalValue,
45
getFinalValue,
5-
66
isMany,
77
many,
88
getManyValues,
9-
109
isFlushable,
1110
flushable,
12-
1311
isFunctionList,
1412
getFunctionList,
1513
setFunctionList,
16-
clearFunctionList
14+
clearFunctionList,
15+
toMany,
16+
normalizeMany,
17+
combineMany,
18+
combineManyMut
1719
} from 'stream-chain/defs.js';
1820

1921
{
@@ -94,3 +96,77 @@ import {
9496

9597
void w;
9698
}
99+
100+
{
101+
const x1 = toMany(none),
102+
t1 = getManyValues(x1);
103+
console.assert(t1.length === 0);
104+
105+
const x2 = toMany(1),
106+
t2 = getManyValues(x2);
107+
console.assert(t2.length === 1);
108+
109+
const x3 = toMany(many([1, 2, 3])),
110+
t3 = getManyValues(x3);
111+
console.assert(t3.length === 3);
112+
}
113+
114+
{
115+
const x1 = normalizeMany(many([]));
116+
console.assert(x1 === none);
117+
118+
const x2 = normalizeMany(many([1]));
119+
console.assert(x2 === 1);
120+
121+
const x3 = normalizeMany(many([1, 2, 3]));
122+
if (isMany(x3)) {
123+
console.assert(getManyValues(x3).length === 3);
124+
}
125+
console.assert(isMany(x3));
126+
}
127+
128+
{
129+
const x1 = combineMany(none, none);
130+
console.assert(getManyValues(x1).length === 0);
131+
132+
const x2 = combineMany(none, many([1, 2, 3]));
133+
console.assert(getManyValues(x2).length === 3);
134+
135+
const x3 = combineMany(many([1, 2, 3]), none);
136+
console.assert(getManyValues(x3).length === 3);
137+
138+
const x4 = combineMany(many([1, 2, 3]), many([4, 5, 6]));
139+
console.assert(getManyValues(x4).length === 6);
140+
141+
const x5 = combineMany(0, many([1, 2, 3]));
142+
console.assert(getManyValues(x5).length === 4);
143+
144+
const x6 = combineMany(many([1, 2, 3]), 4);
145+
console.assert(getManyValues(x6).length === 4);
146+
147+
const x7 = combineMany(1, 2);
148+
console.assert(getManyValues(x7).length === 2);
149+
}
150+
151+
{
152+
const x1 = combineManyMut(none, none);
153+
console.assert(getManyValues(x1).length === 0);
154+
155+
const x2 = combineManyMut(none, many([1, 2, 3]));
156+
console.assert(getManyValues(x2).length === 3);
157+
158+
const x3 = combineManyMut(many([1, 2, 3]), none);
159+
console.assert(getManyValues(x3).length === 3);
160+
161+
const x4 = combineManyMut(many([1, 2, 3]), many([4, 5, 6]));
162+
console.assert(getManyValues(x4).length === 6);
163+
164+
const x5 = combineManyMut(0, many([1, 2, 3]));
165+
console.assert(getManyValues(x5).length === 4);
166+
167+
const x6 = combineManyMut(many([1, 2, 3]), 4);
168+
console.assert(getManyValues(x6).length === 4);
169+
170+
const x7 = combineManyMut(1, 2);
171+
console.assert(getManyValues(x7).length === 2);
172+
}

ts-check/demo.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chain, {asStream, many, none} from 'stream-chain';
1+
import chain, {chainUnchecked, asStream, many, none} from 'stream-chain';
22
import {TypedTransform} from 'stream-chain/typed-streams.js';
33
import readableFrom from 'stream-chain/utils/readableFrom.js';
44

@@ -50,3 +50,45 @@ const c = chain([
5050
c.on('data', (data: boolean) => output.push(data));
5151

5252
readableFrom([1, 2, 3]).pipe(c);
53+
54+
// unchecked
55+
56+
const c2 = chainUnchecked<number, boolean>([
57+
// transforms a value
58+
(x: number) => x * x,
59+
// returns several values
60+
(x: number) => many([x - 1, x, x + 1]),
61+
// waits for an asynchronous operation
62+
async (x: number) => await getTotalFromDatabaseByKey(x),
63+
// or: (x: number) => getTotalFromDatabaseByKey(x),
64+
// returns multiple values with a generator
65+
function* (x: number) {
66+
for (let i = x; i >= 0; --i) {
67+
yield i;
68+
}
69+
},
70+
// filters out even values
71+
(x: number) => (x % 2 ? x : none),
72+
// uses an arbitrary transform stream
73+
new Transform({
74+
objectMode: true,
75+
transform(x, _, callback) {
76+
callback(null, x + 1);
77+
}
78+
}),
79+
// can skip falsy values
80+
[null, undefined],
81+
// uses a typed transform stream
82+
new TypedTransform<number, string>({
83+
objectMode: true,
84+
transform(x, _, callback) {
85+
callback(null, String(x + 1));
86+
}
87+
}),
88+
// uses a wrapped function
89+
asStream((x: string) => !x)
90+
]),
91+
output2: boolean[] = [];
92+
c2.on('data', (data: boolean) => output2.push(data));
93+
94+
readableFrom([1, 2, 3]).pipe(c2);

0 commit comments

Comments
 (0)