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.
Using npm:
$ npm install light-parser --save
Using yarn:
$ yarn add light-parser --save
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" }
// ]
These methods can be called on objects returned from new Parser()
.
Configure the options.
parser.setConfig(
{
rowDelimeter: [";","\n"],
columnDelimeter: [",","="],
removeFirstRow: true
}
);
Type: String
or Array
Default: ','
Specifies Column-Delemeter to use as the column separator for each row.
Type: String
or Array
Default: '\n'
Specifies Row-Delemeter to use as the row separator for input text.
Type: bool
Default: false
If true, skips the first row from input text.
Type: String
Default: false
If true, instructs the parser that the number of columns in each row must match the number of headers specified.
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.
Type: Array
Default: []
parser.setHeader(["No","Title","Contents","date"]);
Set default header with prefix. If prefix option is provided, header will configure [prefix value]0 to [prefix value]n-1.
Type: String
Default: ""
parser.setDefaultHeader("col");
return parsed data
Returns: Array[Object]
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" },
// ]