-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1a06695
Showing
14 changed files
with
684 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,120 @@ | |||
/** | |||
* ArrayStream -- ReadableStream of arrays or hash variables | |||
* @author SHIN Suzuki | |||
* @version 0.0.1 | |||
* | |||
*/ | |||
|
|||
var EventEmitter = require('events').EventEmitter, | |||
nextTick = process.nextTick; | |||
|
|||
/** | |||
* @constructor | |||
* | |||
* @param {Array} or {Object} arr | |||
* @param {Object} op | |||
* {boolean} tolerant if true, continue iteration even if errors occurred | |||
* | |||
* @event {data} function(value, key) | |||
* Emitted when received an element. | |||
* | |||
* @event {end} function() | |||
* Emitted when reached end | |||
* | |||
* @event {error} function(e) | |||
* Emitted when an error occurred | |||
**/ | |||
function ArrayStream(arr, op) { | |||
op = op || {}; | |||
this.i = 0; | |||
|
|||
this.readable = true; | |||
this.paused = !!op.pause; | |||
|
|||
this.arr = arr || []; | |||
this.isArray = Array.isArray(arr); | |||
this.keys = Object.keys(arr); | |||
this.length = this.keys.length; | |||
this._options = op; | |||
|
|||
if (!this.paused) nextTick(emit.bind(this)); | |||
} | |||
|
|||
|
|||
ArrayStream.prototype = new EventEmitter(); | |||
|
|||
/** | |||
* @see ReadableStream | |||
**/ | |||
ArrayStream.prototype.resume = function() { | |||
this.paused = false; | |||
emit.call(this); | |||
}; | |||
|
|||
/** | |||
* @see ReadableStream | |||
**/ | |||
ArrayStream.prototype.pause = function() { | |||
this.paused = true; | |||
}; | |||
|
|||
/** | |||
* @see ReadableStream | |||
**/ | |||
ArrayStream.prototype.destroy = function() { // implementing ReadableStream | |||
this.stream.destroy(); | |||
this.readable = false; | |||
} | |||
|
|||
|
|||
/** | |||
* @see ReadableStream | |||
**/ | |||
ArrayStream.prototype.destroySoon = function() { // implementing ReadableStream | |||
// Not knowing what to do, this remains unimplemented... | |||
this.destroy(); | |||
} | |||
|
|||
/** | |||
* @see ReadableStream | |||
**/ | |||
ArrayStream.prototype.pipe = function() { | |||
return process.stdin.pipe.apply(this, arguments); | |||
} | |||
|
|||
|
|||
/** | |||
* private function | |||
**/ | |||
function emit() { | |||
var self = this; | |||
(function execute() { | |||
try { | |||
var k = self.keys[self.i]; | |||
self.emit('data', self.arr[k], k); | |||
self.i++; | |||
|
|||
if(self.i >= self.length) { | |||
self.emit('end'); | |||
} | |||
else { | |||
if (!self.paused) nextTick(execute); | |||
} | |||
} catch (e) { | |||
self.emit('error', e); | |||
if (self._options.tolerant) { | |||
self.i++; | |||
if (!self.paused) nextTick(execute); | |||
} | |||
else { | |||
self.emit('end'); | |||
self.readable = false; | |||
} | |||
} | |||
})(); | |||
} | |||
|
|||
|
|||
ArrayStream.version = '0.0.1'; | |||
|
|||
module.exports = ArrayStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,22 @@ | |||
(The MIT License) | |||
|
|||
Copyright (c) 2011 SHIN Suzuki <shinout310@gmail.com> | |||
|
|||
Permission is hereby granted, free of charge, to any person obtaining | |||
a copy of this software and associated documentation files (the | |||
'Software'), to deal in the Software without restriction, including | |||
without limitation the rights to use, copy, modify, merge, publish, | |||
distribute, sublicense, and/or sell copies of the Software, and to | |||
permit persons to whom the Software is furnished to do so, subject to | |||
the following conditions: | |||
|
|||
The above copyright notice and this permission notice shall be | |||
included in all copies or substantial portions of the Software. | |||
|
|||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | |||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
ArrayStream.js 0.0.1 | |||
========== | |||
[Node.js] 配列,ハッシュをReadableStreamとして扱う | |||
|
|||
変更履歴 | |||
|
|||
---------------- | |||
* [0.0.1]: リリース | |||
|
|||
概要 | |||
---------------- | |||
### インストール方法 ### | |||
git clone git://github.com/shinout/ArrayStream.git | |||
|
|||
または | |||
|
|||
npm install arraystream | |||
|
|||
### 使い方 ### | |||
#### 配列を扱う #### | |||
var ArrayStream = require('arraystream'); | |||
var stream = new ArrayStream(['hoge', 'fuga', 'piyo']); | |||
|
|||
stream.on('data', function(value, key) { | |||
console.log(value); // hoge, fuga, piyo | |||
console.log(key); // 0, 1, 2 | |||
}); | |||
|
|||
stream.on('end', function() { // イテレーションの終わりに実行されます。 | |||
console.log('end'); | |||
}); | |||
|
|||
stream.on('error', function(e) { // エラー発生時に呼び出されます。 | |||
console.log(e); | |||
}); | |||
|
|||
|
|||
|
|||
#### オブジェクトを扱う #### | |||
var ArrayStream = require('arraystream'); | |||
var objstream = new ArrayStream({a:'hoge', b:'fuga', c:'piyo']); | |||
|
|||
objstream.on('data', function(value, key) { | |||
console.log(key); // a, b, c | |||
console.log(value); // hoge, fuga, piyo | |||
}); | |||
|
|||
objstream.on('end', function() { // Objectの終わりに実行されます。 | |||
console.log('end'); | |||
}); | |||
|
|||
objstream.on('error', function(e) { // エラー発生時に呼び出されます。 | |||
console.log(e); | |||
}); | |||
|
|||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,65 @@ | |||
Extra comma. at line 7. | |||
//------------------------------------// | |||
//-------------- start ---------------// | |||
1 ({ changeLogs : | |||
2 { "0.0.1" : "Release" | |||
3 } | |||
4 | |||
5 , changelog : 'Change Log' | |||
6 , _OR : 'OR' | |||
*7 , description : 'ReadableStream from array (or hash variable)', | |||
8 , install : 'Installation' | |||
9 , overview : 'Overview' | |||
10 , usage : 'Usage' | |||
11 , witharr : 'with Array' | |||
12 , withobj : 'with Object' | |||
13 , iteration : 'iteration' | |||
14 , endemit : function(v) { return 'emitted at the end of '+ v} | |||
15 , erremit : 'emitted when an error occurred' | |||
16 } | |||
17 ) | |||
//--------------- end ----------------// | |||
//------------------------------------// | |||
|
|||
SyntaxError: Unexpected token , | |||
at jsonize (/home/shinout/node_modules/umecob/umecob.js:339:19) | |||
at Object.sync (/home/shinout/node_modules/umecob/umecob.js:365:42) | |||
at Function.sync (/home/shinout/node_modules/umecob/umecob.js:237:90) | |||
at umecob (/home/shinout/node_modules/umecob/umecob.js:20:49) | |||
at Object.<anonymous> (/home/shinout/node_modules/arraystream/scripts/umecob-command.js:18:14) | |||
at Module._compile (module.js:432:26) | |||
at Object..js (module.js:450:10) | |||
at Module.load (module.js:351:31) | |||
at Function._load (module.js:310:12) | |||
at Array.0 (module.js:470:10) | |||
'description' is not defined. in /home/shinout/node_modules/arraystream/scripts/../scripts/README.tpl.md at line 3. | |||
//------------------------------------// | |||
//-------------- start ---------------// | |||
1 ArrayStream.js ${version} | |||
2 ========== | |||
*3 [Node.js] ${description} | |||
4 | |||
5 ${changelog} | |||
6 | |||
7 ---------------- | |||
8 << for (var i in changeLogs) { >> | |||
9 * [${i}]: ${changeLogs[i]} | |||
10 << } >> | |||
11 | |||
12 ${overview} | |||
13 ---------------- | |||
//--------------- end ----------------// | |||
//------------------------------------// | |||
|
|||
ReferenceError: description is not defined | |||
at Function.<anonymous> (eval at <anonymous> (/home/shinout/node_modules/umecob/umecob.js:548:50)) | |||
at Function.eval (/home/shinout/node_modules/umecob/umecob.js:548:45) | |||
at Object.run (/home/shinout/node_modules/umecob/umecob.js:590:21) | |||
at common_end (/home/shinout/node_modules/umecob/umecob.js:222:57) | |||
at Function.sync (/home/shinout/node_modules/umecob/umecob.js:238:16) | |||
at umecob (/home/shinout/node_modules/umecob/umecob.js:20:49) | |||
at Object.<anonymous> (/home/shinout/node_modules/arraystream/scripts/umecob-command.js:18:14) | |||
at Module._compile (module.js:432:26) | |||
at Object..js (module.js:450:10) | |||
at Module.load (module.js:351:31) | |||
'description' is not defined. in /home/shinout/node_modules/arraystream/scripts/../scripts/README.tpl.md at line 3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
{ | |||
"name": "arrayemitter" | |||
, "version": "0.0.1" | |||
, "description": "EventEmitter emittin array-iteration events." | |||
, "tags": ["EventEmitter", "array", "object", "iteration", "event"] | |||
, "author": "SHIN Suzuki <shinout310@gmail.com>" | |||
, "repository": | |||
{ "type": "git" | |||
, "url": "https://github.com/shinout/ArrayEmitter.git" | |||
} | |||
, "bugs": {"web": "https://github.com/shinout/ArrayEmitter/issues" } | |||
, "licences": | |||
[ { "type": "MIT" | |||
, "url": "https://github.com/shinout/ArrayEmitter/raw/master/LICENCE" | |||
} | |||
] | |||
, "main": "./ArrayEmitter.js" | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
ArrayStream.js ${version} | |||
========== | |||
[Node.js] ${description} | |||
|
|||
${changelog} | |||
|
|||
---------------- | |||
<< for (var i in changeLogs) { >> | |||
* [${i}]: ${changeLogs[i]} | |||
<< } >> | |||
|
|||
${overview} | |||
---------------- | |||
### ${install} ### | |||
git clone git://github.com/shinout/ArrayStream.git | |||
|
|||
${_OR} | |||
|
|||
npm install arraystream | |||
|
|||
### ${usage} ### | |||
#### ${witharr} #### | |||
var ArrayStream = require('arraystream'); | |||
var stream = new ArrayStream(['hoge', 'fuga', 'piyo']); | |||
|
|||
stream.on('data', function(value, key) { | |||
console.log(value); // hoge, fuga, piyo | |||
console.log(key); // 0, 1, 2 | |||
}); | |||
|
|||
stream.on('end', function() { // ${endemit(iteration)} | |||
console.log('end'); | |||
}); | |||
|
|||
stream.on('error', function(e) { // ${erremit} | |||
console.log(e); | |||
}); | |||
|
|||
|
|||
|
|||
#### ${withobj} #### | |||
var ArrayStream = require('arraystream'); | |||
var objstream = new ArrayStream({a:'hoge', b:'fuga', c:'piyo']); | |||
|
|||
objstream.on('data', function(value, key) { | |||
console.log(key); // a, b, c | |||
console.log(value); // hoge, fuga, piyo | |||
}); | |||
|
|||
objstream.on('end', function() { // ${endemit('Object')} | |||
console.log('end'); | |||
}); | |||
|
|||
objstream.on('error', function(e) { // ${erremit} | |||
console.log(e); | |||
}); | |||
|
|||
|
|||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
{ changeLogs : | |||
{ "0.0.1" : "Release" | |||
} | |||
|
|||
, changelog : 'Change Log' | |||
, _OR : 'OR' | |||
, description : 'ReadableStream from array (or hash variable)', | |||
, install : 'Installation' | |||
, overview : 'Overview' | |||
, usage : 'Usage' | |||
, witharr : 'with Array' | |||
, withobj : 'with Object' | |||
, iteration : 'iteration' | |||
, endemit : function(v) { return 'emitted at the end of '+ v} | |||
, erremit : 'emitted when an error occurred' | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
{ changeLogs : | |||
{ "0.0.1" : "リリース" | |||
} | |||
|
|||
, changelog : '変更履歴' | |||
, _OR : 'または' | |||
, description : '配列,ハッシュをReadableStreamとして扱う' | |||
, install : 'インストール方法' | |||
, overview : '概要' | |||
, usage : '使い方' | |||
, witharr : '配列を扱う' | |||
, withobj : 'オブジェクトを扱う' | |||
, iteration : 'イテレーション' | |||
, endemit : function(v) { return v + 'の終わりに実行されます。'} | |||
, erremit : 'エラー発生時に呼び出されます。' | |||
} |
Oops, something went wrong.