Skip to content

Commit

Permalink
v0.0.4: deprecated lines-adapter in favour of node-lazy
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Seymour <jon.seymour@gmail.com>
  • Loading branch information
jonseymour committed Apr 24, 2011
1 parent 5b996b3 commit f05e7f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 62 deletions.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ lines-adapter - an evented, line-oriented I/O library for node.js

SYNOPSIS
========

<pre>
var lines=require("lines-adapter");

Expand All @@ -22,14 +23,17 @@ SYNOPSIS
);
</pre>

Note that this lines-adapter is now implemnted in terms of and deprecated in favour of node-lazy.
Please read the ALTERNATIVES section below for details.

DESCRIPTION
===========
Adapts a readable byte stream to produce a stream of lines.

lines(stream,encoding)
----------------------
The lines-adapter module is a function which constructs a line stream from a byte stream and an optional
encoding argument. The supported encodings are those supported by Buffer.toString(). If no encoding is
encoding argument. The supported encodings are those supported by Buffer.toString(). If no encoding is
specified, 'utf8' is assumed.

Event: 'data'
Expand All @@ -43,8 +47,8 @@ Emitted when the stream has received an EOF. Indicates that no more 'data' event

INSTALLATION
============

npm install lines-adapter
git clone git://github.com/jonseymour/node-lines-adapter
npm link

EXAMPLES
========
Expand All @@ -66,8 +70,31 @@ Two examples are provided in the examples/ directory: count-lines and copy-lines
SIMILAR PACKAGES
================
Floby's [node-lines](https://github.com/Floby/node-lines) package provides similar functionality to this
library although it has a different philosophy. In particular node-lines emits 'line' events whereas
library although it has a different philosophy. In particular node-lines emits 'line' events whereas
node-lines-adapter emits 'data' events.

Peteris Krumin's node-lazy solution has a much better and more general solution to this problem.

For example:

var Lazy=require("lazy");

new Lazy(process.stdin)
.lines
.forEach(
function(line)
{
console.log(line);
}
);

process.stdin.resume();

REVISION HISTORY
================
v0.0.4
Deprecated this package in favour of node-lazy.
This is the last version of lines-adapter. Consumers are encouraged to use node-lazy directly instead.

v0.0.3
last version that does not generate deprecation warnings.
80 changes: 22 additions & 58 deletions lib/lines-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,35 @@
//

var
EventEmitter=require("events").EventEmitter;
Lazy=require("lazy"),
EventEmitter=require("events").EventEmitter;

function Lines(stream, encoding)
function Lines(stream)
{
var
self=this,
buffers = [],
total=0;

if (!encoding) {
encoding='utf8'; // perhaps this should be the platform default?
}

function onData(data) {
var i,k,min=0,dump,copied;
for (i=0; i<data.length; ++i) {
switch (data[i]) {
case 0x0a:
copied=0;
total+=(i-min);
dump=new Buffer(total);
for (k in buffers) {
buffers[k].copy(dump, copied, 0);
copied += buffers[k].length;
}
if (i>0) {
data.copy(dump, copied, min, i);
copied+=(i-min);
}
min=i+1;

// remove trailing \r if there is one
if (copied > 1 && dump[copied-2] == 0x0d) {
copied--;
}
self.emit('data', dump.toString(encoding, 0, copied));
buffers=[];
total=0;
break;

default:
break;
var self = this;

var lazy = new Lazy(stream)
.lines
.forEach(
function(line)
{
self.emit('data', line.toString());
}
}
if (min < data.length) {
buffers.push(data.slice(min));
total+=(data.length - min);
}
}

);

stream
.on('data', onData)
.on('end', function() {
if (buffers.length > 0) {
onData(new Buffer('\n'));
}
self.emit('end');
});
stream.on(
'end',
function () {
self.emit('end');
});

return this;
}
return this;
};

Lines.prototype = EventEmitter.prototype;
Lines.prototype = new EventEmitter();

module.exports=function(stream) {
console.warn("warn: as of v0.0.4 lines-adapter is deprecated - please use 'lazy' instead. see README.md");
console.warn("warn: If for some reason, you really want to continue using this package, use v0.0.3");
return new Lines(stream);
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"directories": {
"lib": "lib"
},
"dependencies": {
"lazy": "*"
},
"engines": {
"node": "*"
}
Expand Down

0 comments on commit f05e7f6

Please sign in to comment.