Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added alternative `foreach(...).each` to replace `foreach(...).do` be…

…cause `do` is reserved word in JavaScript.
  • Loading branch information...
commit d28294673563b1ba934ff6276b87eea10f282164 1 parent f42b22f
@jheusala authored
View
6 README.md
@@ -53,17 +53,17 @@ Foreach
Write each array element to console:
var a = [1,2,3,4,5];
- foreach(a).do(function(v) { console.log(v); });
+ foreach(a).each(function(v) { console.log(v); });
Write each object key=value pair to console:
a = {'foo':1,'bar':2};
- foreach(a).do(function(v, k) { console.log(k+"="+v); });
+ foreach(a).each(function(v, k) { console.log(k+"="+v); });
Write single value to console:
a = 'foobar';
- foreach(a).do(function(v) { console.log(v); });
+ foreach(a).each(function(v) { console.log(v); });
Split
-----
View
14 examples/foreach.js
@@ -5,15 +5,15 @@ var foreach = require('snippets').foreach;
// Foreach array
var a = [1,2,3,4,5];
-foreach(a).do(function(v) { console.log(v); });
+foreach(a).each(function(v) { console.log(v); });
// Foreach object
a = {'foo':1,'bar':2};
-foreach(a).do(function(v, k) { console.log(k+"="+v); });
+foreach(a).each(function(v, k) { console.log(k+"="+v); });
// Foreach single variable
a = 'foobar';
-foreach(a).do(function(v) { console.log(v); });
+foreach(a).each(function(v) { console.log(v); });
/* Foreach examples -- Specific format */
@@ -21,18 +21,18 @@ var foreach = require('snippets/lib/foreach');
// Foreach only arrays
a = [1,2,3,4,5];
-foreach.any(a).do(function(v) { console.log(v); });
+foreach.any(a).each(function(v) { console.log(v); });
// Foreach only arrays
a = [1,2,3,4,5];
-foreach.array(a).do(function(v) { console.log(v); });
+foreach.array(a).each(function(v) { console.log(v); });
// Foreach only objects
a = {'foo':1,'bar':2};
-foreach.object(a).do(function(v, k) { console.log(k+"="+v); });
+foreach.object(a).each(function(v, k) { console.log(k+"="+v); });
// Foreach only single elements
a = 'foobar';
-foreach.single(a).do(function(v) { console.log(v); });
+foreach.single(a).each(function(v) { console.log(v); });
/* EOF */
View
14 lib/foreach/foreach.js
@@ -39,7 +39,7 @@ function foreach_array() {
for(i=0; i<length; ++i) f(values[i], i);
}
}
- return {'do':_do};
+ return {'do':_do,'each':_do};
}
/* Foreach every element in an object */
@@ -52,7 +52,7 @@ function foreach_object() {
for(i in values) if(values.hasOwnProperty(i)) f(values[i], i);
}
}
- return {'do':_do};
+ return {'do':_do,'each':_do};
}
/* Foreach single element */
@@ -62,7 +62,7 @@ function foreach_single() {
var a=0;
for(; a<args_length; ++a) f(args[a], a);
}
- return {'do':_do};
+ return {'do':_do,'each':_do};
}
/* Foreach every element in an array or object, otherwise the element itself */
@@ -72,12 +72,12 @@ function foreach_any() {
var a=0, values;
for(; a<args_length; ++a) {
values = args[a];
- if(is.array(values)) foreach_array(values).do(f);
- else if(is.object(values)) foreach_object(values).do(f);
- else foreach_single(values).do(f);
+ if(is.array(values)) foreach_array(values).each(f);
+ else if(is.object(values)) foreach_object(values).each(f);
+ else foreach_single(values).each(f);
}
}
- return {'do':_do};
+ return {'do':_do,'each':_do};
}
exports.any = foreach_any;
View
2  lib/snippets.js
@@ -39,7 +39,7 @@ function join(data, separators, def_separator) {
var undefined, buf, separator,
def_separator = init(def_separator, ",");
- foreach(data).do(function(value) {
+ foreach(data).each(function(value) {
if(buf === undefined) {
buf = value;
return;
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "snippets",
"description": "JavaScript Snippet Collection",
- "version": "0.0.5",
+ "version": "0.0.6",
"homepage": "http://github.com/jheusala/js-snippets",
"repository": {
"type": "git",
Please sign in to comment.
Something went wrong with that request. Please try again.