ltsv.js
LTSV parser and formatter
playgound
http://sasaplus1.github.io/ltsv.js/
Installation
npm
$ npm install ltsvbower
$ bower install ltsvUsage
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'
);
// [ { label1: 'value1', label2: 'value2' },
// { label1: 'value1', label2: 'value2' },
// { label1: 'value1', label2: 'value2' } ]
ltsv.parseLine('label1:value1\tlabel2:value2');
// { label1: 'value1', label2: 'value2' }
ltsv.parseLine('label1:value1\tlabel2:value2\n');
// { label1: 'value1', label2: 'value2' }
ltsv.parseLine('label1:value1\tlabel2:value2\r\n');
// { label1: 'value1', label2: 'value2' }
ltsv.format([
{ label1: 'value1', label2: 'value2' },
{ label1: 'value1', label2: 'value2' },
{ label1: 'value1', label2: 'value2' }
]);
// 'label1:value1\tlabel2:value2\nlabel1:value1\tlabel2:value2\nlabel1:value1\tlabel2:value2'
ltsv.format({ label1: 'value1', label2: 'value2' });
// 'label1:value1\tlabel2:value2'var fs = require('fs'),
ltsv = require('ltsv'),
ltjs = ltsv.createLtsvToJsonStream({
toObject: false,
strict: false
});
// access.log:
// l1:v1\tl2:v2\n
// l1:v1\tl2:v2\n
// l1:v1\tl2:v2\n
fs.createReadStream('./access.log').pipe(ltjs).pipe(process.stdout);
// {"l1":"v1","l2":"v2"}{"l1":"v1","l2":"v2"}{"l1":"v1","l2":"v2"}Functions
parse(text)
textString- LTSV text
returnObject[]- parsed objects
split to LTSV records.
throw SyntaxError if text has no separator.
parseLine(line)
lineString- LTSV line
returnObject- parsed object
split to LTSV record.
throw SyntaxError if line has no separator.
parseStrict(text)
textString- LTSV text
returnObject[]- 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)
lineString- LTSV line
returnObject- parsed object
split to LTSV record.
throw SyntaxError if line has no separator.
also throw SyntaxError if line has unexpected character.
format(data)
dataObject|Object[]- object or object array
returnString- LTSV text
convert to LTSV text.
throw TypeError if data is not an object or array.
formatStrict(data)
dataObject|Object[]- object or object array
returnString- LTSV text
convert to LTSV text.
throw TypeError if data is not an object or array.
also throw SyntaxError if data has unexpected character.
createLtsvToJsonStream([options])
optionsObject- option object
returnLtsvToJsonStream- LTSV to JSON stream
return LtsvToJsonStream instance. this function cannot use by browser.
options
encodingString- StringDecoder's encoding, default isutf8
toObjectBoolean- convert to Object if true, default isfalse
strictBoolean- strict parse if true, default isfalse
Test
node.js
$ npm install
$ npm testbrowser
$ npm install
$ npm run bower
$ npm run testemLicense
The MIT license. Please see LICENSE file.