ltsv.js

LTSV parser and formatter
playgound
http://sasaplus1.github.io/ltsv.js/
Installation
npm
$ npm install ltsv
bower
$ bower install ltsv
Usage
node.js
var ltsv = require('ltsv');
browser
<script src="ltsv.min.js"></script>
Example
ltsv.parse(
'label1:value1\tlabel2:value2\n' +
'label1:value1\tlabel2:value2\n' +
'label1:value1\tlabel2:value2'
);
ltsv.parseLine('label1:value1\tlabel2:value2');
ltsv.parseLine('label1:value1\tlabel2:value2\n');
ltsv.parseLine('label1:value1\tlabel2:value2\r\n');
ltsv.format([
{ label1: 'value1', label2: 'value2' },
{ label1: 'value1', label2: 'value2' },
{ label1: 'value1', label2: 'value2' }
]);
ltsv.format({ label1: 'value1', label2: 'value2' });
var fs = require('fs'),
ltsv = require('ltsv'),
ltjs = ltsv.createLtsvToJsonStream({
toObject: false,
strict: false
});
fs.createReadStream('./access.log').pipe(ltjs).pipe(process.stdout);
Functions
parse(text)
text
return
Object[] - parsed objects
split to LTSV records.
throw SyntaxError if text has no separator.
parseLine(line)
split to LTSV record.
throw SyntaxError if line has no separator.
parseStrict(text)
text
return
Object[] - parsed objects
split to LTSV records and validate label and value of fields.
throw SyntaxError if text has no separator.
also throw SyntaxError if text has unexpected character.
parseLineStrict(line)
split to LTSV record.
throw SyntaxError if line has no separator.
also throw SyntaxError if line has unexpected character.
format(data)
data
Object|Object[] - object or object array
return
convert to LTSV text.
throw TypeError if data is not an object or array.
formatStrict(data)
data
Object|Object[] - object or object array
return
convert to LTSV text.
throw TypeError if data is not an object or array.
also throw SyntaxError if data has unexpected character.
createLtsvToJsonStream([options])
options
return
LtsvToJsonStream - LTSV to JSON stream
return LtsvToJsonStream instance. this function cannot use by browser.
options
encoding
String - StringDecoder's encoding, default is utf8
toObject
Boolean - convert to Object if true, default is false
strict
Boolean - strict parse if true, default is false
Test
node.js
$ npm install
$ npm test
browser
$ npm install
$ npm run bower
$ npm run testem
License
The MIT license. Please see LICENSE file.