-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (215 loc) · 5.95 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
export function isIterable(value) {
return value != null && typeof value[Symbol.iterator] === "function";
}
export function map(source, callback) {
return callback
? from(function* () {
for (const value of source) yield callback(value);
})
: (anotherSource) => map(anotherSource, source);
}
export function chain(source, callback) {
return callback
? from(function* () {
for (const value of source) yield* callback(value);
})
: (anotherSource) => chain(anotherSource, source);
}
export function filter(source, predicate) {
return predicate
? from(function* () {
for (const value of source) if (predicate(value)) yield value;
})
: (anotherSource) => filter(anotherSource, source);
}
export function forEach(source, callback) {
if (callback) for (const value of source) callback(value);
else return (anotherSource) => forEach(anotherSource, source);
}
export function of(...values) {
return from(values);
}
export function from(value) {
if (isIterable(value)) return value;
if (typeof value === "function") return (value[Symbol.iterator] = value);
return from(function* () {
for (const key in value) Number.isNaN(+key) || (yield value[key]);
});
}
export function fold(source, accumulator, reduce) {
if (arguments.length === 3) {
for (const value of source) accumulator = reduce(accumulator, value);
return accumulator;
} else return (anotherSource) => fold(anotherSource, source, accumulator);
}
export function concat(source, other) {
return other
? from(function* () {
yield* source;
yield* other;
})
: (anotherSource) => concat(anotherSource, source);
}
export function all(source, predicate) {
if (predicate) {
for (const item of source) if (!predicate(item)) return false;
return true;
} else return (anotherSource) => all(anotherSource, source);
}
export function any(source, predicate) {
if (predicate) {
for (const item of source) if (predicate(item)) return true;
return false;
} else return (anotherSource) => any(anotherSource, source);
}
export function take(source, amount) {
if (arguments.length === 2) {
let _amount = amount;
return takeWhile(
source,
() => _amount-- > 0 || ((_amount = amount), false),
);
} else return (anotherSource) => take(anotherSource, source);
}
export function takeWhile(source, predicate) {
return predicate
? from(function* () {
for (const value of source) {
if (predicate(value)) yield value;
else return;
}
})
: (anotherSource) => takeWhile(anotherSource, source);
}
export function skip(source, amount) {
return arguments.length === 2
? from(function* () {
let _amount = amount;
for (const value of source) _amount <= 0 ? yield value : _amount--;
})
: (anotherSource) => skip(anotherSource, source);
}
export function skipWhile(source, predicate) {
return predicate
? from(function* () {
let skipping = true;
for (const value of source)
skipping
? !(skipping = predicate(value)) && (yield value)
: yield value;
})
: (anotherSource) => skipWhile(anotherSource, source);
}
export function enumerate(source) {
return from(function* () {
let index = 0;
for (const value of source) yield [value, index++];
});
}
export function sort(source, compare) {
return compare
? from(function* () {
yield* Array.from(source).sort(compare);
})
: (anotherSource) => sort(anotherSource, source);
}
export function count(source) {
return fold(source, 0, (amount) => amount + 1);
}
export function scan(source, accumulator, reduce) {
return arguments.length === 3
? from(function* () {
let intermediate = accumulator;
for (const value of source)
yield (intermediate = reduce(intermediate, value));
})
: (anotherSource) => scan(anotherSource, source, accumulator);
}
export function first(source) {
return source[Symbol.iterator]().next().value;
}
export function isEmpty(source) {
return Boolean(source[Symbol.iterator]().next().done);
}
export function last(source) {
return fold(source, (_, value) => value);
}
export function zip(source, other) {
return other
? from(function* () {
const otherIterator = other[Symbol.iterator]();
for (const item of source) {
const { done, value } = otherIterator.next();
if (done) return;
yield [item, value];
}
})
: (anotherSource) => zip(anotherSource, source);
}
export function unzip(source) {
return [
from(function* () {
for (const [first] of source) yield first;
}),
from(function* () {
for (const [, second] of source) yield second;
}),
];
}
export function find(source, predicate) {
if (predicate) {
for (const item of source) if (predicate(item)) return item;
} else return (anotherSource) => find(anotherSource, source);
}
export function group(source, callback) {
return callback
? from(function* () {
const groups = new Map();
forEach(source, (value) => {
const key = callback(value);
groups.set(key, concat(groups.get(key) ?? of(), of(value)));
});
for (const pair of groups) yield pair;
})
: (anotherSource) => group(anotherSource, source);
}
export function unique(source, callback = (item) => item) {
return isIterable(source)
? from(function* () {
const cache = new Set();
for (const item of source) {
const key = callback(item);
cache.has(key) || (cache.add(key), yield item);
}
})
: (anotherSource) => unique(anotherSource, source);
}
export default {
of,
is: isIterable,
all,
zip,
any,
map,
skip,
last,
from,
fold,
find,
take,
sort,
scan,
unzip,
count,
group,
first,
chain,
filter,
concat,
unique,
isEmpty,
forEach,
takeWhile,
skipWhile,
enumerate,
};