diff --git a/Readme.md b/Readme.md index 977ae85..107a7ef 100644 --- a/Readme.md +++ b/Readme.md @@ -38,6 +38,7 @@ users - [.none()](#nonefnfunctionstring) - [.any()](#anyfnfunction) - [.count()](#countfnfunction) + - [.length()](#length) - [.indexOf()](#indexofobjmixed) - [.has()](#hasobjmixed) - [.reduce()](#reducefnfunctionvalmixed) @@ -195,7 +196,8 @@ users ## .count(fn:Function) - Count the number of times `fn(val, i)` returns true. + Count the number of times `fn(val, i)` returns true + or, if no function supplied, return total number of values. ```js var n = pets.count(function(pet){ @@ -203,6 +205,10 @@ users }) ``` +## .length() + + Return total number of values. + ## .indexOf(obj:Mixed) Determine the indexof `obj` or return `-1`. diff --git a/index.js b/index.js index 7eff715..9fdd50f 100644 --- a/index.js +++ b/index.js @@ -379,7 +379,8 @@ proto.any = function(fn){ }; /** - * Count the number of times `fn(val, i)` returns true. + * Count the number of times `fn(val, i)` returns true + * or, if no function supplied, return total number of values. * * var n = pets.count(function(pet){ * return pet.species == 'ferret' @@ -394,6 +395,7 @@ proto.count = function(fn){ var val; var vals = this.__iterate__(); var len = vals.length(); + if (!fn) return len; var n = 0; for (var i = 0; i < len; ++i) { val = vals.get(i); @@ -402,6 +404,18 @@ proto.count = function(fn){ return n; }; +/** + * Return total number of values. + * + * @return {Number} + * @api public + */ + +proto.length = function() { + var vals = this.__iterate__(); + return vals.length(); +}; + /** * Determine the indexof `obj` or return `-1`. * diff --git a/test/index.js b/test/index.js index 131cb3a..93f20e1 100644 --- a/test/index.js +++ b/test/index.js @@ -336,6 +336,17 @@ describe('.count(fn)', function(){ var arr = Enumerable([1,2,3,4,5]); arr.count(function(n){ return n > 3 }).should.equal(2); }) + it('should return total number of items if no function given', function(){ + var arr = Enumerable([1,2,3,4,5]); + arr.count().should.equal(5); + }) +}) + +describe('.length()', function() { + it('should return total number of items', function(){ + var arr = Enumerable([1,2,3,4,5]); + arr.length().should.equal(5); + }) }) describe('.first()', function(){