-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (158 loc) · 4.24 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const STREAM = "@@stream";
export function from(source) {
const stream = (sink) => {
const stop = source(sink);
return () => stop?.();
};
stream[STREAM] = stream;
return stream;
}
export function isStream(value) {
return typeof value === "function" && value === value[STREAM];
}
export function of(...values) {
return from((push) => {
const iterator = values[Symbol.iterator]();
Promise.resolve().then(() => {
for (const value of iterator) push(value);
});
return () => {
iterator.return();
};
});
}
export function map(source, callback) {
return callback
? from((push) => source((value) => push(callback(value))))
: (anotherSource) => map(anotherSource, source);
}
export function filter(source, callback) {
return callback
? from((push) => source((value) => callback(value) && push(value)))
: (anotherSource) => filter(anotherSource, source);
}
export function forEach(source, callback) {
return callback
? source(callback)
: (anotherSource) => forEach(anotherSource, source);
}
export function scan(source, accumulator, reduce = accumulator) {
return isStream(source)
? from((push) => {
let created = true;
let intermediate = accumulator;
return source((value) => {
push(
(intermediate =
created && intermediate === reduce
? value
: reduce(intermediate, value))
);
if (created) created = false;
});
})
: (anotherSource) => scan(anotherSource, source, accumulator);
}
export function take(source, amount) {
let _amount = amount;
return isStream(source)
? takeWhile(source, () => _amount-- > 0 || ((_amount = amount), false))
: (anotherSource) => take(anotherSource, source);
}
export function takeWhile(source, callback) {
return callback
? from((push) => {
let stop;
let cleared;
const clear = () => {
stop?.();
stop = null;
cleared = true;
};
stop = source(
(value) => cleared || (callback(value) ? push(value) : clear())
);
return clear;
})
: (anotherSource) => takeWhile(anotherSource, source);
}
export function skip(source, amount) {
return isStream(source)
? from((push) => {
let _amount = amount;
return source((value) => (_amount > 0 ? _amount-- : push(value)));
})
: (anotherSource) => skip(anotherSource, source);
}
export function skipWhile(source, callback) {
return callback
? from((push) => {
let skipping = true;
return source((value) =>
skipping ? (skipping = callback(value)) || push(value) : push(value)
);
})
: (anotherSource) => skipWhile(anotherSource, source);
}
export function unique(source, selector, flushes) {
return isStream(source)
? from((push) => {
const keys = new Set();
const stopFiltering = source((value) => {
const key = selector?.(value) ?? value;
if (!keys.has(key)) {
keys.add(key);
push(value);
}
});
const stopFlushing = flushes?.(() => keys.clear());
return () => {
keys.clear();
stopFiltering();
stopFlushing?.();
};
})
: (anotherSource) => unique(anotherSource, source, selector);
}
export function merge(source, other) {
return other
? from((push) => {
const stopSource = source(push);
const stopOther = other(push);
return () => {
stopSource();
stopOther();
};
})
: (actualSource) => merge(actualSource, source);
}
export function distinct(source, compare = Object.is) {
return isStream(source)
? from((push) => {
let firstSent = false;
let previous;
return source((value) => {
firstSent
? compare(previous, value) || push(value)
: ((firstSent = true), push(value));
previous = value;
});
})
: (anotherSource) => distinct(anotherSource, source ?? compare);
}
export default {
of,
is: isStream,
map,
from,
scan,
take,
skip,
merge,
filter,
unique,
forEach,
distinct,
skipWhile,
takeWhile,
};