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 4.0.4 to 4.1.0

lib/api/index.js

170

lib/index.js

@@ -11,62 +11,9 @@

import util from 'util';
import {normalize_options, init_state, read} from './api/index.js';
const Generator = function(options = {}){
// Convert Stream Readable options if underscored
if(options.high_water_mark){
options.highWaterMark = options.high_water_mark;
}
if(options.object_mode){
options.objectMode = options.object_mode;
}
this.options = normalize_options(options);
// Call parent constructor
stream.Readable.call(this, options);
// Clone and camelize options
this.options = {};
for(const k in options){
this.options[Generator.camelize(k)] = options[k];
}
// Normalize options
const dft = {
columns: 8,
delimiter: ',',
duration: null,
encoding: null,
end: null,
eof: false,
fixedSize: false,
length: -1,
maxWordLength: 16,
rowDelimiter: '\n',
seed: false,
sleep: 0,
};
for(const k in dft){
if(this.options[k] === undefined){
this.options[k] = dft[k];
}
}
// Default values
if(this.options.eof === true){
this.options.eof = this.options.rowDelimiter;
}
// State
this._ = {
start_time: this.options.duration ? Date.now() : null,
fixed_size_buffer: '',
count_written: 0,
count_created: 0,
};
if(typeof this.options.columns === 'number'){
this.options.columns = new Array(this.options.columns);
}
const accepted_header_types = Object.keys(Generator).filter((t) => (!['super_', 'camelize'].includes(t)));
for(let i = 0; i < this.options.columns.length; i++){
const v = this.options.columns[i] || 'ascii';
if(typeof v === 'string'){
if(!accepted_header_types.includes(v)){
throw Error(`Invalid column type: got "${v}", default values are ${JSON.stringify(accepted_header_types)}`);
}
this.options.columns[i] = Generator[v];
}
}
stream.Readable.call(this, this.options);
this.state = init_state(this.options);
return this;

@@ -76,10 +23,2 @@ };

// 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(){
if(this.options.seed){
return this.options.seed = this.options.seed * Math.PI * 100 % 100 / 100;
}else{
return Math.random();
}
};
// Stop the generation.

@@ -91,73 +30,19 @@ Generator.prototype.end = function(){

Generator.prototype._read = function(size){
// Already started
const data = [];
let length = this._.fixed_size_buffer.length;
if(length !== 0){
data.push(this._.fixed_size_buffer);
const self = this;
const err = read(this.options, this.state, size, function(chunk) {
self.__push(chunk);
}, function(){
self.push(null);
});
if(err){
this.destroy(err);
}
// eslint-disable-next-line
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) || (this.options.duration && Date.now() > this._.start_time + this.options.duration)){
// Flush
if(data.length){
if(this.options.objectMode){
for(const record of data){
this.__push(record);
}
}else{
this.__push(data.join('') + (this.options.eof ? this.options.eof : ''));
}
this._.end = true;
}else{
this.push(null);
}
return;
}
// Create the record
let record = [];
let recordLength;
this.options.columns.forEach((fn) => {
record.push(fn(this));
});
// Obtain record length
if(this.options.objectMode){
recordLength = 0;
// recordLength is currently equal to the number of columns
// This is wrong and shall equal to 1 record only
for(const column of record)
recordLength += column.length;
}else{
// Stringify the record
record = (this._.count_created === 0 ? '' : this.options.rowDelimiter)+record.join(this.options.delimiter);
recordLength = record.length;
}
this._.count_created++;
if(length + recordLength > size){
if(this.options.objectMode){
data.push(record);
for(const record of data){
this.__push(record);
}
}else{
if(this.options.fixedSize){
this._.fixed_size_buffer = record.substr(size - length);
data.push(record.substr(0, size - length));
}else{
data.push(record);
}
this.__push(data.join(''));
}
return;
}
length += recordLength;
data.push(record);
}
};
// Put new data into the read queue.
Generator.prototype.__push = function(record){
// console.log('push', record)
const push = () => {
this._.count_written++;
this.state.count_written++;
this.push(record);
if(this._.end === true){
if(this.state.end === true){
return this.push(null);

@@ -168,27 +53,2 @@ }

};
// Generate an ASCII value.
Generator.ascii = function(gen){
// Column
const column = [];
const nb_chars = Math.ceil(gen.random() * gen.options.maxWordLength);
for(let i=0; i<nb_chars; i++){
const char = Math.floor(gen.random() * 32);
column.push(String.fromCharCode(char + (char < 16 ? 65 : 97 - 16)));
}
return column.join('');
};
// Generate an integer value.
Generator.int = function(gen){
return Math.floor(gen.random() * Math.pow(2, 52));
};
// Generate an boolean value.
Generator.bool = function(gen){
return Math.floor(gen.random() * 2);
};
// Camelize option properties
Generator.camelize = function(str){
return str.replace(/_([a-z])/gi, function(_, match){
return match.toUpperCase();
});
};

@@ -195,0 +55,0 @@ const generate = function(){

{
"version": "4.0.4",
"version": "4.1.0",
"name": "csv-generate",

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

"devDependencies": {
"@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-node-resolve": "^13.0.6",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.7",
"@rollup/plugin-eslint": "^8.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.35",
"@types/should": "^13.0.0",
"coffeescript": "~2.6.1",
"coffeescript": "~2.7.0",
"each": "^1.2.2",
"eslint": "^8.2.0",
"mocha": "~9.1.3",
"rollup": "^2.60.0",
"eslint": "^8.16.0",
"mocha": "~10.0.0",
"rollup": "^2.74.1",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"should": "~13.2.3",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
"ts-node": "^10.8.0",
"typescript": "^4.6.4"
},

@@ -40,2 +40,6 @@ "exports": {

},
"./stream": {
"import": "./lib/stream.js",
"require": "./dist/cjs/stream.cjs"
},
"./browser/esm": "./dist/esm/index.js",

@@ -53,3 +57,3 @@ "./browser/esm/sync": "./dist/esm/sync.js"

"inline-diffs": true,
"loader": "./test/loaders/all.mjs",
"loader": "./test/loaders/all.js",
"recursive": true,

@@ -79,3 +83,3 @@ "reporter": "spec",

"test": "mocha 'test/**/*.{coffee,ts}'",
"test:legacy": "mocha --loader=./test/loaders/legacy/all.mjs 'test/**/*.{coffee,ts}'"
"test:legacy": "mocha --ignore test/api.web_stream.coffee --loader=./test/loaders/legacy/all.js 'test/**/*.{coffee,ts}'"
},

@@ -100,3 +104,3 @@ "type": "module",

},
"gitHead": "bab8d89a6eb3bc073233e27b7af0a50284b1590f"
"gitHead": "59cf7a4333c08020a029fa6922483f058bec04ab"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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