LTSV parser, formatter, validator and TransformStream
$ npm install ltsv
const ltsv = require('ltsv');
<script src="ltsv.min.js"></script>
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'
const fs = require('fs');
const ltsv = require('ltsv');
const stream = ltsv.createLtsvToJsonStream({
encoding: 'utf8',
objectMode: 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(stream).pipe(process.stdout);
// {"l1":"v1","l2":"v2"}{"l1":"v1","l2":"v2"}{"l1":"v1","l2":"v2"}
see docs or https://sasaplus1.github.io/ltsv.js
The MIT license.