Created
September 29, 2012 10:28
-
-
Save ishiduca/3803650 to your computer and use it in GitHub Desktop.
イケてない 行処理 #Node.js
This file contains hidden or 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
'use strict'; | |
function onData (chunk) { | |
var chunks = chunk.split(this.spliter); | |
var i = 0, last = chunks.length - 1; | |
for (; i < last; i++) { | |
this.rs.emit('line', chunks[i], this.lines++); | |
} | |
this.buf = chunks[last]; | |
} | |
function onEnd () { | |
this.buf && this.rs.emit('line', this.buf, this.lines); | |
this.buf = ''; | |
} | |
function createReadLine (readableStream, option) { | |
option = (typeof option === 'object' && option !== null) ? option : {}; | |
if (! readableStream) readableStream = process.stdin; | |
if (typeof readableStream === 'string') | |
readableStream = require('fs').createReadStream(readableStream, option); | |
if (! readableStream.readable) | |
throw Error('"readableStream" is not "readable"'); | |
if (! option.encoding) readableStream.setEncoding('utf8'); | |
var that = { | |
rs: readableStream | |
, buf: '' | |
, lines: 0 | |
, spliter: '\n' | |
}; | |
readableStream.on('data', onData.bind(that)); | |
readableStream.on('end', onEnd.bind(that)); | |
readableStream.on('close', function () { that = undefined; }); | |
return readableStream; | |
} | |
module.exports.createReadLine= createReadLine; | |
module.exports.eachline = function (file, onLine, option) { | |
var readStream = createReadLine(file, option); | |
readStream.on('line', onLine); | |
readStream.on('end', readStream.destroy.bind(readStream)); | |
return readStream; | |
}; |
This file contains hidden or 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
var createReadLine = require('./each-line').createReadLine; | |
var inputTxt = './video/list.txt'; | |
var outputTxt = './video/list.mp4.txt'; | |
var ws = require('fs').createWriteStream(outputTxt); | |
var rs = createLineStream(inputTxt); | |
rs.on('line', function (line, i) { | |
if (! /\.mp4/.test(line)) return; | |
(ws.write(line + '\n') === false) && rs.pause(); | |
}); | |
ws.on('drain', function () { | |
rs.resume(); | |
}); |
This file contains hidden or 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
var sample_text = __dirname + '/sample.txt'; | |
var createReadLine = require('./each-line').createReadLine; | |
var reader = createReadLine(sample_text); | |
reader.on('open', function () { | |
console.log('OPEN'); | |
}); | |
reader.on('line', function (line, i) { | |
console.log('%d: %s', i, line); | |
}); | |
reader.on('end', function () { | |
console.log('END>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); | |
}); |
This file contains hidden or 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
/* | |
* ショートカット的な | |
*/ | |
var sample_text = __dirname + '/sample.txt'; | |
var eachine = require('./each-line').eachline; | |
eachline(sample_text, function (line, i) { | |
console.log('%d: %s', i, line); | |
}); |
This file contains hidden or 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
var createReadLine = require('./each-line').createReadLine; | |
var http = require('http'); | |
var url = 'http://friendfeed.com/ishiduca'; | |
http.get( url, function (res) { | |
createReadLine(res); | |
res.on('line', function (line, i) { | |
console.log('%d: %s', i, line); | |
}); | |
}); |
This file contains hidden or 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
var util = require('util'); | |
var createReadLine = require('./each-line').createReadLine; | |
var rs = createReadLine(process.stdin); | |
rs.on('line', function (line) { | |
/^\.exit/.test(line) && process.exit(0); | |
util.log(line); | |
}); | |
rs.resume(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment