Skip to content

Commit

Permalink
fix(utils): correct _.every method (#274)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mlue authored and Haroenv committed Dec 12, 2018
1 parent 892a8f0 commit 55af1e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ module.exports = {
return result;
}
this.each(obj, function(val, key) {
result = test.call(null, val, key, obj);
if (!result) {
return false;
if (result) {
result = test.call(null, val, key, obj) && result;
}
});
return !!result;
Expand Down
19 changes: 19 additions & 0 deletions test/unit/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ describe('escapeHTML', function() {
expect(actual).toEqual(false);
});
});

describe('every', function(){
it('_.every should return false when at least one result is true ', function(){
// simulating an implementation of Array.prototype.each
_.each = function(obj, callback) {
for (var i = 0; i < obj.length; i++){
callback(obj[i], i, _);
//note that we do not return here to break for loop, angular does not do this
}
};
expect(
_.every([
{ isEmpty: function(){ return true; } },
{ isEmpty: function(){ return false; } }
], function (dataset) {
return dataset.isEmpty();
})).toEqual(false);
})
});

0 comments on commit 55af1e3

Please sign in to comment.