<commit_before>"use strict";

var crypto  = require('crypto');
var charset = require('./charset.js');

exports.generate = function(length, options) {
  
  var chars;
  
  if (typeof options === 'object') {
    if (options.charset) {
      chars = charset.generate(options.charset, options.readable);
    }
    else {
      charset.generate('default', options.readable);
    }
  }
  else {
    chars = charset.generate('default');
  }

  length = length || 32;

  var string = '';

  while(string.length < length) {
    var bf;
    try {
      bf = crypto.randomBytes(length);
    }
    catch (e) {
      continue;
    }
    for (var i = 0; i < bf.length; i++) {
      var index = bf.readUInt8(i) % chars.length;
      string += chars.charAt(index);
    }
  }

  return string;
}<commit_msg>Move length to options while keeping support for legacy params<commit_after>"use strict";

var crypto  = require('crypto');
var charset = require('./charset.js');

exports.generate = function(options) {
  
  var length, chars, string = '';
  
  // Handle options
  if (typeof options === 'object') {
    length = options.length || 32;
    
    if (options.charset) {
      chars = charset.generate(options.charset, options.readable);
    }
    else {
      charset.generate('default', options.readable);
    }
  }
  else if (typeof options === 'number') {
    length = options;
    chars  = charset.generate('default');
  }
  else {
    length = 32;
    chars  = charset.generate('default');
  }
  
  // Generate the string
  while (string.length < length) {
    var bf;
    try {
      bf = crypto.randomBytes(length);
    }
    catch (e) {
      continue;
    }
    for (var i = 0; i < bf.length; i++) {
      var index = bf.readUInt8(i) % chars.length;
      string += chars.charAt(index);
    }
  }

  return string;
}
{'fraction_non_alphanumeric': 0.14845474613686535, 'fraction_numerical': 0.005518763796909493, 'mean_word_length': 2.4932562620423893, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4696734', 'n_tokens_mistral': 620, 'n_tokens_neox': 573, 'n_words': 141}