pdf-data-parser
Version:
Parse, search and stream PDF tabular data using Node.js with Mozilla's PDF.js library.
60 lines (51 loc) • 1.17 kB
JavaScript
/**
* lib/FormatCSV.js
*/
import { Transform } from 'stream';
/**
* Transforms row objects to CSV strings.
*/
export default class FormatCSV extends Transform {
constructor(options) {
let streamOptions = {
writableObjectMode: true,
readableObjectMode: false
};
super(streamOptions);
this.first = true;
}
/**
* Internal call from streamWriter to process an object
* @param {Object} row
* @param {String} encoding
* @param {Function} callback
*/
_transform(row, encoding, callback) {
if (this.first) {
// output headers
let text = "";
let sep = "";
for (let header of Object.keys(row)) {
text = text + sep + '"' + header + '"';
sep = ","
}
this.push(text + "\n");
this.first = false;
}
// output headers
let text = "";
let sep = "";
for (let value of Object.values(row)) {
let delim = '"';
text = text + sep + delim + value + delim;
sep = ","
}
this.push(text + "\n");
callback();
}
/*
_flush(callback) {
callback();
}
*/
};