Skip to content

Commit 55af1e3

Browse files
mlueHaroenv
authored andcommitted
fix(utils): correct _.every method (#274)
* every now logical ANDs itself with results from previous iteration * this is slightly more performant - if only calls the evaluator if it isn't encountered a false * removing dist * adding test to guarantee fixing of old functionality * Update utils.js
1 parent 892a8f0 commit 55af1e3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/common/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ module.exports = {
6464
return result;
6565
}
6666
this.each(obj, function(val, key) {
67-
result = test.call(null, val, key, obj);
68-
if (!result) {
69-
return false;
67+
if (result) {
68+
result = test.call(null, val, key, obj) && result;
7069
}
7170
});
7271
return !!result;

test/unit/utils_spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@ describe('escapeHTML', function() {
4242
expect(actual).toEqual(false);
4343
});
4444
});
45+
46+
describe('every', function(){
47+
it('_.every should return false when at least one result is true ', function(){
48+
// simulating an implementation of Array.prototype.each
49+
_.each = function(obj, callback) {
50+
for (var i = 0; i < obj.length; i++){
51+
callback(obj[i], i, _);
52+
//note that we do not return here to break for loop, angular does not do this
53+
}
54+
};
55+
expect(
56+
_.every([
57+
{ isEmpty: function(){ return true; } },
58+
{ isEmpty: function(){ return false; } }
59+
], function (dataset) {
60+
return dataset.isEmpty();
61+
})).toEqual(false);
62+
})
63+
});

0 commit comments

Comments
 (0)