ltsv

LTSV parser and formatter

ltsv.js

LTSV parser and formatter

http://sasaplus1.github.io/ltsv.js/

$ npm install ltsv
$ bower install ltsv
var 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' 
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"} 
  • text
    • String - LTSV text
  • return
    • Object[] - parsed objects

split to LTSV records.

throw SyntaxError if text has no separator.

  • line
    • String - LTSV line
  • return
    • Object - parsed object

split to LTSV record.

throw SyntaxError if line has no separator.

  • text
    • String - LTSV 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.

  • line
    • String - LTSV line
  • return
    • Object - parsed object

split to LTSV record.

throw SyntaxError if line has no separator. also throw SyntaxError if line has unexpected character.

  • data
    • Object|Object[] - object or object array
  • return
    • String - LTSV text

convert to LTSV text.

throw TypeError if data is not an object or array.

  • data
    • Object|Object[] - object or object array
  • return
    • String - LTSV text

convert to LTSV text.

throw TypeError if data is not an object or array. also throw SyntaxError if data has unexpected character.

  • options
    • Object - option object
  • return
    • LtsvToJsonStream - LTSV to JSON stream

return LtsvToJsonStream instance. this function cannot use by browser.

  • 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
$ npm install
$ npm test
$ npm install
$ npm run bower
$ npm run testem

The MIT license. Please see LICENSE file.