Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add context argument to invoke iterator in

  • Loading branch information...
commit c1028953610d275f751d460b02632821344059f1 1 parent 188d754
@stagas stagas authored
Showing with 60 additions and 15 deletions.
  1. +11 −3 Readme.md
  2. +18 −12 index.js
  3. +31 −0 test/index.js
View
14 Readme.md
@@ -9,7 +9,7 @@
## API
-### each(array, fn)
+### each(array, fn[, ctx])
Iterate an array:
@@ -19,7 +19,15 @@ each([1,2,3], function(num, i){
})
```
-### each(object, fn)
+ Optionally pass a context object:
+
+```js
+each([1,2,3], function(num, i){
+
+}, this)
+```
+
+### each(object, fn[, ctx])
Iterate an object's key / value pairs:
@@ -37,7 +45,7 @@ each(collection, function(val, i){
})
```
-### each(string, fn)
+### each(string, fn[, ctx])
Iterate a string's characters:
View
30 index.js
@@ -19,23 +19,26 @@ try {
var has = Object.prototype.hasOwnProperty;
/**
- * Iterate the given `obj` and invoke `fn(val, i)`.
+ * Iterate the given `obj` and invoke `fn(val, i)`
+ * in optional context `ctx`.
*
* @param {String|Array|Object} obj
* @param {Function} fn
+ * @param {Object} [ctx]
* @api public
*/
-module.exports = function(obj, fn){
+module.exports = function(obj, fn, ctx){
fn = toFunction(fn);
+ ctx = ctx || this;
switch (type(obj)) {
case 'array':
- return array(obj, fn);
+ return array(obj, fn, ctx);
case 'object':
- if ('number' == typeof obj.length) return array(obj, fn);
- return object(obj, fn);
+ if ('number' == typeof obj.length) return array(obj, fn, ctx);
+ return object(obj, fn, ctx);
case 'string':
- return string(obj, fn);
+ return string(obj, fn, ctx);
}
};
@@ -44,12 +47,13 @@ module.exports = function(obj, fn){
*
* @param {String} obj
* @param {Function} fn
+ * @param {Object} ctx
* @api private
*/
-function string(obj, fn) {
+function string(obj, fn, ctx) {
for (var i = 0; i < obj.length; ++i) {
- fn(obj.charAt(i), i);
+ fn.call(ctx, obj.charAt(i), i);
}
}
@@ -58,13 +62,14 @@ function string(obj, fn) {
*
* @param {Object} obj
* @param {Function} fn
+ * @param {Object} ctx
* @api private
*/
-function object(obj, fn) {
+function object(obj, fn, ctx) {
for (var key in obj) {
if (has.call(obj, key)) {
- fn(key, obj[key]);
+ fn.call(ctx, key, obj[key]);
}
}
}
@@ -74,11 +79,12 @@ function object(obj, fn) {
*
* @param {Array|Object} obj
* @param {Function} fn
+ * @param {Object} ctx
* @api private
*/
-function array(obj, fn) {
+function array(obj, fn, ctx) {
for (var i = 0; i < obj.length; ++i) {
- fn(obj[i], i);
+ fn.call(ctx, obj[i], i);
}
}
View
31 test/index.js
@@ -17,6 +17,16 @@ describe('each(arr, fn)', function(){
});
vals.should.eql([0,1,2]);
})
+
+ describe('when passed a context', function(){
+ it('should iterate in context', function(){
+ var vals = [];
+ each([1,2,3], function(val, i){
+ this.push(val);
+ }, vals);
+ vals.should.eql([1,2,3]);
+ })
+ })
})
describe('each(obj, fn)', function(){
@@ -39,6 +49,17 @@ describe('each(obj, fn)', function(){
vals.should.eql(['foo', 0, 'bar', 1]);
})
})
+
+ describe('when passed a context', function(){
+ it('should iterate in context', function(){
+ var user = { name: 'Tobi', age: 2 };
+ var vals = [];
+ each(user, function(key, val){
+ this.push([key, val]);
+ }, vals);
+ vals.should.eql([['name', 'Tobi'], ['age', 2]]);
+ })
+ })
})
describe('each(str, fn)', function(){
@@ -49,6 +70,16 @@ describe('each(str, fn)', function(){
});
vals.should.eql(['h', 0, 'e', 1, 'y', 2]);
})
+
+ describe('when passed a context', function(){
+ it('should iterate in context', function(){
+ var vals = [];
+ each('hey', function(c, i){
+ this.push(c, i);
+ }, vals);
+ vals.should.eql(['h', 0, 'e', 1, 'y', 2]);
+ })
+ })
})
describe('each(users, fn)', function(){
Please sign in to comment.
Something went wrong with that request. Please try again.