(collection, useKeys)
| 279 | } |
| 280 | |
| 281 | export function reverseFactory(collection, useKeys) { |
| 282 | const reversedSequence = makeSequence(collection); |
| 283 | reversedSequence._iter = collection; |
| 284 | reversedSequence.size = collection.size; |
| 285 | reversedSequence.reverse = () => collection; |
| 286 | if (collection.flip) { |
| 287 | reversedSequence.flip = function () { |
| 288 | const flipSequence = flipFactory(collection); |
| 289 | flipSequence.reverse = () => collection.flip(); |
| 290 | return flipSequence; |
| 291 | }; |
| 292 | } |
| 293 | reversedSequence.get = (key, notSetValue) => |
| 294 | collection.get(useKeys ? key : -1 - key, notSetValue); |
| 295 | reversedSequence.has = (key) => collection.has(useKeys ? key : -1 - key); |
| 296 | reversedSequence.includes = (value) => collection.includes(value); |
| 297 | reversedSequence.cacheResult = cacheResultThrough; |
| 298 | reversedSequence.__iterate = function (fn, reverse) { |
| 299 | let i = 0; |
| 300 | // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here |
| 301 | reverse && ensureSize(collection); |
| 302 | return collection.__iterate( |
| 303 | (v, k) => fn(v, useKeys ? k : reverse ? this.size - ++i : i++, this), |
| 304 | !reverse |
| 305 | ); |
| 306 | }; |
| 307 | reversedSequence.__iterator = (type, reverse) => { |
| 308 | let i = 0; |
| 309 | // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here |
| 310 | reverse && ensureSize(collection); |
| 311 | const iterator = collection.__iterator(ITERATE_ENTRIES, !reverse); |
| 312 | return new Iterator(() => { |
| 313 | const step = iterator.next(); |
| 314 | if (step.done) { |
| 315 | return step; |
| 316 | } |
| 317 | const entry = step.value; |
| 318 | return iteratorValue( |
| 319 | type, |
| 320 | // `__iterator` is an arrow function, so `this` is not the reversed |
| 321 | // sequence here — read `reversedSequence.size` explicitly. |
| 322 | useKeys ? entry[0] : reverse ? reversedSequence.size - ++i : i++, |
| 323 | entry[1], |
| 324 | step |
| 325 | ); |
| 326 | }); |
| 327 | }; |
| 328 | return reversedSequence; |
| 329 | } |
| 330 | |
| 331 | export function filterFactory(collection, predicate, context, useKeys) { |
| 332 | const filterSequence = makeSequence(collection); |
no test coverage detected