Skip to content

Commit 4dffa22

Browse files
committed
[BUGFIX beta] allow enumerable/any to match undefined as value
1 parent 5b09bd5 commit 4dffa22

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

packages/ember-runtime/lib/mixins/enumerable.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,23 @@ Ember.Enumerable = Ember.Mixin.create({
617617
@return {Boolean} `true` if the passed function returns `true` for any item
618618
*/
619619
any: function(callback, target) {
620-
var found = this.find(function(x, idx, i) {
621-
return !!callback.call(target, x, idx, i);
622-
});
620+
var len = get(this, 'length'),
621+
context = popCtx(),
622+
found = false,
623+
last = null,
624+
next, idx;
625+
626+
if (target === undefined) { target = null; }
627+
628+
for (idx = 0; idx < len && !found; idx++) {
629+
next = this.nextObject(idx, last, context);
630+
found = callback.call(target, next, idx, this);
631+
last = next;
632+
}
623633

624-
return typeof found !== 'undefined';
634+
next = last = null;
635+
context = pushCtx(context);
636+
return found;
625637
},
626638

627639
/**

packages/ember-runtime/tests/suites/enumerable/any.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ suite.test('any should stop invoking when you return true', function() {
3131
deepEqual(found, ary.slice(0,-2), 'items passed during any() should match');
3232
});
3333

34+
35+
suite.test('any should return true if any object matches the callback', function() {
36+
var obj = Ember.A([0, 1, 2]), result;
37+
38+
result = obj.any(function(i) { return !!i; });
39+
equal(result, true, 'return value of obj.any');
40+
});
41+
42+
43+
suite.test('any should return false if no object matches the callback', function() {
44+
var obj = Ember.A([0, null, false]), result;
45+
46+
result = obj.any(function(i) { return !!i; });
47+
equal(result, false, 'return value of obj.any');
48+
});
49+
50+
51+
suite.test('any should produce correct results even if the matching element is undefined', function() {
52+
var obj = Ember.A([undefined]), result;
53+
54+
result = obj.any(function(i) { return true; });
55+
equal(result, true, 'return value of obj.any');
56+
});
57+
58+
3459
suite.test('any should be aliased to some', function() {
3560
var obj = this.newObject(),
3661
ary = this.toArray(obj),

0 commit comments

Comments
 (0)