Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Implemented Manager.map & Manager.filter; Changes to Manager.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
miksago committed Jul 20, 2010
1 parent 38a8af4 commit ecb6a7c
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions lib/ws/manager.js
Expand Up @@ -86,11 +86,44 @@ Manager.prototype.find = function(id, callback){
}
};

Manager.prototype.forEach = function(callback){
Manager.prototype.forEach = function(callback, thisArg){
var current = this._head;

while(current !== null){
callback(current.client);
callback.call(thisArg, current.client);
current = current._next;
}
};

Manager.prototype.map = function(callback, thisArg){
var current = this._head
, len = 0
, result = new Array(this._length);
, mappedValue;

while(current !== null){
result[len] = callback.call(thisArg, current.client, len, this._head);
current = current._next;
++len;
}

return result;
};

Manager.prototype.filter = function(callback, thisArg){
var current = this._head
, len = 0
, result = new Array(this._length);
, mappedValue;

while(current !== null){
if( Boolean(callback.call(thisArg, current.client, len, this._head)) ){
result[len] = current.client;
++len;
}

current = current._next;
}

return result;
};

0 comments on commit ecb6a7c

Please sign in to comment.