Skip to content

Commit

Permalink
Array.prototype.reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
msn0 committed Jun 4, 2019
1 parent 381643f commit c860b23
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Polyfills are also available over a CDN, for example
|[Array.prototype.forEach](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach?v=control#Polyfill)|328|
|[Array.prototype.includes](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes?v=control#Polyfill)|346|
|[Array.prototype.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill)|346|
|[Array.prototype.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Polyfill)|492|
|[String.prototype.includes](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill)|153|
|[String.prototype.repeat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat#Polyfill)|504|
|[String.prototype.startsWith](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#Polyfill)|117|
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"firstElementChild",
"includes",
"some",
"reduce",
"repeat",
"startsWith",
"endsWith",
Expand All @@ -47,6 +48,7 @@
"Array.prototype.findIndex",
"Array.prototype.includes",
"Array.prototype.some",
"Array.prototype.reduce",
"String.prototype.includes",
"String.prototype.repeat",
"String.prototype.startsWith",
Expand Down Expand Up @@ -87,6 +89,7 @@
"Array.prototype.forEach.js",
"Array.prototype.includes.js",
"Array.prototype.some.js",
"Array.prototype.reduce.js",
"CustomEvent.js",
"Element.prototype.classList.js",
"Element.prototype.closest.js",
Expand Down
1 change: 1 addition & 0 deletions rollup.build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rollup(input('./src/Array.prototype.findIndex/index.js')).then(output('./Array.p
rollup(input('./src/Array.prototype.forEach/index.js')).then(output('./Array.prototype.forEach.js'));
rollup(input('./src/Array.prototype.includes/index.js')).then(output('./Array.prototype.includes.js'));
rollup(input('./src/Array.prototype.some/index.js')).then(output('./Array.prototype.some.js'));
rollup(input('./src/Array.prototype.reduce/index.js')).then(output('./Array.prototype.reduce.js'));
rollup(input('./src/Array.of/index.js')).then(output('./Array.of.js'));
rollup(input('./src/String.prototype.repeat/index.js')).then(output('./String.prototype.repeat.js'));
rollup(input('./src/String.prototype.startsWith/index.js')).then(output('./String.prototype.startsWith.js'));
Expand Down
63 changes: 63 additions & 0 deletions src/Array.prototype.reduce/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: http://es5.github.io/#x15.4.4.21
// https://tc39.github.io/ecma262/#sec-array.prototype.reduce
if (!Array.prototype.reduce) {
Object.defineProperty(Array.prototype, 'reduce', {
value(callback /*, initialValue*/) {
if (this === null) {
throw new TypeError('Array.prototype.reduce ' +
'called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback +
' is not a function');
}

// 1. Let O be ? ToObject(this value).
var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// Steps 3, 4, 5, 6, 7
var k = 0;
var value;

if (arguments.length >= 2) {
value = arguments[1];
} else {
while (k < len && !(k in o)) {
k++;
}

// 3. If len is 0 and initialValue is not present,
// throw a TypeError exception.
if (k >= len) {
throw new TypeError('Reduce of empty array ' +
'with no initial value');
}
value = o[k++];
}

// 8. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kPresent be ? HasProperty(O, Pk).
// c. If kPresent is true, then
// i. Let kValue be ? Get(O, Pk).
// ii. Let accumulator be ? Call(
// callbackfn, undefined,
// « accumulator, kValue, k, O »).
if (k in o) {
value = callback(value, o[k], k, o);
}

// d. Increase k by 1.
k++;
}

// 9. Return accumulator.
return value;
}
});
}

0 comments on commit c860b23

Please sign in to comment.