Skip to content

Loading…

count(): simply return total number of items if no function supplied. #15

Open
wants to merge 2 commits into from

2 participants

@timoxley
Components member

As far as I can tell, there's no way to access the length of the enumerable's data via the enumerable interface without invoking __iterator__.length, or doing .count(function(){return true}). This grants access to the length via calling count() without a function argument, e.g.

var arr = Enumerable([1,2,3,4,5]);
arr.count().should.equal(5);
@timoxley
Components member

Possible should also add a length() method or something.

@tj
Components member
tj commented

+1 for .length()

@tj
Components member
tj commented

this needs a rebase apparently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 18, 2012
  1. @timoxley
  2. @timoxley

    Add .length()

    timoxley committed
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 2 deletions.
  1. +7 −1 Readme.md
  2. +15 −1 index.js
  3. +11 −0 test/index.js
View
8 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`.
View
16 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);
@@ -403,6 +405,18 @@ proto.count = function(fn){
};
/**
+ * 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`.
*
* @param {Mixed} obj
View
11 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(){
Something went wrong with that request. Please try again.