Socket
Socket
Sign inDemoInstall

csv-generate

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csv-generate - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

samples/api.callback.js

12

CHANGELOG.md
# Changelog
## Version 2.2.0
* package: move to csv.js.org
* package: upgrade dependencies including babel 7
* options: new duration option
* samples: new api sync scripts
* samples: new objectmode scripts
* readme: remove api doc
* travis: support Node.js 10
* samples: update syntax
* package: improve ignore files
## Version 2.1.0

@@ -5,0 +17,0 @@

165

lib/es5/index.js

@@ -1,15 +0,6 @@

'use strict';
"use strict";
// Generated by CoffeeScript 2.0.3
// Generated by CoffeeScript 2.3.2
// # CSV Generator
// A Readable Stream for random CSV and data generator. Features include:
// * Node.js Stream 2 implementation.
// * Idempotence with the "seed" option.
// * Framework to generate custom data.
// * Various option to personnalize the generation.
// Please look at the [documentation], the [README], the [samples] and the
// [tests] for additional information.
// Please look at the [documentation](https://csv.js.org/generate/) for additional information.
var _Generator,

@@ -21,18 +12,12 @@ stream,

stream = require('stream');
util = require('util');
// ## Usage
util = require('util'); // ## Usage
// Stream API, for maximum of power:
// `generate([options])`
// Callback approach, for ease of use:
// `generate([options], callback)`
// ## Source Code
// ## Source Code
module.exports = function () {
var callback, data, generator, options;
if (arguments.length === 2) {

@@ -51,3 +36,5 @@ options = arguments[0];

}
generator = new _Generator(options);
if (callback) {

@@ -58,5 +45,7 @@ data = [];

results = [];
while (d = generator.read()) {
results.push(data.push(options.objectMode ? d : d.toString()));
}
return results;

@@ -69,40 +58,50 @@ });

}
return generator;
};
}; // ## `Generator([options])`
// Feel free to ask for new features and to participate by writting issues and preparing push requests.
// Options are documented [here](http://csv.js.org/generate/options/).
// ## `Generator([options])`
// Feel free to ask for new features and to participate by writting issues and
// preparing push requests.
// Options are documented [here](http://csv.adaltas.com/generate/).
_Generator = function Generator() {
var options1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var accepted_header_types, base, base1, base2, base3, base4, base5, base6, i, j, len, ref, v;
var accepted_header_types, base, base1, base2, base3, base4, base5, base6, base7, i, j, len, ref, v;
this.options = options1;
stream.Readable.call(this, this.options);
if ((base = this.options).columns == null) {
base.columns = 8;
}
if ((base1 = this.options).max_word_length == null) {
base1.max_word_length = 16;
}
if ((base2 = this.options).fixed_size == null) {
base2.fixed_size = false;
}
if ((base3 = this.options).end == null) {
base3.end = null;
}
if ((base4 = this.options).seed == null) {
base4.seed = false;
if ((base4 = this.options).duration == null) {
base4.duration = null;
}
if ((base5 = this.options).length == null) {
base5.length = -1;
if ((base5 = this.options).seed == null) {
base5.seed = false;
}
if ((base6 = this.options).delimiter == null) {
base6.delimiter = ',';
if ((base6 = this.options).length == null) {
base6.length = -1;
}
// State
if ((base7 = this.options).delimiter == null) {
base7.delimiter = ',';
} // State
this._ = {
start_time: Date.now(),
fixed_size_buffer: '',

@@ -112,5 +111,7 @@ count_written: 0,

};
if (typeof this.options.columns === 'number') {
this.options.columns = new Array(this.options.columns);
}
accepted_header_types = Object.keys(_Generator).filter(function (t) {

@@ -120,14 +121,19 @@ return t !== 'super_';

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') {
if (indexOf.call(accepted_header_types, v) < 0) {
throw Error('Invalid column type: got "' + v + '", default values are ' + JSON.stringify(accepted_header_types));
throw Error("Invalid column type: got \"".concat(v, "\", default values are ").concat(JSON.stringify(accepted_header_types)));
}
this.options.columns[i] = _Generator[v];
}
}
return this;

@@ -137,9 +143,5 @@ };

util.inherits(_Generator, stream.Readable);
module.exports.Generator = _Generator; // ## `Generator.prototype.random()`
// Generate a random number between 0 and 1 with 2 decimals. The function is idempotent if it detect the "seed" option.
module.exports.Generator = _Generator;
// ## `Generator.prototype.random()`
// Generate a random number between 0 and 1 with 2 decimals. The function is
// idempotent if it detect the "seed" option.
_Generator.prototype.random = function () {

@@ -151,25 +153,25 @@ if (this.options.seed) {

}
};
}; // ## `Generator.prototype.end()`
// Stop the generation.
// ## `Generator.prototype.end()`
// Stop the generation.
_Generator.prototype.end = function () {
return this.push(null);
};
}; // ## `Generator.prototype._read(size)`
// Put new data into the read queue.
// ## `Generator.prototype._read(size)`
// Put new data into the read queue.
_Generator.prototype._read = function (size) {
var column, data, header, j, k, l, len, len1, len2, len3, length, line, lineLength, m, ref;
// Already started
var column, data, header, j, k, l, len, len1, len2, len3, length, line, lineLength, m, ref; // Already started
data = [];
length = this._.fixed_size_buffer.length;
if (length) {
data.push(this._.fixed_size_buffer);
}
while (true) {
// Time for some rest: flush first and stop later
if (this._.count_created === this.options.length || this.options.end && Date.now() > this.options.end) {
if (this._.count_created === this.options.length || this.options.end && Date.now() > this.options.end || this.options.duration && Date.now() > this._.start_time + this.options.duration) {
// Flush

@@ -187,17 +189,22 @@ if (data.length) {

}
}
// Stop
} // Stop
return this.push(null);
}
// Create the line
} // Create the line
line = [];
ref = this.options.columns;
for (k = 0, len1 = ref.length; k < len1; k++) {
header = ref[k];
// Create the field
line.push('' + header(this));
}
// Obtain line length
header = ref[k]; // Create the field
line.push("".concat(header(this)));
} // Obtain line length
if (this.options.objectMode) {
lineLength = 0;
for (l = 0, len2 = line.length; l < len2; l++) {

@@ -209,9 +216,12 @@ column = line[l];

// Stringify the line
line = '' + (this._.count_created === 0 ? '' : '\n') + line.join(this.options.delimiter);
line = "".concat(this._.count_created === 0 ? '' : '\n').concat(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++) {

@@ -229,19 +239,22 @@ line = data[m];

}
this._.count_written++;
this.push(data.join(''));
}
break;
}
length += lineLength;
data.push(line);
}
};
}; // ## `Generator.ascii(gen)`
// Generate an ASCII value.
// ## `Generator.ascii(gen)`
// Generate an ASCII value.
_Generator.ascii = function (gen) {
var char, column, j, nb_chars, ref;
// Column
var char, column, j, nb_chars, ref; // Column
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) {

@@ -251,22 +264,16 @@ char = Math.floor(gen.random() * 32);

}
return column.join('');
};
}; // ## `Generator.ascii(gen)`
// Generate an integer value.
// ## `Generator.ascii(gen)`
// Generate an integer value.
_Generator.int = function (gen) {
return Math.floor(gen.random() * Math.pow(2, 52));
};
}; // ## `Generator.bool(gen)`
// Generate an boolean value.
// ## `Generator.bool(gen)`
// Generate an boolean value.
_Generator.bool = function (gen) {
return Math.floor(gen.random() * 2);
};
// [documentation]: http://csv.adaltas.com/generate/
// [readme]: https://github.com/wdavidw/node-csv-generate
// [samples]: https://github.com/wdavidw/node-csv-generate/tree/master/samples
// [tests]: https://github.com/wdavidw/node-csv-generate/tree/master/test
};

@@ -1,13 +0,10 @@

'use strict';
"use strict";
// Generated by CoffeeScript 2.0.3
// Generated by CoffeeScript 2.3.2
// # CSV Generator Sync
// Provides a synchronous alternative to the CSV generator.
// ## Usage
// `generate(options)`
// `const csv = generate(options)`
// ## Source Code
var generate;
generate = require('.');

@@ -17,5 +14,7 @@

var chunks, generator, work;
if (typeof options === 'string' && /\d+/.test(options)) {
options = parseInt(options);
}
if (Number.isInteger(options)) {

@@ -26,10 +25,13 @@ options = {

}
if (!Number.isInteger(options != null ? options.length : void 0)) {
throw Error('Invalid Argument: length is not defined');
}
chunks = [];
work = true;
// See https://nodejs.org/api/stream.html#stream_new_stream_readable_options
work = true; // See https://nodejs.org/api/stream.html#stream_new_stream_readable_options
options.highWaterMark = options.objectMode ? 16 : 16384;
generator = new generate.Generator(options);
generator.push = function (chunk) {

@@ -39,2 +41,3 @@ if (chunk === null) {

}
if (options.objectMode) {

@@ -46,9 +49,12 @@ return chunks.push(chunk);

};
while (work) {
generator._read(options.highWaterMark);
}
if (!options.objectMode) {
chunks = chunks.join('');
}
return chunks;
};

@@ -1,13 +0,5 @@

// Generated by CoffeeScript 2.0.3
// Generated by CoffeeScript 2.3.2
// # CSV Generator
// A Readable Stream for random CSV and data generator. Features include:
// * Node.js Stream 2 implementation.
// * Idempotence with the "seed" option.
// * Framework to generate custom data.
// * Various option to personnalize the generation.
// Please look at the [documentation], the [README], the [samples] and the
// [tests] for additional information.
// Please look at the [documentation](https://csv.js.org/generate/) for additional information.
var Generator, stream, util,

@@ -67,8 +59,7 @@ indexOf = [].indexOf;

// Feel free to ask for new features and to participate by writting issues and
// preparing push requests.
// Feel free to ask for new features and to participate by writting issues and preparing push requests.
// Options are documented [here](http://csv.adaltas.com/generate/).
// Options are documented [here](http://csv.js.org/generate/options/).
Generator = function(options1 = {}) {
var accepted_header_types, base, base1, base2, base3, base4, base5, base6, i, j, len, ref, v;
var accepted_header_types, base, base1, base2, base3, base4, base5, base6, base7, i, j, len, ref, v;
this.options = options1;

@@ -88,13 +79,17 @@ stream.Readable.call(this, this.options);

}
if ((base4 = this.options).seed == null) {
base4.seed = false;
if ((base4 = this.options).duration == null) {
base4.duration = null;
}
if ((base5 = this.options).length == null) {
base5.length = -1;
if ((base5 = this.options).seed == null) {
base5.seed = false;
}
if ((base6 = this.options).delimiter == null) {
base6.delimiter = ',';
if ((base6 = this.options).length == null) {
base6.length = -1;
}
if ((base7 = this.options).delimiter == null) {
base7.delimiter = ',';
}
// State
this._ = {
start_time: Date.now(),
fixed_size_buffer: '',

@@ -132,4 +127,3 @@ count_written: 0,

// Generate a random number between 0 and 1 with 2 decimals. The function is
// idempotent if it detect the "seed" option.
// Generate a random number between 0 and 1 with 2 decimals. The function is idempotent if it detect the "seed" option.
Generator.prototype.random = function() {

@@ -163,3 +157,3 @@ if (this.options.seed) {

// Time for some rest: flush first and stop later
if ((this._.count_created === this.options.length) || (this.options.end && Date.now() > this.options.end)) {
if ((this._.count_created === this.options.length) || (this.options.end && Date.now() > this.options.end) || (this.options.duration && Date.now() > this._.start_time + this.options.duration)) {
// Flush

@@ -234,3 +228,3 @@ if (data.length) {

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) {
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);

@@ -255,6 +249,1 @@ column.push(String.fromCharCode(char + (char < 16 ? 65 : 97 - 16)));

};
// [documentation]: http://csv.adaltas.com/generate/
// [readme]: https://github.com/wdavidw/node-csv-generate
// [samples]: https://github.com/wdavidw/node-csv-generate/tree/master/samples
// [tests]: https://github.com/wdavidw/node-csv-generate/tree/master/test

@@ -1,2 +0,2 @@

// Generated by CoffeeScript 2.0.3
// Generated by CoffeeScript 2.3.2
// # CSV Generator Sync

@@ -8,3 +8,5 @@

// `generate(options)`
// `const csv = generate(options)`
// ## Source Code
var generate;

@@ -11,0 +13,0 @@

{
"version": "2.1.0",
"version": "2.2.0",
"name": "csv-generate",

@@ -16,11 +16,12 @@ "description": "CSV and object generation implementing the Node.js `stream.Readable` API",

},
"homepage": "http://csv.adaltas.com/generate/",
"homepage": "https://csv.js.org/generate/",
"bugs": "https://github.com/adaltas/node-csv-generate/issues",
"author": "David Worms <david@adaltas.com> (http://www.adaltas.com)",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"coffeescript": "~2.0.1",
"mocha": "~4.0.1",
"should": "~13.1.2"
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"coffeescript": "~2.3.2",
"mocha": "~5.2.0",
"should": "~13.2.3"
},

@@ -27,0 +28,0 @@ "optionalDependencies": {},

@@ -1,101 +0,43 @@

[![Build Status](https://secure.travis-ci.org/adaltas/node-csv-generate.svg)][travis]
CSV and object generation
=========================
[![Build Status](https://api.travis-ci.org/adaltas/node-csv-generate.svg)](https://travis-ci.org/#!/adaltas/node-csv-generate)
# CSV and object generation
This package provides a flexible generator of CSV strings and Javascript objects
implementing the Node.js `stream.Readable` API.
[Documentation for the "csv-generate" package is available here][home].
[Documentation for the "csv-generate" package is available here](https://csv.js.org/generate/).
Features includes:
## Documentation
* random or pseudo-random seed based generation
* `stream.Readable` implementation
* BSD License
* [Project homepage](http://localhost:8000/stringify/)
* [API](http://localhost:8000/stringify/api/)
* [Options](http://localhost:8000/stringify/options/)
* [Examples](http://localhost:8000/stringify/examples/)
Usage
-----
## Main features
Run `npm install csv` to install the full csv module or run
`npm install csv-generate` if you are only interested by the CSV generator.
* Scalable `stream.Readable` implementation
* random or pseudo-random seed based generation
* Idempotence with the "seed" option
* User-defined value generation
* Multiple types of values (integer, boolean, dates, ...)
* BSD License
Use the callback style API for simplicity or the stream based API for
scalability.
## Usage
### Using the callback API
Run `npm install csv` to install the full csv module or run `npm install csv-generate` if you are only interested by the CSV generator.
The parser receive a string and return an array inside a user-provided
callback. This example is available with the command `node samples/callback.js`.
Use the callback style API for simplicity or the stream based API for scalability.
```javascript
var generate = require('csv-generate');
## Development
generate({seed: 1, columns: 2, length: 2}, function(err, output){
output.should.eql('OMH,ONKCHhJmjadoA\nD,GeACHiN');
});
```
Tests are executed with [Mocha](https://mochajs.org/). To install it, simple run `npm install` followed by `npm test`. It will install mocha and its dependencies in your project "node_modules" directory and run the test suite. The tests run against the CoffeeScript source files.
### Using the stream API
```javascript
// node samples/stream.js
var generate = require('csv-generate');
var data = []
var generator = generate({seed: 1, objectMode: true, columns: 2, length: 2});
generator.on('readable', function(){
while(d = generator.read()){
data.push(d);
}
});
generator.on('error', function(err){
console.log(err);
});
generator.on('end', function(){
data.should.eql([ [ 'OMH', 'ONKCHhJmjadoA' ],[ 'D', 'GeACHiN' ] ]);
});
```
### Using the pipe function
One usefull function part of the Stream API is `pipe` to interact between
multiple streams. You may use this function to pipe a `stream.Readable` string
source to a `stream.Writable` object destination. The next example available as
`node samples/pipe.js` read the file, parse its content and transform it.
```javascript
// node samples/pipe.js
var generate = require('csv-generate');
var generator = generate({columns: ['int', 'bool'], length: 2});
generator.pipe(process.stdout);
```
Migration
---------
Most of the generator is imported from its parent project [CSV][csv] in a effort
to split it between the generator, the parser, the transformer and the stringifier.
Development
-----------
Tests are executed with mocha. To install it, simple run `npm install`
followed by `npm test`. It will install mocha and its dependencies in your
project "node_modules" directory and run the test suite. The tests run
against the CoffeeScript source files.
To generate the JavaScript files, run `npm run coffee`.
The test suite is run online with [Travis][travis] against the versions
0.9, 0.10 and 0.11 of Node.js.
The test suite is run online with [Travis](https://travis-ci.org/#!/adaltas/node-csv-generate). See the [Travis definition file](https://github.com/adaltas/node-csv-generate/blob/master/.travis.yml) to view the tested Node.js version.
Contributors
------------
## Contributors
* David Worms: <https://github.com/wdavidw>
[home]: http://csv.adaltas.com/generate/
[csv]: https://github.com/adaltas/node-csv
[travis]: https://travis-ci.org/#!/adaltas/node-csv-generate
should = require('should');
generate = require('../lib');
const generate = require('../lib')
var generator = generate({columns: ['int', 'bool'], length: 2});
generator.pipe(process.stdout);
generate({
columns: ['int', 'bool'],
length: 2
})
.pipe(process.stdout)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc