-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
implementation.js
67 lines (56 loc) · 1.83 KB
/
implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
var Call = require('es-abstract/2024/Call');
var Get = require('es-abstract/2024/Get');
var HasProperty = require('es-abstract/2024/HasProperty');
var IsCallable = require('es-abstract/2024/IsCallable');
var LengthOfArrayLike = require('es-abstract/2024/LengthOfArrayLike');
var ToObject = require('es-object-atoms/ToObject');
var ToString = require('es-abstract/2024/ToString');
var callBound = require('call-bound');
var isString = require('is-string');
var $Object = require('es-object-atoms');
// Check failure of by-index access of string characters (IE < 9) and failure of `0 in boxedString` (Rhino)
var boxedString = $Object('a');
var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
var $split = callBound('String.prototype.split');
var $TypeError = require('es-errors/type');
module.exports = function reduceRight(callbackfn) {
var O = ToObject(this);
var self = splitString && isString(O) ? $split(O, '') : O;
var len = LengthOfArrayLike(self);
if (!IsCallable(callbackfn)) {
throw new $TypeError('Array.prototype.reduceRight callback must be a function');
}
if (len === 0 && arguments.length < 2) {
throw new $TypeError('reduceRight of empty array with no initial value');
}
var k = len - 1;
var accumulator;
var Pk, kPresent;
if (arguments.length > 1) {
accumulator = arguments[1];
} else {
kPresent = false;
while (!kPresent && k >= 0) {
Pk = ToString(k);
kPresent = HasProperty(O, Pk);
if (kPresent) {
accumulator = Get(O, Pk);
}
k -= 1;
}
if (!kPresent) {
throw new $TypeError('reduceRight of empty array with no initial value');
}
}
while (k >= 0) {
Pk = ToString(k);
kPresent = HasProperty(O, Pk);
if (kPresent) {
var kValue = Get(O, Pk);
accumulator = Call(callbackfn, void undefined, [accumulator, kValue, k, O]);
}
k -= 1;
}
return accumulator;
};