light-parser

1.0.3 • Public • Published

Light Parser

This is a light, simple and flexible text parser for Node.js. It's based string type as input and could configure to your needs.

Install

Using npm:

$ npm install light-parser --save

Using yarn:

$ yarn add light-parser --save

Usage

var Parser = require('light-parser')

var inputText = "A,B,C\n1,James,20\n2,Nick,24\n3,Kevin,39";

var p = new Parser();
p.setConfig({ rowDelimeter: "\n", columnDelimeter: "," , removeFirstRow:true});
p.setHeader(["No", "Name", "Age"]);
console.log(p.parse(inputText));
// Output : 
// [
// 	{ No: "1", Name: "James", Age: "20" },
// 	{ No: "2", Name: "Nick", Age: "24" },
// 	{ No: "3", Name: "Kevin", Age: "39" }
// ]

API

Instance Methods

These methods can be called on objects returned from new Parser().

parser.setConfig(options)

Configure the options.

parser.setConfig(
  { 
  rowDelimeter: [";","\n"],
  columnDelimeter: [",","="], 
  removeFirstRow: true
  }
  );

Options:

columnDelimeter

Type: String or Array
Default: ','

Specifies Column-Delemeter to use as the column separator for each row.

rowDelimeter

Type: String or Array
Default: '\n'

Specifies Row-Delemeter to use as the row separator for input text.

Any item(s) of columnDelimeter and rowDelimeter could not be overlapped.

removeFirstRow

Type: bool
Default: false

If true, skips the first row from input text.

strict

Type: String
Default: false

If true, instructs the parser that the number of columns in each row must match the number of headers specified.

parser.setHeader(header)

Specifies the headers. Headers define the property key for each value in the row. If no headers option is provided, parser will use the first line as the header.

header

Type: Array
Default: []

parser.setHeader(["No","Title","Contents","date"]);

parser.setDefaultHeader(prefix)

Set default header with prefix. If prefix option is provided, header will configure [prefix value]0 to [prefix value]n-1.

prefix

Type: String
Default: ""

parser.setDefaultHeader("col");

parser.parse(inputText)

return parsed data

Returns: Array[Object]

inputText

Type: String

parser.parse("A,B,C\n1,2,3\n4,5,6\n7,8,9");
// return
//    [
//			{ A: "1", B: "2",C:"3" },
//			{ A: "4", B: "5", C: "6" },
//			{ A: "7", B: "8", C: "9" },
//   ]

Package Sidebar

Install

npm i light-parser

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

15.2 kB

Total Files

6

Last publish

Collaborators

  • martinlim45