Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
853 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 | // https://github.com/tc39/proposal-iterator-helpers
|
---|
3 | var $ = require('../internals/export');
|
---|
4 | var iterate = require('../internals/iterate');
|
---|
5 | var aFunction = require('../internals/a-function');
|
---|
6 | var anObject = require('../internals/an-object');
|
---|
7 |
|
---|
8 | $({ target: 'Iterator', proto: true, real: true }, {
|
---|
9 | reduce: function reduce(reducer /* , initialValue */) {
|
---|
10 | anObject(this);
|
---|
11 | aFunction(reducer);
|
---|
12 | var noInitial = arguments.length < 2;
|
---|
13 | var accumulator = noInitial ? undefined : arguments[1];
|
---|
14 | iterate(this, function (value) {
|
---|
15 | if (noInitial) {
|
---|
16 | noInitial = false;
|
---|
17 | accumulator = value;
|
---|
18 | } else {
|
---|
19 | accumulator = reducer(accumulator, value);
|
---|
20 | }
|
---|
21 | }, { IS_ITERATOR: true });
|
---|
22 | if (noInitial) throw TypeError('Reduce of empty iterator with no initial value');
|
---|
23 | return accumulator;
|
---|
24 | }
|
---|
25 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.