@ezs/basics
Version:
Basics statements for EZS
62 lines (60 loc) • 1.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _csvString = _interopRequireDefault(require("csv-string"));
var _streamWrite = _interopRequireDefault(require("stream-write"));
var _string_decoder = require("string_decoder");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function CSVParse(data, feed) {
const separator = this.getParam('separator');
const quote = this.getParam('quote');
if (!this.decoder) {
this.decoder = new _string_decoder.StringDecoder('utf8');
this.input = _csvString.default.createStream({
separator,
quote
});
this.whenFinish = feed.flow(this.input);
}
if (this.isLast()) {
(0, _streamWrite.default)(this.input, this.decoder.end(), () => this.input.end());
this.whenFinish.finally(() => feed.close());
return;
}
(0, _streamWrite.default)(this.input, Buffer.isBuffer(data) ? this.decoder.write(data) : data, () => feed.end());
}
/**
* Take `String` and parse it as CSV to generate arrays.
*
* See:
*
* - {@link CSVObject}
* - {@link https://github.com/Inist-CNRS/node-csv-string}
*
* Input:
*
* ```json
* "a,b,c\nd,e,d\n"
* ```
*
* Output:
*
* ```json
* [
* ["a", "b", "c"],
* ["d", "e", "d"]
* ]
* ```
*
* > **Tip**: see CSVObject, to convert arrays of values to array of objects.
*
* @name CSVParse
* @param {String} [separator=auto] to indicate the CSV separator
* @param {String} [quote=auto] to indicate the CSV quote.
* @returns {Array<String[]>}
*/
var _default = exports.default = {
CSVParse
};