generate = function () {
var callback, data, generator, options;
if (arguments.length === 2) {
options = arguments[0];
callback = arguments[1];
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
options = {};
callback = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length === 0) {
options = {};
}
generator = new Generator(options);
if (callback) {
data = [];
generator.on('readable', function() {
var d, results;
results = [];
while (d = generator.read()) {
results.push(data.push(options.objectMode ? d : d.toString()));
}
return results;
});
generator.on('error', callback);
generator.on('end', function() {
return callback(null, options.objectMode ? data : data.join(''));
});
}
return generator;
}...
### Callback example
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
...generate.Generator = function (options1) {
var base, base1, base2, base3, base4, base5, base6, base7, base8, i, j, len, ref, v;
this.options = options1 != null ? options1 : {};
stream.Readable.call(this, this.options);
this.options.count = 0;
if ((base = this.options).duration == null) {
base.duration = 4 * 60 * 1000;
}
if ((base1 = this.options).columns == null) {
base1.columns = 8;
}
if ((base2 = this.options).max_word_length == null) {
base2.max_word_length = 16;
}
if ((base3 = this.options).fixed_size == null) {
base3.fixed_size = false;
}
if (this.fixed_size_buffer == null) {
this.fixed_size_buffer = '';
}
if ((base4 = this.options).start == null) {
base4.start = Date.now();
}
if ((base5 = this.options).end == null) {
base5.end = null;
}
if ((base6 = this.options).seed == null) {
base6.seed = false;
}
if ((base7 = this.options).length == null) {
base7.length = -1;
}
if ((base8 = this.options).delimiter == null) {
base8.delimiter = ',';
}
this.count_written = 0;
this.count_created = 0;
if (typeof this.options.columns === 'number') {
this.options.columns = new Array(this.options.columns);
}
ref = this.options.columns;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
if (v == null) {
v = 'ascii';
}
if (typeof v === 'string') {
this.options.columns[i] = Generator[v];
}
}
return this;
}n/a
parse = function () {
var callback, called, chunks, data, options, parser;
if (arguments.length === 3) {
data = arguments[0];
options = arguments[1];
callback = arguments[2];
if (typeof callback !== 'function') {
throw Error("Invalid callback argument: " + (JSON.stringify(callback)));
}
if (!(typeof data === 'string' || Buffer.isBuffer(arguments[0]))) {
return callback(Error("Invalid data argument: " + (JSON.stringify(data))));
}
} else if (arguments.length === 2) {
if (typeof arguments[0] === 'string' || Buffer.isBuffer(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
if (typeof arguments[1] === 'function') {
callback = arguments[1];
} else {
options = arguments[1];
}
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
} else {
options = arguments[0];
}
}
if (options == null) {
options = {};
}
parser = new Parser(options);
if (data != null) {
process.nextTick(function() {
parser.write(data);
return parser.end();
});
}
if (callback) {
called = false;
chunks = options.objname ? {} : [];
parser.on('readable', function() {
var chunk, results;
results = [];
while (chunk = parser.read()) {
if (options.objname) {
results.push(chunks[chunk[0]] = chunk[1]);
} else {
results.push(chunks.push(chunk));
}
}
return results;
});
parser.on('error', function(err) {
called = true;
return callback(err);
});
parser.on('end', function() {
if (!called) {
return callback(null, chunks);
}
});
}
return parser;
}...
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
...parse.Parser = function (options) {
var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9
, k, v;
if (options == null) {
options = {};
}
options.objectMode = true;
this.options = {};
for (k in options) {
v = options[k];
this.options[k] = v;
}
stream.Transform.call(this, this.options);
if ((base = this.options).rowDelimiter == null) {
base.rowDelimiter = null;
}
if (typeof this.options.rowDelimiter === 'string') {
this.options.rowDelimiter = [this.options.rowDelimiter];
}
if ((base1 = this.options).delimiter == null) {
base1.delimiter = ',';
}
if ((base2 = this.options).quote == null) {
base2.quote = '"';
}
if ((base3 = this.options).escape == null) {
base3.escape = '"';
}
if ((base4 = this.options).columns == null) {
base4.columns = null;
}
if ((base5 = this.options).comment == null) {
base5.comment = '';
}
if ((base6 = this.options).objname == null) {
base6.objname = false;
}
if ((base7 = this.options).trim == null) {
base7.trim = false;
}
if ((base8 = this.options).ltrim == null) {
base8.ltrim = false;
}
if ((base9 = this.options).rtrim == null) {
base9.rtrim = false;
}
if ((base10 = this.options).auto_parse == null) {
base10.auto_parse = false;
}
if ((base11 = this.options).auto_parse_date == null) {
base11.auto_parse_date = false;
}
if ((base12 = this.options).relax == null) {
base12.relax = false;
}
if ((base13 = this.options).relax_column_count == null) {
base13.relax_column_count = false;
}
if ((base14 = this.options).skip_empty_lines == null) {
base14.skip_empty_lines = false;
}
if ((base15 = this.options).max_limit_on_data_read == null) {
base15.max_limit_on_data_read = 128000;
}
if ((base16 = this.options).skip_lines_with_empty_values == null) {
base16.skip_lines_with_empty_values = false;
}
this.lines = 0;
this.count = 0;
this.skipped_line_count = 0;
this.empty_line_count = 0;
this.is_int = /^(\-|\+)?([1-9]+[0-9]*)$/;
this.is_float = function(value) {
return (value - parseFloat(value) + 1) >= 0;
};
this._ = {};
this._.decoder = new StringDecoder();
this._.quoting = false;
this._.commenting = false;
this._.field = null;
this._.nextChar = null;
this._.closingQuote = 0;
this._.line = [];
this._.chunks = [];
this._.rawBuf = '';
this._.buf = '';
if (this.options.rowDelimiter) {
this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) {
return v.length;
}));
}
return this;
}n/a
stringify = function () {
var callback, chunks, data, options, stringifier;
if (arguments.length === 3) {
data = arguments[0];
options = arguments[1];
callback = arguments[2];
} else if (arguments.length === 2) {
if (Array.isArray(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
if (typeof arguments[1] === 'function') {
callback = arguments[1];
} else {
options = arguments[1];
}
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
} else if (Array.isArray(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
}
if (options == null) {
options = {};
}
stringifier = new Stringifier(options);
if (data) {
process.nextTick(function() {
var d, j, len;
for (j = 0, len = data.length; j < len; j++) {
d = data[j];
stringifier.write(d);
}
return stringifier.end();
});
}
if (callback) {
chunks = [];
stringifier.on('readable', function() {
var chunk, results;
results = [];
while (chunk = stringifier.read()) {
results.push(chunks.push(chunk));
}
return results;
});
stringifier.on('error', function(err) {
return callback(err);
});
stringifier.on('end', function() {
return callback(null, chunks.join(''));
});
}
return stringifier;
}...
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
});
```
...stringify.Stringifier = function (opts) {
var base, base1, base10, base11, base12, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v;
if (opts == null) {
opts = {};
}
options = {};
for (k in opts) {
v = opts[k];
options[k] = v;
}
stream.Transform.call(this, options);
this.options = options;
if ((base = this.options).delimiter == null) {
base.delimiter = ',';
}
if ((base1 = this.options).quote == null) {
base1.quote = '"';
}
if ((base2 = this.options).quoted == null) {
base2.quoted = false;
}
if ((base3 = this.options).quotedString == null) {
base3.quotedString = false;
}
if ((base4 = this.options).eof == null) {
base4.eof = true;
}
if ((base5 = this.options).escape == null) {
base5.escape = '"';
}
if ((base6 = this.options).columns == null) {
base6.columns = null;
}
if ((base7 = this.options).header == null) {
base7.header = false;
}
if ((base8 = this.options).formatters == null) {
base8.formatters = {};
}
if ((base9 = this.options.formatters).date == null) {
base9.date = function(value) {
return '' + value.getTime();
};
}
if ((base10 = this.options.formatters).bool == null) {
base10.bool = function(value) {
if (value) {
return '1';
} else {
return '';
}
};
}
if ((base11 = this.options.formatters).object == null) {
base11.object = function(value) {
return JSON.stringify(value);
};
}
if ((base12 = this.options).rowDelimiter == null) {
base12.rowDelimiter = '\n';
}
if (this.countWriten == null) {
this.countWriten = 0;
}
switch (this.options.rowDelimiter) {
case 'auto':
this.options.rowDelimiter = null;
break;
case 'unix':
this.options.rowDelimiter = "\n";
break;
case 'mac':
this.options.rowDelimiter = "\r";
break;
case 'windows':
this.options.rowDelimiter = "\r\n";
break;
case 'unicode':
this.options.rowDelimiter = "\u2028";
}
return this;
}n/a
transform = function () {
var argument, callback, data, error, handler, i, j, k, len, options, result, transform, type, v;
options = {};
for (i = j = 0, len = arguments.length; j < len; i = ++j) {
argument = arguments[i];
type = typeof argument;
if (argument === null) {
type = 'null';
} else if (type === 'object' && Array.isArray(argument)) {
type = 'array';
}
if (i === 0) {
if (type === 'function') {
handler = argument;
} else if (type !== null) {
data = argument;
}
continue;
}
if (type === 'object') {
for (k in argument) {
v = argument[k];
options[k] = v;
}
} else if (type === 'function') {
if (handler && i === arguments.length - 1) {
callback = argument;
} else {
handler = argument;
}
} else if (type !== 'null') {
throw new Error('Invalid arguments');
}
}
transform = new Transformer(options, handler);
error = false;
if (data) {
process.nextTick(function() {
var len1, m, row;
for (m = 0, len1 = data.length; m < len1; m++) {
row = data[m];
if (error) {
break;
}
transform.write(row);
}
return transform.end();
});
}
if (callback || options.consume) {
result = [];
transform.on('readable', function() {
var r, results;
results = [];
while ((r = transform.read())) {
if (callback) {
results.push(result.push(r));
} else {
results.push(void 0);
}
}
return results;
});
transform.on('error', function(err) {
error = true;
if (callback) {
return callback(err);
}
});
transform.on('end', function() {
if (callback && !error) {
return callback(null, result);
}
});
}
return transform;
}...
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
...transform.Transformer = function (options1, transform1) {
var base;
this.options = options1 != null ? options1 : {};
this.transform = transform1;
this.options.objectMode = true;
if ((base = this.options).parallel == null) {
base.parallel = 100;
}
stream.Transform.call(this, this.options);
this.running = 0;
this.started = 0;
this.finished = 0;
return this;
}n/a
generate = function () {
var callback, data, generator, options;
if (arguments.length === 2) {
options = arguments[0];
callback = arguments[1];
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
options = {};
callback = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length === 0) {
options = {};
}
generator = new Generator(options);
if (callback) {
data = [];
generator.on('readable', function() {
var d, results;
results = [];
while (d = generator.read()) {
results.push(data.push(options.objectMode ? d : d.toString()));
}
return results;
});
generator.on('error', callback);
generator.on('end', function() {
return callback(null, options.objectMode ? data : data.join(''));
});
}
return generator;
}...
### Callback example
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
...Generator = function (options1) {
var base, base1, base2, base3, base4, base5, base6, base7, base8, i, j, len, ref, v;
this.options = options1 != null ? options1 : {};
stream.Readable.call(this, this.options);
this.options.count = 0;
if ((base = this.options).duration == null) {
base.duration = 4 * 60 * 1000;
}
if ((base1 = this.options).columns == null) {
base1.columns = 8;
}
if ((base2 = this.options).max_word_length == null) {
base2.max_word_length = 16;
}
if ((base3 = this.options).fixed_size == null) {
base3.fixed_size = false;
}
if (this.fixed_size_buffer == null) {
this.fixed_size_buffer = '';
}
if ((base4 = this.options).start == null) {
base4.start = Date.now();
}
if ((base5 = this.options).end == null) {
base5.end = null;
}
if ((base6 = this.options).seed == null) {
base6.seed = false;
}
if ((base7 = this.options).length == null) {
base7.length = -1;
}
if ((base8 = this.options).delimiter == null) {
base8.delimiter = ',';
}
this.count_written = 0;
this.count_created = 0;
if (typeof this.options.columns === 'number') {
this.options.columns = new Array(this.options.columns);
}
ref = this.options.columns;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
if (v == null) {
v = 'ascii';
}
if (typeof v === 'string') {
this.options.columns[i] = Generator[v];
}
}
return this;
}n/a
Generator = function (options1) {
var base, base1, base2, base3, base4, base5, base6, base7, base8, i, j, len, ref, v;
this.options = options1 != null ? options1 : {};
stream.Readable.call(this, this.options);
this.options.count = 0;
if ((base = this.options).duration == null) {
base.duration = 4 * 60 * 1000;
}
if ((base1 = this.options).columns == null) {
base1.columns = 8;
}
if ((base2 = this.options).max_word_length == null) {
base2.max_word_length = 16;
}
if ((base3 = this.options).fixed_size == null) {
base3.fixed_size = false;
}
if (this.fixed_size_buffer == null) {
this.fixed_size_buffer = '';
}
if ((base4 = this.options).start == null) {
base4.start = Date.now();
}
if ((base5 = this.options).end == null) {
base5.end = null;
}
if ((base6 = this.options).seed == null) {
base6.seed = false;
}
if ((base7 = this.options).length == null) {
base7.length = -1;
}
if ((base8 = this.options).delimiter == null) {
base8.delimiter = ',';
}
this.count_written = 0;
this.count_created = 0;
if (typeof this.options.columns === 'number') {
this.options.columns = new Array(this.options.columns);
}
ref = this.options.columns;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
if (v == null) {
v = 'ascii';
}
if (typeof v === 'string') {
this.options.columns[i] = Generator[v];
}
}
return this;
}n/a
ascii = function (gen) {
var char, column, j, nb_chars, ref;
column = [];
for (nb_chars = j = 0, ref = Math.ceil(gen.random() * gen.options.max_word_length); 0 <= ref ? j < ref : j > ref; nb_chars = 0
<= ref ? ++j : --j) {
char = Math.floor(gen.random() * 32);
column.push(String.fromCharCode(char + (char < 16 ? 65 : 97 - 16)));
}
return column.join('');
}n/a
bool = function (gen) {
return Math.floor(gen.random() * 2);
}n/a
int = function (gen) {
return Math.floor(gen.random() * Math.pow(2, 52));
}n/a
function Readable(options) {
if (!(this instanceof Readable))
return new Readable(options);
this._readableState = new ReadableState(options, this);
// legacy
this.readable = true;
if (options && typeof options.read === 'function')
this._read = options.read;
Stream.call(this);
}n/a
_read = function (size) {
var column, data, header, j, k, l, len, len1, len2, len3, length, line, lineLength, m, ref;
data = [];
length = this.fixed_size_buffer.length;
if (length) {
data.push(this.fixed_size_buffer);
}
while (true) {
if ((this.count_created === this.options.length) || (this.options.end && Date.now() > this.options.end)) {
if (data.length) {
if (this.options.objectMode) {
for (j = 0, len = data.length; j < len; j++) {
line = data[j];
this.count_written++;
this.push(line);
}
} else {
this.count_written++;
this.push(data.join(''));
}
}
return this.push(null);
}
line = [];
ref = this.options.columns;
for (k = 0, len1 = ref.length; k < len1; k++) {
header = ref[k];
line.push("" + (header(this)));
}
if (this.options.objectMode) {
lineLength = 0;
for (l = 0, len2 = line.length; l < len2; l++) {
column = line[l];
lineLength += column.length;
}
} else {
line = "" + (this.count_created === 0 ? '' : '\n') + (line.join(this.options.delimiter));
lineLength = line.length;
}
this.count_created++;
if (length + lineLength > size) {
if (this.options.objectMode) {
data.push(line);
for (m = 0, len3 = data.length; m < len3; m++) {
line = data[m];
this.count_written++;
this.push(line);
}
} else {
if (this.options.fixed_size) {
this.fixed_size_buffer = line.substr(size - length);
data.push(line.substr(0, size - length));
} else {
data.push(line);
}
this.count_written++;
this.push(data.join(''));
}
break;
}
length += lineLength;
data.push(line);
}
}n/a
end = function () {
return this.push(null);
}...
generator.on('readable', function(){
while(data = generator.read()){
parser.write(data);
}
});
generator.on('end', function(){
parser.end()
});
parser.on('readable', function(){
while(data = parser.read()){
transformer.write(data);
}
});
...random = function () {
if (this.options.seed) {
return this.options.seed = this.options.seed * Math.PI * 100 % 100 / 100;
} else {
return Math.random();
}
}n/a
parse = function () {
var callback, called, chunks, data, options, parser;
if (arguments.length === 3) {
data = arguments[0];
options = arguments[1];
callback = arguments[2];
if (typeof callback !== 'function') {
throw Error("Invalid callback argument: " + (JSON.stringify(callback)));
}
if (!(typeof data === 'string' || Buffer.isBuffer(arguments[0]))) {
return callback(Error("Invalid data argument: " + (JSON.stringify(data))));
}
} else if (arguments.length === 2) {
if (typeof arguments[0] === 'string' || Buffer.isBuffer(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
if (typeof arguments[1] === 'function') {
callback = arguments[1];
} else {
options = arguments[1];
}
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
} else {
options = arguments[0];
}
}
if (options == null) {
options = {};
}
parser = new Parser(options);
if (data != null) {
process.nextTick(function() {
parser.write(data);
return parser.end();
});
}
if (callback) {
called = false;
chunks = options.objname ? {} : [];
parser.on('readable', function() {
var chunk, results;
results = [];
while (chunk = parser.read()) {
if (options.objname) {
results.push(chunks[chunk[0]] = chunk[1]);
} else {
results.push(chunks.push(chunk));
}
}
return results;
});
parser.on('error', function(err) {
called = true;
return callback(err);
});
parser.on('end', function() {
if (!called) {
return callback(null, chunks);
}
});
}
return parser;
}...
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
...Parser = function (options) {
var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9
, k, v;
if (options == null) {
options = {};
}
options.objectMode = true;
this.options = {};
for (k in options) {
v = options[k];
this.options[k] = v;
}
stream.Transform.call(this, this.options);
if ((base = this.options).rowDelimiter == null) {
base.rowDelimiter = null;
}
if (typeof this.options.rowDelimiter === 'string') {
this.options.rowDelimiter = [this.options.rowDelimiter];
}
if ((base1 = this.options).delimiter == null) {
base1.delimiter = ',';
}
if ((base2 = this.options).quote == null) {
base2.quote = '"';
}
if ((base3 = this.options).escape == null) {
base3.escape = '"';
}
if ((base4 = this.options).columns == null) {
base4.columns = null;
}
if ((base5 = this.options).comment == null) {
base5.comment = '';
}
if ((base6 = this.options).objname == null) {
base6.objname = false;
}
if ((base7 = this.options).trim == null) {
base7.trim = false;
}
if ((base8 = this.options).ltrim == null) {
base8.ltrim = false;
}
if ((base9 = this.options).rtrim == null) {
base9.rtrim = false;
}
if ((base10 = this.options).auto_parse == null) {
base10.auto_parse = false;
}
if ((base11 = this.options).auto_parse_date == null) {
base11.auto_parse_date = false;
}
if ((base12 = this.options).relax == null) {
base12.relax = false;
}
if ((base13 = this.options).relax_column_count == null) {
base13.relax_column_count = false;
}
if ((base14 = this.options).skip_empty_lines == null) {
base14.skip_empty_lines = false;
}
if ((base15 = this.options).max_limit_on_data_read == null) {
base15.max_limit_on_data_read = 128000;
}
if ((base16 = this.options).skip_lines_with_empty_values == null) {
base16.skip_lines_with_empty_values = false;
}
this.lines = 0;
this.count = 0;
this.skipped_line_count = 0;
this.empty_line_count = 0;
this.is_int = /^(\-|\+)?([1-9]+[0-9]*)$/;
this.is_float = function(value) {
return (value - parseFloat(value) + 1) >= 0;
};
this._ = {};
this._.decoder = new StringDecoder();
this._.quoting = false;
this._.commenting = false;
this._.field = null;
this._.nextChar = null;
this._.closingQuote = 0;
this._.line = [];
this._.chunks = [];
this._.rawBuf = '';
this._.buf = '';
if (this.options.rowDelimiter) {
this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) {
return v.length;
}));
}
return this;
}n/a
Parser = function (options) {
var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9
, k, v;
if (options == null) {
options = {};
}
options.objectMode = true;
this.options = {};
for (k in options) {
v = options[k];
this.options[k] = v;
}
stream.Transform.call(this, this.options);
if ((base = this.options).rowDelimiter == null) {
base.rowDelimiter = null;
}
if (typeof this.options.rowDelimiter === 'string') {
this.options.rowDelimiter = [this.options.rowDelimiter];
}
if ((base1 = this.options).delimiter == null) {
base1.delimiter = ',';
}
if ((base2 = this.options).quote == null) {
base2.quote = '"';
}
if ((base3 = this.options).escape == null) {
base3.escape = '"';
}
if ((base4 = this.options).columns == null) {
base4.columns = null;
}
if ((base5 = this.options).comment == null) {
base5.comment = '';
}
if ((base6 = this.options).objname == null) {
base6.objname = false;
}
if ((base7 = this.options).trim == null) {
base7.trim = false;
}
if ((base8 = this.options).ltrim == null) {
base8.ltrim = false;
}
if ((base9 = this.options).rtrim == null) {
base9.rtrim = false;
}
if ((base10 = this.options).auto_parse == null) {
base10.auto_parse = false;
}
if ((base11 = this.options).auto_parse_date == null) {
base11.auto_parse_date = false;
}
if ((base12 = this.options).relax == null) {
base12.relax = false;
}
if ((base13 = this.options).relax_column_count == null) {
base13.relax_column_count = false;
}
if ((base14 = this.options).skip_empty_lines == null) {
base14.skip_empty_lines = false;
}
if ((base15 = this.options).max_limit_on_data_read == null) {
base15.max_limit_on_data_read = 128000;
}
if ((base16 = this.options).skip_lines_with_empty_values == null) {
base16.skip_lines_with_empty_values = false;
}
this.lines = 0;
this.count = 0;
this.skipped_line_count = 0;
this.empty_line_count = 0;
this.is_int = /^(\-|\+)?([1-9]+[0-9]*)$/;
this.is_float = function(value) {
return (value - parseFloat(value) + 1) >= 0;
};
this._ = {};
this._.decoder = new StringDecoder();
this._.quoting = false;
this._.commenting = false;
this._.field = null;
this._.nextChar = null;
this._.closingQuote = 0;
this._.line = [];
this._.chunks = [];
this._.rawBuf = '';
this._.buf = '';
if (this.options.rowDelimiter) {
this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) {
return v.length;
}));
}
return this;
}n/a
function Transform(options) {
if (!(this instanceof Transform))
return new Transform(options);
Duplex.call(this, options);
this._transformState = new TransformState(this);
var stream = this;
// start out asking for a readable event once data is transformed.
this._readableState.needReadable = true;
// we have implemented the _read method, and done the other things
// that Readable wants before the first _read call, so unset the
// sync guard flag.
this._readableState.sync = false;
if (options) {
if (typeof options.transform === 'function')
this._transform = options.transform;
if (typeof options.flush === 'function')
this._flush = options.flush;
}
// When the writable side finishes, then flush out anything remaining.
this.once('prefinish', function() {
if (typeof this._flush === 'function')
this._flush(function(er) {
done(stream, er);
});
else
done(stream);
});
}n/a
__push = function (line) {
var field, i, j, len, lineAsColumns, rawBuf, row;
if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') {
return;
}
row = null;
if (this.options.columns === true) {
this.options.columns = line;
rawBuf = '';
return;
} else if (typeof this.options.columns === 'function') {
this.options.columns = this.options.columns(line);
rawBuf = '';
return;
}
if (!this._.line_length && line.length > 0) {
this._.line_length = this.options.columns ? this.options.columns.length : line.length;
}
if (line.length === 1 && line[0] === '') {
this.empty_line_count++;
} else if (line.length !== this._.line_length) {
if (this.options.relax_column_count) {
this.skipped_line_count++;
} else if (this.options.columns != null) {
throw Error("Number of columns on line " + this.lines + " does not match header");
} else {
throw Error("Number of columns is inconsistent on line " + this.lines);
}
} else {
this.count++;
}
if (this.options.columns != null) {
lineAsColumns = {};
for (i = j = 0, len = line.length; j < len; i = ++j) {
field = line[i];
if (this.options.columns[i] === false) {
continue;
}
lineAsColumns[this.options.columns[i]] = field;
}
if (this.options.objname) {
row = [lineAsColumns[this.options.objname], lineAsColumns];
} else {
row = lineAsColumns;
}
} else {
row = line;
}
if (this.count < this.options.from) {
return;
}
if (this.count > this.options.to) {
return;
}
if (this.options.raw) {
this.push({
raw: this._.rawBuf,
row: row
});
return this._.rawBuf = '';
} else {
return this.push(row);
}
}n/a
__write = function (chars, end) {
var areNextCharsDelimiter, areNextCharsRowDelimiters, auto_parse, char, escapeIsQuote, i, isDelimiter, isEscape, isNextCharAComment
, isQuote, isRowDelimiter, isRowDelimiterLength, is_float, is_int, l, ltrim, nextCharPos, ref, ref1, ref2, ref3, ref4, remainingBuffer
, results, rowDelimiter, rtrim, wasCommenting;
is_int = (function(_this) {
return function(value) {
if (typeof _this.is_int === 'function') {
return _this.is_int(value);
} else {
return _this.is_int.test(value);
}
};
})(this);
is_float = (function(_this) {
return function(value) {
if (typeof _this.is_float === 'function') {
return _this.is_float(value);
} else {
return _this.is_float.test(value);
}
};
})(this);
auto_parse = (function(_this) {
return function(value) {
var m;
if (!_this.options.auto_parse) {
return value;
}
if (is_int(value)) {
value = parseInt(value);
} else if (is_float(value)) {
value = parseFloat(value);
} else if (_this.options.auto_parse_date) {
m = Date.parse(value);
if (!isNaN(m)) {
value = new Date(m);
}
}
return value;
};
})(this);
ltrim = this.options.trim || this.options.ltrim;
rtrim = this.options.trim || this.options.rtrim;
chars = this._.buf + chars;
l = chars.length;
i = 0;
if (this.lines === 0 && 0xFEFF === chars.charCodeAt(0)) {
i++;
}
while (i < l) {
if (!end) {
remainingBuffer = chars.substr(i, l - i);
if ((!this.options.rowDelimiter && i + 3 > l) || (!this._.commenting && l - i < this.options.comment.length && this.options
.comment.substr(0, l - i) === remainingBuffer) || (this.options.rowDelimiter && l - i < this._.rowDelimiterLength && this.options
.rowDelimiter.some(function(rd) {
return rd.substr(0, l - i) === remainingBuffer;
})) || (this.options.rowDelimiter && this._.quoting && l - i < (this.options.quote.length + this._.rowDelimiterLength) &&
this.options.rowDelimiter.some((function(_this) {
return function(rd) {
return (_this.options.quote + rd).substr(0, l - i) === remainingBuffer;
};
})(this))) || (l - i <= this.options.delimiter.length && this.options.delimiter.substr(0, l - i) === remainingBuffer) || (
l - i <= this.options.escape.length && this.options.escape.substr(0, l - i) === remainingBuffer)) {
break;
}
}
char = this._.nextChar ? this._.nextChar : chars.charAt(i);
this._.nextChar = l > i + 1 ? chars.charAt(i + 1) : '';
if (this.options.raw) {
this._.rawBuf += char;
}
if (this.options.rowDelimiter == null) {
nextCharPos = i;
rowDelimiter = null;
if (!this._.quoting && (char === '\n' || char === '\r')) {
rowDelimiter = char;
nextCharPos += 1;
} else if (!(!this._.quoting && char === this.options.quote) && (this._.nextChar === '\n' || this._.nextChar === '\r')) {
rowDelimiter = this._.nextChar;
nextCharPos += 2;
if (this.raw) {
rawBuf += this._.nextChar;
}
}
if (rowDelimiter) {
if (rowDelimiter === '\r' && chars.charAt(nextCharPos) === '\n') {
rowDelimiter += '\n';
}
this.options.rowDelimiter = [rowDelimiter];
this._.rowDelimiterLength = rowDelimiter.length;
}
}
if (!this._.commenting && char === this.options.escape) {
escapeIsQuote = this.options.escape === this.options.quote;
isEscape = this._.nextChar === this.options.escape;
isQuote = this._.nextChar === this.options.quote;
if (!(escapeIsQuote && (this._.field == null) && !this._.quoting) && (isEscape || isQuote)) {
i++;
char = this._.nextChar;
this._.nextChar = chars.charAt(i + 1);
if (this._.field == null) {
this._.field = '';
}
this._.field += char;
if (this.options.raw) {
this._.rawBuf += char;
}
i++;
continue;
}
} ...n/a
_flush = function (callback) {
var err, error;
try {
this.__write(this._.decoder.end(), true);
if (this._.quoting) {
this.emit('error', new Error("Quoted field not terminated at line " + (this.lines + 1)));
return;
}
if (this._.line.length > 0) {
this.__push(this._.line);
}
return callback();
} catch (error) {
err = error;
return this.emit('error', err);
}
}n/a
_transform = function (chunk, encoding, callback) {
var err, error;
if (chunk instanceof Buffer) {
chunk = this._.decoder.write(chunk);
}
try {
this.__write(chunk, false);
return callback();
} catch (error) {
err = error;
return this.emit('error', err);
}
}n/a
stringify = function () {
var callback, chunks, data, options, stringifier;
if (arguments.length === 3) {
data = arguments[0];
options = arguments[1];
callback = arguments[2];
} else if (arguments.length === 2) {
if (Array.isArray(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
if (typeof arguments[1] === 'function') {
callback = arguments[1];
} else {
options = arguments[1];
}
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
} else if (Array.isArray(arguments[0])) {
data = arguments[0];
} else {
options = arguments[0];
}
}
if (options == null) {
options = {};
}
stringifier = new Stringifier(options);
if (data) {
process.nextTick(function() {
var d, j, len;
for (j = 0, len = data.length; j < len; j++) {
d = data[j];
stringifier.write(d);
}
return stringifier.end();
});
}
if (callback) {
chunks = [];
stringifier.on('readable', function() {
var chunk, results;
results = [];
while (chunk = stringifier.read()) {
results.push(chunks.push(chunk));
}
return results;
});
stringifier.on('error', function(err) {
return callback(err);
});
stringifier.on('end', function() {
return callback(null, chunks.join(''));
});
}
return stringifier;
}...
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
});
```
...Stringifier = function (opts) {
var base, base1, base10, base11, base12, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v;
if (opts == null) {
opts = {};
}
options = {};
for (k in opts) {
v = opts[k];
options[k] = v;
}
stream.Transform.call(this, options);
this.options = options;
if ((base = this.options).delimiter == null) {
base.delimiter = ',';
}
if ((base1 = this.options).quote == null) {
base1.quote = '"';
}
if ((base2 = this.options).quoted == null) {
base2.quoted = false;
}
if ((base3 = this.options).quotedString == null) {
base3.quotedString = false;
}
if ((base4 = this.options).eof == null) {
base4.eof = true;
}
if ((base5 = this.options).escape == null) {
base5.escape = '"';
}
if ((base6 = this.options).columns == null) {
base6.columns = null;
}
if ((base7 = this.options).header == null) {
base7.header = false;
}
if ((base8 = this.options).formatters == null) {
base8.formatters = {};
}
if ((base9 = this.options.formatters).date == null) {
base9.date = function(value) {
return '' + value.getTime();
};
}
if ((base10 = this.options.formatters).bool == null) {
base10.bool = function(value) {
if (value) {
return '1';
} else {
return '';
}
};
}
if ((base11 = this.options.formatters).object == null) {
base11.object = function(value) {
return JSON.stringify(value);
};
}
if ((base12 = this.options).rowDelimiter == null) {
base12.rowDelimiter = '\n';
}
if (this.countWriten == null) {
this.countWriten = 0;
}
switch (this.options.rowDelimiter) {
case 'auto':
this.options.rowDelimiter = null;
break;
case 'unix':
this.options.rowDelimiter = "\n";
break;
case 'mac':
this.options.rowDelimiter = "\r";
break;
case 'windows':
this.options.rowDelimiter = "\r\n";
break;
case 'unicode':
this.options.rowDelimiter = "\u2028";
}
return this;
}n/a
Stringifier = function (opts) {
var base, base1, base10, base11, base12, base2, base3, base4, base5, base6, base7, base8, base9, k, options, v;
if (opts == null) {
opts = {};
}
options = {};
for (k in opts) {
v = opts[k];
options[k] = v;
}
stream.Transform.call(this, options);
this.options = options;
if ((base = this.options).delimiter == null) {
base.delimiter = ',';
}
if ((base1 = this.options).quote == null) {
base1.quote = '"';
}
if ((base2 = this.options).quoted == null) {
base2.quoted = false;
}
if ((base3 = this.options).quotedString == null) {
base3.quotedString = false;
}
if ((base4 = this.options).eof == null) {
base4.eof = true;
}
if ((base5 = this.options).escape == null) {
base5.escape = '"';
}
if ((base6 = this.options).columns == null) {
base6.columns = null;
}
if ((base7 = this.options).header == null) {
base7.header = false;
}
if ((base8 = this.options).formatters == null) {
base8.formatters = {};
}
if ((base9 = this.options.formatters).date == null) {
base9.date = function(value) {
return '' + value.getTime();
};
}
if ((base10 = this.options.formatters).bool == null) {
base10.bool = function(value) {
if (value) {
return '1';
} else {
return '';
}
};
}
if ((base11 = this.options.formatters).object == null) {
base11.object = function(value) {
return JSON.stringify(value);
};
}
if ((base12 = this.options).rowDelimiter == null) {
base12.rowDelimiter = '\n';
}
if (this.countWriten == null) {
this.countWriten = 0;
}
switch (this.options.rowDelimiter) {
case 'auto':
this.options.rowDelimiter = null;
break;
case 'unix':
this.options.rowDelimiter = "\n";
break;
case 'mac':
this.options.rowDelimiter = "\r";
break;
case 'windows':
this.options.rowDelimiter = "\r\n";
break;
case 'unicode':
this.options.rowDelimiter = "\u2028";
}
return this;
}n/a
function Transform(options) {
if (!(this instanceof Transform))
return new Transform(options);
Duplex.call(this, options);
this._transformState = new TransformState(this);
var stream = this;
// start out asking for a readable event once data is transformed.
this._readableState.needReadable = true;
// we have implemented the _read method, and done the other things
// that Readable wants before the first _read call, so unset the
// sync guard flag.
this._readableState.sync = false;
if (options) {
if (typeof options.transform === 'function')
this._transform = options.transform;
if (typeof options.flush === 'function')
this._flush = options.flush;
}
// When the writable side finishes, then flush out anything remaining.
this.once('prefinish', function() {
if (typeof this._flush === 'function')
this._flush(function(er) {
done(stream, er);
});
else
done(stream);
});
}n/a
_transform = function (chunk, encoding, callback) {
this.push(chunk);
return callback();
}n/a
end = function (chunk, encoding, callback) {
if (this.countWriten === 0) {
this.headers();
}
return stream.Transform.prototype.end.apply(this, arguments);
}...
generator.on('readable', function(){
while(data = generator.read()){
parser.write(data);
}
});
generator.on('end', function(){
parser.end()
});
parser.on('readable', function(){
while(data = parser.read()){
transformer.write(data);
}
});
...headers = function () {
var k, label, labels;
if (!this.options.header) {
return;
}
if (!this.options.columns) {
return;
}
labels = this.options.columns;
if (typeof labels === 'object') {
labels = (function() {
var results;
results = [];
for (k in labels) {
label = labels[k];
results.push(label);
}
return results;
})();
}
if (this.options.eof) {
labels = this.stringify(labels) + this.options.rowDelimiter;
} else {
labels = this.stringify(labels);
}
return stream.Transform.prototype.write.call(this, labels);
}n/a
stringify = function (line) {
var _line, column, columns, containsEscape, containsLinebreak, containsQuote, containsdelimiter, delimiter, escape, field, i,
j, l, newLine, quote, ref, ref1, regexp, shouldQuote, value;
if (typeof line !== 'object') {
return line;
}
columns = this.options.columns;
if (typeof columns === 'object' && columns !== null && !Array.isArray(columns)) {
columns = Object.keys(columns);
}
delimiter = this.options.delimiter;
quote = this.options.quote;
escape = this.options.escape;
if (!Array.isArray(line)) {
_line = [];
if (columns) {
for (i = j = 0, ref = columns.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
column = columns[i];
value = get(line, column);
_line[i] = typeof value === 'undefined' || value === null ? '' : value;
}
} else {
for (column in line) {
_line.push(line[column]);
}
}
line = _line;
_line = null;
} else if (columns) {
line.splice(columns.length);
}
if (Array.isArray(line)) {
newLine = '';
for (i = l = 0, ref1 = line.length; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
field = line[i];
if (typeof field === 'string') {
} else if (typeof field === 'number') {
field = '' + field;
} else if (typeof field === 'boolean') {
field = this.options.formatters.bool(field);
} else if (field instanceof Date) {
field = this.options.formatters.date(field);
} else if (typeof field === 'object' && field !== null) {
field = this.options.formatters.object(field);
}
if (field) {
containsdelimiter = field.indexOf(delimiter) >= 0;
containsQuote = field.indexOf(quote) >= 0;
containsEscape = field.indexOf(escape) >= 0 && (escape !== quote);
containsLinebreak = field.indexOf('\r') >= 0 || field.indexOf('\n') >= 0;
shouldQuote = containsQuote || containsdelimiter || containsLinebreak || this.options.quoted || (this.options.quotedString
&& typeof line[i] === 'string');
if (shouldQuote && containsEscape) {
regexp = escape === '\\' ? new RegExp(escape + escape, 'g') : new RegExp(escape, 'g');
field = field.replace(regexp, escape + escape);
}
if (containsQuote) {
regexp = new RegExp(quote, 'g');
field = field.replace(regexp, escape + quote);
}
if (shouldQuote) {
field = quote + field + quote;
}
newLine += field;
} else if (this.options.quotedEmpty || ((this.options.quotedEmpty == null) && line[i] === '' && this.options.quotedString)) {
newLine += quote + quote;
}
if (i !== line.length - 1) {
newLine += delimiter;
}
}
line = newLine;
}
return line;
}...
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
});
```
...write = function (chunk, encoding, callback) {
var base, e, error, preserve;
if (chunk == null) {
return;
}
preserve = typeof chunk !== 'object';
if (!preserve) {
if (this.countWriten === 0 && !Array.isArray(chunk)) {
if ((base = this.options).columns == null) {
base.columns = Object.keys(chunk);
}
}
try {
this.emit('record', chunk, this.countWriten);
} catch (error) {
e = error;
return this.emit('error', e);
}
if (this.options.eof) {
chunk = this.stringify(chunk) + this.options.rowDelimiter;
} else {
chunk = this.stringify(chunk);
if (this.options.header || this.countWriten) {
chunk = this.options.rowDelimiter + chunk;
}
}
}
if (typeof chunk === 'number') {
chunk = "" + chunk;
}
if (this.countWriten === 0) {
this.headers();
}
if (!preserve) {
this.countWriten++;
}
return stream.Transform.prototype.write.call(this, chunk, encoding, callback);
}...
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
});
```
### Stream example
...transform = function () {
var argument, callback, data, error, handler, i, j, k, len, options, result, transform, type, v;
options = {};
for (i = j = 0, len = arguments.length; j < len; i = ++j) {
argument = arguments[i];
type = typeof argument;
if (argument === null) {
type = 'null';
} else if (type === 'object' && Array.isArray(argument)) {
type = 'array';
}
if (i === 0) {
if (type === 'function') {
handler = argument;
} else if (type !== null) {
data = argument;
}
continue;
}
if (type === 'object') {
for (k in argument) {
v = argument[k];
options[k] = v;
}
} else if (type === 'function') {
if (handler && i === arguments.length - 1) {
callback = argument;
} else {
handler = argument;
}
} else if (type !== 'null') {
throw new Error('Invalid arguments');
}
}
transform = new Transformer(options, handler);
error = false;
if (data) {
process.nextTick(function() {
var len1, m, row;
for (m = 0, len1 = data.length; m < len1; m++) {
row = data[m];
if (error) {
break;
}
transform.write(row);
}
return transform.end();
});
}
if (callback || options.consume) {
result = [];
transform.on('readable', function() {
var r, results;
results = [];
while ((r = transform.read())) {
if (callback) {
results.push(result.push(r));
} else {
results.push(void 0);
}
}
return results;
});
transform.on('error', function(err) {
error = true;
if (callback) {
return callback(err);
}
});
transform.on('end', function() {
if (callback && !error) {
return callback(null, result);
}
});
}
return transform;
}...
Execute this script with the command `node samples/callback.js`.
```javascript
var csv = require('csv');
csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
csv.stringify(data, function(err, data){
process.stdout.write(data);
});
});
});
...Transformer = function (options1, transform1) {
var base;
this.options = options1 != null ? options1 : {};
this.transform = transform1;
this.options.objectMode = true;
if ((base = this.options).parallel == null) {
base.parallel = 100;
}
stream.Transform.call(this, this.options);
this.running = 0;
this.started = 0;
this.finished = 0;
return this;
}n/a
Transformer = function (options1, transform1) {
var base;
this.options = options1 != null ? options1 : {};
this.transform = transform1;
this.options.objectMode = true;
if ((base = this.options).parallel == null) {
base.parallel = 100;
}
stream.Transform.call(this, this.options);
this.running = 0;
this.started = 0;
this.finished = 0;
return this;
}n/a
function Transform(options) {
if (!(this instanceof Transform))
return new Transform(options);
Duplex.call(this, options);
this._transformState = new TransformState(this);
var stream = this;
// start out asking for a readable event once data is transformed.
this._readableState.needReadable = true;
// we have implemented the _read method, and done the other things
// that Readable wants before the first _read call, so unset the
// sync guard flag.
this._readableState.sync = false;
if (options) {
if (typeof options.transform === 'function')
this._transform = options.transform;
if (typeof options.flush === 'function')
this._flush = options.flush;
}
// When the writable side finishes, then flush out anything remaining.
this.once('prefinish', function() {
if (typeof this._flush === 'function')
this._flush(function(er) {
done(stream, er);
});
else
done(stream);
});
}n/a
_done = function (err, chunks, cb) {
var chunk, j, len;
this.running--;
if (err) {
return this.emit('error', err);
}
this.finished++;
for (j = 0, len = chunks.length; j < len; j++) {
chunk = chunks[j];
if (typeof chunk === 'number') {
chunk = "" + chunk;
}
if (chunk != null) {
this.push(chunk);
}
}
if (cb) {
cb();
}
if (this._ending) {
return this._ending();
}
}n/a
_flush = function (cb) {
this._ending = function() {
if (this.running === 0) {
return cb();
}
};
return this._ending();
}n/a
_transform = function (chunk, encoding, cb) {
var callback, err, l;
this.started++;
this.running++;
if (this.running < this.options.parallel) {
cb();
cb = null;
}
try {
l = this.transform.length;
if (this.options.params != null) {
l--;
}
if (l === 1) {
this._done(null, [this.transform.call(null, chunk, this.options.params)], cb);
} else if (l === 2) {
callback = (function(_this) {
return function() {
var chunks, err;
err = arguments[0], chunks = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return _this._done(err, chunks, cb);
};
})(this);
this.transform.call(null, chunk, callback, this.options.params);
} else {
throw Error("Invalid handler arguments");
}
return false;
} catch (_error) {
err = _error;
return this._done(err);
}
}n/a