delimited-file-reader
v1.0.7
Published
Reads delimited text files and delivers records in object format
Downloads
3
Readme
A promise enabled, ES6 Class for reading delimited text files and emitting the records as objects.
##Synopsis
Read the given file, listen to the various events to get the data:
const DelimitedFileReader = require('delimited-file-reader');
let fileReader = new DelimitedFileReader(',');
fileReader.on('comment', (comment) => {
console.log(`comment: ${comment}`);
});
fileReader.on('data', (data) => {
console.log(`data: ${JSON.stringify(data)}`);
});
fileReader.on('close', () => {
console.log('Elvis has left the building!');
});
// The parse method takes a string filename or a read stream as an argument.
fileReader.parse('./simple-example.csv');
// comment: # Example file using comma delimited format with comments
// data: {"field_0":"value1-1","field_1":"value2-2"}
// data: {"field_0":"value2-1","field_1":"value2-2"}
// Elvis has left the building!
This code comes from the provided example file: simple-example.js
##Description
This class is a simple implementation of a delimited text file reader. The following are configurable.
In simpler terms:
- I have a delimited text file or stream with lf or lf\cr as record delimiters.
- The column delimiter can be expressed as a string or regular expresssion.
- They're may be comments in the file using a defined string in the starting position.
- I want to read the file and process each record as an object.
- I may want to name the properties of each object or I may let the class name them.
##Project Features
- ES6 Class
- Promise enabled
- Complete test coverage with Mocha and Chai
- JSDoc generated API documentation
- Rest parameters are used for maximum flexibility.
##Installation npm install delimited-file-reader --save
Git Repository: https://github.com/wildbillh/delimited-file-reader
##Documentation
There is JSDOC documentation provided: DelimitedFileReader.html
##Methods
###Constructor
constructor(delimiter = /[,]/, fields = [], commentChar = '#')
The constructor takes three defaulted parameters:
- delimiter - defaults to a common. Can be any valid string or regular expresssion.
- fields - defaults to an empty array. In this case, the class will generated field names. The user can also supply an array of strings.
- commentChar - The string to use to designate a comment line (if it occurs at position 0). Set this to null of the feature should be disabled.
###parse
parse(filename)
This method starts the process of reading the files and emitting events. Prior to calling this method, the desired event listeners should be instantiated.
##Events
- close - File reading is complete and all records have been emitted.
- comment - A comment was encountered. The entire line is returned.
- data - Contains an object representing the record.
- error - Contains the error message.
- invalid - A record was encountered with the wrong number of fields. This can only occur if the user specifies field names. The entire line is returned.