-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathiterator-helpers.js
More file actions
303 lines (259 loc) · 8.67 KB
/
iterator-helpers.js
File metadata and controls
303 lines (259 loc) · 8.67 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {expect} from 'chai'
import {apply, isPolyfilled, isSupported} from '../src/iterator-helpers.ts'
// eslint-disable-next-line i18n-text/no-en
describe('Iterator helpers', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})
it('isSupported returns true when Iterator is a function (native support)', () => {
// Native Iterator in Chromium is a function, not an object.
// Simulate this by temporarily replacing globalThis.Iterator with a function.
const original = globalThis.Iterator
try {
apply() // ensure polyfill is applied first so prototype methods exist
const proto = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))
// Create a function-based Iterator with a `from` method, like native Chromium
globalThis.Iterator = function Iterator() {}
globalThis.Iterator.from = function from() {}
// Assign all required methods to the iterator prototype
for (const method of [
'map',
'filter',
'take',
'drop',
'flatMap',
'reduce',
'toArray',
'forEach',
'some',
'every',
'find',
]) {
if (!(method in proto)) {
proto[method] = function () {}
}
}
expect(isSupported()).to.equal(true)
} finally {
globalThis.Iterator = original
}
})
// Helper to create an iterator from an array
function* arrayIterator(arr) {
for (const item of arr) {
yield item
}
}
describe('map', () => {
beforeEach(() => apply())
it('maps values', () => {
const iter = arrayIterator([1, 2, 3])
const mapped = iter.map(x => x * 2)
expect([...mapped]).to.eql([2, 4, 6])
})
it('passes index to mapper', () => {
const iter = arrayIterator(['a', 'b', 'c'])
const mapped = iter.map((x, i) => `${i}:${x}`)
expect([...mapped]).to.eql(['0:a', '1:b', '2:c'])
})
})
describe('filter', () => {
beforeEach(() => apply())
it('filters values', () => {
const iter = arrayIterator([1, 2, 3, 4, 5])
const filtered = iter.filter(x => x % 2 === 0)
expect([...filtered]).to.eql([2, 4])
})
it('passes index to predicate', () => {
const iter = arrayIterator(['a', 'b', 'c', 'd'])
const filtered = iter.filter((_, i) => i % 2 === 0)
expect([...filtered]).to.eql(['a', 'c'])
})
})
describe('take', () => {
beforeEach(() => apply())
it('takes first n values', () => {
const iter = arrayIterator([1, 2, 3, 4, 5])
const taken = iter.take(3)
expect([...taken]).to.eql([1, 2, 3])
})
it('handles taking more than available', () => {
const iter = arrayIterator([1, 2])
const taken = iter.take(5)
expect([...taken]).to.eql([1, 2])
})
})
describe('drop', () => {
beforeEach(() => apply())
it('drops first n values', () => {
const iter = arrayIterator([1, 2, 3, 4, 5])
const dropped = iter.drop(2)
expect([...dropped]).to.eql([3, 4, 5])
})
it('handles dropping more than available', () => {
const iter = arrayIterator([1, 2])
const dropped = iter.drop(5)
expect([...dropped]).to.eql([])
})
})
describe('flatMap', () => {
beforeEach(() => apply())
it('flattens mapped values', () => {
const iter = arrayIterator([1, 2, 3])
const flatMapped = iter.flatMap(x => [x, x * 2])
expect([...flatMapped]).to.eql([1, 2, 2, 4, 3, 6])
})
it('handles empty results', () => {
const iter = arrayIterator([1, 2, 3])
const flatMapped = iter.flatMap(x => (x % 2 === 0 ? [x] : []))
expect([...flatMapped]).to.eql([2])
})
})
describe('reduce', () => {
beforeEach(() => apply())
it('reduces with initial value', () => {
const iter = arrayIterator([1, 2, 3, 4])
const sum = iter.reduce((acc, x) => acc + x, 0)
expect(sum).to.equal(10)
})
it('reduces without initial value uses first element as accumulator', () => {
const iter = arrayIterator([1, 2, 3, 4])
const calls = []
iter.reduce((acc, x, i) => {
calls.push({acc, x, index: i})
return acc + x
})
// First element (1) is used as accumulator; reducer is called starting at index 1
expect(calls).to.eql([
{acc: 1, x: 2, index: 1},
{acc: 3, x: 3, index: 2},
{acc: 6, x: 4, index: 3},
])
})
it('reduces without initial value returns correct sum', () => {
const iter = arrayIterator([1, 2, 3, 4])
const sum = iter.reduce((acc, x) => acc + x)
expect(sum).to.equal(10)
})
it('throws on empty iterator without initial value', () => {
const iter = arrayIterator([])
expect(() => iter.reduce((acc, x) => acc + x)).to.throw(TypeError)
})
it('treats explicit undefined as a provided initial value', () => {
const iter = arrayIterator([1, 2, 3])
const calls = []
iter.reduce((acc, x, i) => {
calls.push({acc, x, index: i})
return x
}, undefined)
// undefined is the initial accumulator; reducer is called starting at index 0
expect(calls[0]).to.eql({acc: undefined, x: 1, index: 0})
expect(calls).to.have.lengthOf(3)
})
})
describe('toArray', () => {
beforeEach(() => apply())
it('converts iterator to array', () => {
const iter = arrayIterator([1, 2, 3])
expect(iter.toArray()).to.eql([1, 2, 3])
})
it('handles empty iterator', () => {
const iter = arrayIterator([])
expect(iter.toArray()).to.eql([])
})
})
describe('forEach', () => {
beforeEach(() => apply())
it('calls callback for each value', () => {
const iter = arrayIterator([1, 2, 3])
const results = []
// eslint-disable-next-line github/array-foreach
iter.forEach((x, i) => results.push({value: x, index: i}))
expect(results).to.eql([
{value: 1, index: 0},
{value: 2, index: 1},
{value: 3, index: 2},
])
})
})
describe('some', () => {
beforeEach(() => apply())
it('returns true if any value matches', () => {
const iter = arrayIterator([1, 2, 3, 4])
expect(iter.some(x => x > 3)).to.be.true
})
it('returns false if no value matches', () => {
const iter = arrayIterator([1, 2, 3])
expect(iter.some(x => x > 5)).to.be.false
})
})
describe('every', () => {
beforeEach(() => apply())
it('returns true if all values match', () => {
const iter = arrayIterator([2, 4, 6])
expect(iter.every(x => x % 2 === 0)).to.be.true
})
it('returns false if any value does not match', () => {
const iter = arrayIterator([2, 3, 4])
expect(iter.every(x => x % 2 === 0)).to.be.false
})
})
describe('find', () => {
beforeEach(() => apply())
it('returns first matching value', () => {
const iter = arrayIterator([1, 2, 3, 4])
expect(iter.find(x => x > 2)).to.equal(3)
})
it('returns undefined if no match', () => {
const iter = arrayIterator([1, 2, 3])
expect(iter.find(x => x > 5)).to.be.undefined
})
})
describe('Iterator.from', () => {
beforeEach(() => apply())
it('converts iterable to iterator', () => {
const IteratorConstructor = globalThis.Iterator
if (!IteratorConstructor?.from) {
// Skip if Iterator.from is not available after polyfill
return
}
const iter = IteratorConstructor.from([1, 2, 3])
expect([...iter]).to.eql([1, 2, 3])
})
it('returns iterator as-is', () => {
const IteratorConstructor = globalThis.Iterator
if (!IteratorConstructor?.from) {
// Skip if Iterator.from is not available after polyfill
return
}
const original = arrayIterator([1, 2, 3])
const iter = IteratorConstructor.from(original)
expect(iter).to.equal(original)
})
it('handles primitive iterable strings', () => {
const IteratorConstructor = globalThis.Iterator
if (!IteratorConstructor?.from) {
// Skip if Iterator.from is not available after polyfill
return
}
const iter = IteratorConstructor.from('abc')
expect([...iter]).to.eql(['a', 'b', 'c'])
})
})
describe('chaining', () => {
beforeEach(() => apply())
it('chains multiple operations', () => {
const iter = arrayIterator([1, 2, 3, 4, 5, 6])
const result = iter
.filter(x => x % 2 === 0)
.map(x => x * 2)
.take(2)
.toArray()
expect(result).to.eql([4, 8])
})
})
})