Skip to content

Commit 72ff0df

Browse files
Fix regression with handling of $break inside Array#inject. [close prototypejs#162]
1 parent 2c23562 commit 72ff0df

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/prototype/lang/array.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -720,22 +720,11 @@ Array.from = $A;
720720
var every = wrapNative(Array.prototype.every);
721721
}
722722

723-
// Prototype's `Array#inject` behaves similarly to ES5's `Array#reduce`.
724-
var _reduce = arrayProto.reduce;
725-
function inject(memo, iterator) {
726-
iterator = iterator || Prototype.K;
727-
var context = arguments[2];
728-
// The iterator must be bound, as `Array#reduce` always binds to
729-
// `undefined`.
730-
return _reduce.call(this, iterator.bind(context), memo);
731-
}
723+
// We used to define an `inject` method here that relied on ES5's
724+
// `Array#reduce` (if present), but using `reduce` prevents us from
725+
// catching a thrown `$break`. So arrays now use the standard
726+
// `Enumerable.inject` like they did previously.
732727

733-
// Piggyback on `Array#reduce` if it exists; otherwise fall back to the
734-
// standard `Enumerable.inject`.
735-
if (!arrayProto.reduce) {
736-
var inject = Enumerable.inject;
737-
}
738-
739728
Object.extend(arrayProto, Enumerable);
740729

741730
if (!arrayProto._reverse)
@@ -753,7 +742,6 @@ Array.from = $A;
753742
any: some,
754743
every: every,
755744
all: every,
756-
inject: inject,
757745

758746
clear: clear,
759747
first: first,

test/unit/enumerable_test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ new Test.Unit.Runner({
271271
Fixtures.Primes.inject(0, function(sum, value) {
272272
return sum + value;
273273
}));
274+
275+
var sum = Fixtures.Primes.inject(0, function(sum, value) {
276+
if (value === 5) throw $break;
277+
return sum + value;
278+
});
279+
280+
this.assertEqual(6, sum, 'inject should catch a thrown $break');
274281
},
275282

276283
"test #inject passes memo, value, index and collection to the iterator": function() {

0 commit comments

Comments
 (0)