From c59f7b1dc638cf93443a2dc67ce06d346b04bc35 Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Sun, 16 Dec 2012 20:16:57 +0800 Subject: [PATCH 1/2] count simply returns total number if no function supplied. --- Readme.md | 3 ++- index.js | 4 +++- test/index.js | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 977ae85..dfa90f2 100644 --- a/Readme.md +++ b/Readme.md @@ -195,7 +195,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){ diff --git a/index.js b/index.js index 7eff715..be8e502 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); diff --git a/test/index.js b/test/index.js index 131cb3a..bd1cd14 100644 --- a/test/index.js +++ b/test/index.js @@ -336,6 +336,10 @@ 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 items if no function given', function(){ + var arr = Enumerable([1,2,3,4,5]); + arr.count().should.equal(5); + }) }) describe('.first()', function(){ From 81fcffa1eb25ff33e4b83c1007a2026aca19eb8b Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Tue, 18 Dec 2012 15:13:12 +0800 Subject: [PATCH 2/2] Add .length() --- Readme.md | 5 +++++ index.js | 12 ++++++++++++ test/index.js | 9 ++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index dfa90f2..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) @@ -204,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 be8e502..9fdd50f 100644 --- a/index.js +++ b/index.js @@ -404,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 bd1cd14..93f20e1 100644 --- a/test/index.js +++ b/test/index.js @@ -336,12 +336,19 @@ 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 items if no function given', function(){ + 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(){ it('should return the first value', function(){ _(['foo', 'bar']).first().should.equal('foo');