main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | 'use strict';
|
---|
| 2 | var $ = require('../internals/export');
|
---|
| 3 | var iterate = require('../internals/iterate');
|
---|
| 4 | var aCallable = require('../internals/a-callable');
|
---|
| 5 | var anObject = require('../internals/an-object');
|
---|
| 6 | var getIteratorDirect = require('../internals/get-iterator-direct');
|
---|
| 7 |
|
---|
| 8 | var $TypeError = TypeError;
|
---|
| 9 |
|
---|
| 10 | // `Iterator.prototype.reduce` method
|
---|
| 11 | // https://github.com/tc39/proposal-iterator-helpers
|
---|
| 12 | $({ target: 'Iterator', proto: true, real: true }, {
|
---|
| 13 | reduce: function reduce(reducer /* , initialValue */) {
|
---|
| 14 | anObject(this);
|
---|
| 15 | aCallable(reducer);
|
---|
| 16 | var record = getIteratorDirect(this);
|
---|
| 17 | var noInitial = arguments.length < 2;
|
---|
| 18 | var accumulator = noInitial ? undefined : arguments[1];
|
---|
| 19 | var counter = 0;
|
---|
| 20 | iterate(record, function (value) {
|
---|
| 21 | if (noInitial) {
|
---|
| 22 | noInitial = false;
|
---|
| 23 | accumulator = value;
|
---|
| 24 | } else {
|
---|
| 25 | accumulator = reducer(accumulator, value, counter);
|
---|
| 26 | }
|
---|
| 27 | counter++;
|
---|
| 28 | }, { IS_RECORD: true });
|
---|
| 29 | if (noInitial) throw new $TypeError('Reduce of empty iterator with no initial value');
|
---|
| 30 | return accumulator;
|
---|
| 31 | }
|
---|
| 32 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.