Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

forEach()

  • Loading branch information...
commit 07be614eb84deda0a1b487329c6b392604fd9a2b 1 parent 30b4b51
SHIN Suzuki authored
View
13 ArrayStream.js
@@ -43,6 +43,19 @@ ArrayStream.create = function(arr, op) {
return new this(arr, op);
};
+ArrayStream.forEach = function(arr, op, fn) {
+ var args = Array.prototype.slice.call(arguments);
+ var arg = args.shift();
+ var fn = args.pop();
+
+ var stream = ArrayStream.create(arg, args.length? args[0]: null);
+
+ stream.on("data", function(value, key) {
+ fn(value, key);
+ });
+ return stream;
+};
+
/**
* extends Stream
**/
View
38 README.md
@@ -1,23 +1,21 @@
-ArrayStream.js 0.0.3
+ArrayStream.js 0.0.5
==========
[Node.js] ReadableStream from array (or hash variable)
-Change Log
-
-----------------
-* [0.0.1]: Release
Overview
----------------
-### Installation ###
- git clone git://github.com/shinout/ArrayStream.git
+## Installation ##
+
+ $ git clone git://github.com/shinout/ArrayStream.git
OR
- npm install arraystream
+ $ npm install arraystream
+
+## Usage ##
+### with Array ###
-### Usage ###
-#### with Array ####
var ArrayStream = require('arraystream');
var stream = ArrayStream.create(['hoge', 'fuga', 'piyo']);
@@ -36,7 +34,8 @@ Overview
-#### with Object ####
+
+### with Object ###
var ArrayStream = require('arraystream');
var objstream = new ArrayStream({a:'hoge', b:'fuga', c:'piyo']);
@@ -54,3 +53,20 @@ Overview
});
+### syntax sugar (forEach) ###
+
+ var arr = ['hoge', 'fuga', 'piyo'];
+ var stream = ArrayStream.forEach(arr, function(value, key) {
+ console.log(value); // hoge, fuga, piyo
+ console.log(key); // 0, 1, 2
+ });
+
+ stream.on('end', function() { // emitted at the end of iteration
+ console.log('end');
+ });
+
+ stream.on('error', function(e) { // emitted when an error occurred
+ console.log(e);
+ });
+
+
View
5 package.json
@@ -1,6 +1,6 @@
{
"name": "arraystream"
-, "version": "0.0.4"
+, "version": "0.0.5"
, "description": "ReadableStream of arrays and hash variables."
, "keywords": ["ReadableStream", "array", "object", "stream"]
, "author": "SHIN Suzuki <shinout310@gmail.com>"
@@ -14,5 +14,8 @@
, "url": "https://github.com/shinout/ArrayStream/raw/master/LICENCE"
}
]
+, "devDependencies" : {
+ "termcolor": ">=v0.2.0"
+ }
, "main": "./ArrayStream.js"
}
View
18 test/forEach.js
@@ -0,0 +1,18 @@
+var ArrayStream = require('../ArrayStream');
+require('./load.test').load(global);
+require('termcolor').define;
+
+var TOTAL = 1000;
+var fs = require('fs');
+
+/**
+ * normal usage (arr)
+ */
+var arr = [];
+for (var i=0; i<TOTAL; i++) {
+ arr[i] = Math.sin(i);
+}
+
+var as = ArrayStream.forEach(arr, function(v, k) {
+ T.equal(v, arr[k], 'foreach');
+});
View
1  test/load.test.js
@@ -1,5 +1,4 @@
var node = (typeof exports == 'object' && exports === this);
-if (node) { require('./termcolor').define() }
var assert = (node) ? require('assert') : {};
var T = { count : 0, success : 0 };
View
59 test/termcolor.js
@@ -1,59 +0,0 @@
-const colors = {
- 'clear' : '\033[0m',
- 'black' : '\033[30m',
- 'red' : '\033[31m',
- 'green' : '\033[32m',
- 'yellow' : '\033[33m',
- 'blue' : '\033[34m',
- 'purple' : '\033[35m',
- 'cyan' : '\033[36m',
- 'white' : '\033[37m'
-};
-
-
-function colorize(str, colorname) {
- return (colors[colorname] || colors['white']) + str + colors['clear']
-}
-
-function colorize_args() {
- var colorname = Array.prototype.shift.call(arguments);
- Array.prototype.unshift.call(arguments, colors[colorname] || colors['white']);
- Array.prototype.push.call(arguments, colors['clear']);
- return arguments;
-}
-
-module.exports = colorize;
-
-module.exports.colors = Object.keys(colors);
-module.exports.args = colorize_args;
-
-module.exports.define = function() {
- console.color = function() {
- console.log.apply(console, colorize_args.apply(console, arguments));
- };
-
- console.ecolor = function() {
- console.error.apply(console, colorize_args.apply(console, arguments));
- };
-
- Object.keys(colors).forEach(function(color) {
- console[color] = function() {
- Array.prototype.unshift.call(arguments, color);
- console.color.apply(console, arguments);
- };
- console['e' + color] = function(v) {
- Array.prototype.unshift.call(arguments, color);
- console.ecolor.apply(console, arguments);
- };
- colorize[color] = function(v) { return colorize(v, color) };
- });
-
- return colorize;
-};
-
-module.exports.prod = function() {
- console.ecolor = function() {};
- return this;
-};
-
-module.exports.define();
View
1  test/test.js
@@ -1,5 +1,6 @@
var ArrayStream = require('../ArrayStream');
if (typeof global != 'undefined') require('./load.test').load(global);
+require("termcolor").define;
var TOTAL = 1000;
Please sign in to comment.
Something went wrong with that request. Please try again.