wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"use strict";
 
// Generated by CoffeeScript 2.3.2
// # CSV Generator
// Please look at the [documentation](https://csv.js.org/generate/) for additional information.
var _Generator,
    stream,
    util,
    indexOf = [].indexOf;
 
stream = require('stream');
util = require('util'); // ## Usage  
// Stream API, for maximum of power:
// `generate([options])`
// Callback approach, for ease of use:
// `generate([options], callback)`
// ## Source Code
 
module.exports = 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(d));
      }
 
      return results;
    });
    generator.on('error', callback);
    generator.on('end', function () {
      if (!generator.options.objectMode) {
        if (generator.options.encoding) {
          data = data.join('');
        } else {
          data = Buffer.concat(data);
        }
      }
 
      return callback(null, data);
    });
  }
 
  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 = function Generator() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var accepted_header_types, base, base1, base10, base11, base2, base3, base4, base5, base6, base7, base8, base9, i, j, k, len, ref, v;
 
  if (options.high_water_mark) {
    // Convert Stream Readable options if underscored
    options.highWaterMark = options.high_water_mark;
  }
 
  if (options.object_mode) {
    options.objectMode = options.object_mode;
  } // Call parent constructor
 
 
  stream.Readable.call(this, options); // Clone and camelize options
 
  this.options = {};
 
  for (k in options) {
    v = options[k];
    this.options[_Generator.camelize(k)] = v;
  } // Normalize options
 
 
  if ((base = this.options).columns == null) {
    base.columns = 8;
  }
 
  if ((base1 = this.options).delimiter == null) {
    base1.delimiter = ',';
  }
 
  if ((base2 = this.options).duration == null) {
    base2.duration = null;
  }
 
  if ((base3 = this.options).encoding == null) {
    base3.encoding = null;
  }
 
  if ((base4 = this.options).end == null) {
    base4.end = null;
  }
 
  if ((base5 = this.options).eof == null) {
    base5.eof = false;
  }
 
  if ((base6 = this.options).fixedSize == null) {
    base6.fixedSize = false;
  }
 
  if ((base7 = this.options).length == null) {
    base7.length = -1;
  }
 
  if ((base8 = this.options).maxWordLength == null) {
    base8.maxWordLength = 16;
  }
 
  if ((base9 = this.options).rowDelimiter == null) {
    base9.rowDelimiter = '\n';
  }
 
  if ((base10 = this.options).seed == null) {
    base10.seed = false;
  }
 
  if ((base11 = this.options).sleep == null) {
    base11.sleep = 0;
  }
 
  if (this.options.eof === true) {
    // Default values
    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);
  }
 
  accepted_header_types = Object.keys(_Generator).filter(function (t) {
    return t !== 'super_' && t !== 'camelize';
  });
  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 \"".concat(v, "\", default values are ").concat(JSON.stringify(accepted_header_types)));
      }
 
      this.options.columns[i] = _Generator[v];
    }
  }
 
  return this;
};
 
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.
 
_Generator.prototype.random = function () {
  if (this.options.seed) {
    return this.options.seed = this.options.seed * Math.PI * 100 % 100 / 100;
  } else {
    return Math.random();
  }
}; // ## `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 = function (size) {
  var column, data, header, j, l, len, len1, len2, len3, length, line, lineLength, m, n, 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 || this.options.duration && Date.now() > this._.start_time + this.options.duration) {
      // Flush
      if (data.length) {
        if (this.options.objectMode) {
          for (j = 0, len = data.length; j < len; j++) {
            line = data[j];
 
            this.__push(line);
          }
        } else {
          this.__push(data.join('') + (this.options.eof ? this.options.eof : ''));
        }
      } // Stop
 
 
      return this.push(null);
    } // Create the line
 
 
    line = [];
    ref = this.options.columns;
 
    for (l = 0, len1 = ref.length; l < len1; l++) {
      header = ref[l]; // Create the field
 
      line.push("".concat(header(this)));
    } // Obtain line length
 
 
    if (this.options.objectMode) {
      lineLength = 0;
 
      for (m = 0, len2 = line.length; m < len2; m++) {
        column = line[m];
        lineLength += column.length;
      }
    } else {
      // Stringify the line
      line = "".concat(this._.count_created === 0 ? '' : this.options.rowDelimiter).concat(line.join(this.options.delimiter));
      lineLength = line.length;
    }
 
    this._.count_created++;
 
    if (length + lineLength > size) {
      if (this.options.objectMode) {
        data.push(line);
 
        for (n = 0, len3 = data.length; n < len3; n++) {
          line = data[n];
 
          this.__push(line);
        }
      } else {
        if (this.options.fixedSize) {
          this._.fixed_size_buffer = line.substr(size - length);
          data.push(line.substr(0, size - length));
        } else {
          data.push(line);
        }
 
        this.__push(data.join(''));
      }
 
      break;
    }
 
    length += lineLength;
    data.push(line);
  }
}; // ## `Generator.prototype._read(size)`
// Put new data into the read queue.
 
 
_Generator.prototype.__push = function (record) {
  var _this = this;
 
  this._.count_written++;
 
  if (this.options.sleep > 0) {
    return setTimeout(function () {
      return _this.push(record);
    }, this.options.sleep);
  } else {
    return this.push(record);
  }
}; // ## `Generator.ascii(gen)`
// Generate an ASCII value.
 
 
_Generator.ascii = function (gen) {
  var _, _char, column, j, nb_chars, ref; // Column
 
 
  column = [];
  nb_chars = Math.ceil(gen.random() * gen.options.maxWordLength);
 
  for (_ = j = 0, ref = nb_chars; 0 <= ref ? j < ref : j > ref; _ = 0 <= ref ? ++j : --j) {
    _char = Math.floor(gen.random() * 32);
    column.push(String.fromCharCode(_char + (_char < 16 ? 65 : 97 - 16)));
  }
 
  return column.join('');
}; // ## `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 = function (gen) {
  return Math.floor(gen.random() * 2);
}; // ## `Generator.camelize`
 
 
_Generator.camelize = function (str) {
  return str.replace(/_([a-z])/gi, function (_, match, index) {
    return match.toUpperCase();
  });
};