-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
174 lines (144 loc) · 3.82 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
class Iterator {
constructor(generator) {
this[Symbol.iterator] = generator;
}
async * [Symbol.asyncIterator]() {
for (const element of this) {
yield await element;
}
}
map(callback) {
const result = [];
for (const element of this) {
result.push(callback(element));
}
return result;
}
filter(callback) {
const result = [];
for (const element of this) {
if (callback(element)) {
result.push(element);
}
}
return result;
}
reduce(callback, initialValue) {
let empty = typeof initialValue === 'undefined';
let accumulator = initialValue;
let index = 0;
for (const currentValue of this) {
if (empty) {
accumulator = currentValue;
empty = false;
continue;
}
accumulator = callback(accumulator, currentValue, index, this);
index++;
}
if (empty) {
throw new TypeError('Reduce of empty Iterator with no initial value');
}
return accumulator;
}
some(callback) {
for (const element of this) {
if (callback(element)) {
return true;
}
}
return false;
}
every(callback) {
for (const element of this) {
if (!callback(element)) {
return false;
}
}
return true;
}
static fromIterable(iterable) {
return new Iterator(function * () {
for (const element of iterable) {
yield element;
}
});
}
toArray() {
return Array.from(this);
}
next() {
if (!this.currentInvokedGenerator) {
this.currentInvokedGenerator = this[Symbol.iterator]();
}
return this.currentInvokedGenerator.next();
}
reset() {
delete this.currentInvokedGenerator;
}
}
function rangeSimple(stop) {
return new Iterator(function * () {
for (let i = 0; i < stop; i++) {
yield i;
}
});
}
function rangeOverload(start, stop, step = 1) {
return new Iterator(function * () {
for (let i = start; i < stop; i += step) {
yield i;
}
});
}
function range(...args) {
if (args.length < 2) {
return rangeSimple(...args);
}
return rangeOverload(...args);
}
function enumerate(iterable) {
return new Iterator(function * () {
let index = 0;
for (const element of iterable) {
yield [index, element];
index++;
}
});
}
const zip = longest => (...iterables) => {
if (iterables.length < 2) {
throw new TypeError(`zip takes 2 iterables at least, ${iterables.length} given`);
}
return new Iterator(function * () {
const iterators = iterables.map(iterable => Iterator.fromIterable(iterable));
while (true) {
const row = iterators.map(iterator => iterator.next());
const check = longest ? row.every.bind(row) : row.some.bind(row);
if (check(next => next.done)) {
return;
}
yield row.map(next => next.value);
}
});
};
function items(obj) {
let {keys, get} = obj;
if (obj instanceof Map) {
keys = keys.bind(obj);
get = get.bind(obj);
} else {
keys = function () {
return Object.keys(obj);
};
get = function (key) {
return obj[key];
};
}
return new Iterator(function * () {
for (const key of keys()) {
yield [key, get(key)];
}
});
}
module.exports = {Iterator, range, enumerate, zip: zip(false), zipLongest: zip(true), items};