Skip to content

LTSV parser, formatter, validator and TransformStream

License

Notifications You must be signed in to change notification settings

sasaplus1/ltsv.js

Repository files navigation

ltsv.js

test npm version Try ltsv on RunKit renovate

LTSV parser, formatter, validator and TransformStream

Installation

npm

$ npm install ltsv

Usage

node.js

const 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'
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"}

Functions

see docs or https://sasaplus1.github.io/ltsv.js

License

The MIT license.