Permalink
Browse files

Fixes 2169.

As per spec, `Array.filter` should return the filtered items, and not
andany possible mutation that might have occurred in the callback. See
referenced issue for more information.

Added coverage for case.

PASSED: IE6-9; FF3-5, 8, 11; Chrome latest; Safari 5; Opera 11
  • Loading branch information...
1 parent bb22a20 commit 676f6ceed129c8b6cc0d9078014c07604694db18 @ibolmo ibolmo committed Dec 19, 2011
Showing with 19 additions and 2 deletions.
  1. +3 −2 Source/Types/Array.js
  2. +16 −0 Specs/1.4base/Types/Array.js
@@ -26,8 +26,9 @@ Array.implement({
filter: function(fn, bind){
var results = [];
- for (var i = 0, l = this.length >>> 0; i < l; i++){
- if ((i in this) && fn.call(bind, this[i], i, this)) results.push(this[i]);
+ for (var value, i = 0, l = this.length >>> 0; i < l; i++) if (i in this){
+ value = this[i];
+ if (fn.call(bind, value, i, this)) results.push(value);
}
return results;
},
@@ -26,4 +26,20 @@ describe('Array', function(){
expect([].some.call(object, fn)).toBe(false);
});
+ describe('Array.filter', function(){
+
+ it('should return the original item, and not any mutations.', function(){
+
+ var result = [0, 1, 2].filter(function(num, i, array){
+ if (num == 1){
+ array[i] = 'mutation';
+ return true;
+ }
+ });
+
+ expect(result[0]).toEqual(1);
+ });
+
+ });
+
});

0 comments on commit 676f6ce

Please sign in to comment.