| 1 | 'use strict';
|
|---|
| 2 | var $ = require('../internals/export');
|
|---|
| 3 | var anObject = require('../internals/an-object');
|
|---|
| 4 | var call = require('../internals/function-call');
|
|---|
| 5 | var createIteratorProxy = require('../internals/iterator-create-proxy');
|
|---|
| 6 | var getIteratorDirect = require('../internals/get-iterator-direct');
|
|---|
| 7 | var iteratorClose = require('../internals/iterator-close');
|
|---|
| 8 | var uncurryThis = require('../internals/function-uncurry-this');
|
|---|
| 9 |
|
|---|
| 10 | var $RangeError = RangeError;
|
|---|
| 11 | var push = uncurryThis([].push);
|
|---|
| 12 |
|
|---|
| 13 | var IteratorProxy = createIteratorProxy(function () {
|
|---|
| 14 | var iterator = this.iterator;
|
|---|
| 15 | var next = this.next;
|
|---|
| 16 | var chunkSize = this.chunkSize;
|
|---|
| 17 | var buffer = [];
|
|---|
| 18 | var result, done;
|
|---|
| 19 | while (true) {
|
|---|
| 20 | result = anObject(call(next, iterator));
|
|---|
| 21 | done = !!result.done;
|
|---|
| 22 | if (done) {
|
|---|
| 23 | if (buffer.length) return buffer;
|
|---|
| 24 | this.done = true;
|
|---|
| 25 | return;
|
|---|
| 26 | }
|
|---|
| 27 | push(buffer, result.value);
|
|---|
| 28 | if (buffer.length === chunkSize) return buffer;
|
|---|
| 29 | }
|
|---|
| 30 | });
|
|---|
| 31 |
|
|---|
| 32 | // `Iterator.prototype.chunks` method
|
|---|
| 33 | // https://github.com/tc39/proposal-iterator-chunking
|
|---|
| 34 | $({ target: 'Iterator', proto: true, real: true, forced: true }, {
|
|---|
| 35 | chunks: function chunks(chunkSize) {
|
|---|
| 36 | var O = anObject(this);
|
|---|
| 37 | if (typeof chunkSize != 'number' || !chunkSize || chunkSize >>> 0 !== chunkSize) {
|
|---|
| 38 | return iteratorClose(O, 'throw', new $RangeError('chunkSize must be integer in [1, 2^32-1]'));
|
|---|
| 39 | }
|
|---|
| 40 | return new IteratorProxy(getIteratorDirect(O), {
|
|---|
| 41 | chunkSize: chunkSize
|
|---|
| 42 | });
|
|---|
| 43 | }
|
|---|
| 44 | });
|
|---|