<commit_before>module.exports = formatter;

function formatter(options) {
  options = options || {};
  options.negative = options.negative === 'R' ? 'R' : 'L';
  options.negativeOut = options.negativeOut === false ? false : true;
  options.prefix = options.prefix || '';
  options.suffix = options.suffix || '';
  options.separator = typeof options.separator === 'string' ? options.separator : ',';
  options.decimal = options.decimal || '.';
  
  function format(number, includeUnits, separate) {
    includeUnits = includeUnits === false ? false : true;
    separate = separate === false ? false : true;
    if (number || number === 0) {
      number = '' + number;//convert number to string if it isn't already
    } else {
      return '';
    }
    var output = [];
    var negative = number.charAt(0) === '-';
    number = number.replace(/^\-/g, '');

    if (!options.negativeOut && includeUnits) {
      output.push(options.prefix);
    }
    if (negative && options.negative === 'L') {
      output.push('-');
    }
    if (options.negativeOut && includeUnits) {
      output.push(options.prefix);
    }

    number = number.split(options.decimal);
    if (options.padLeft) number[0] = padLeft(number[0], options.padLeft);
    if (separate) number[0] = addSeparators(number[0], options.separator);
    output.push(number[0]);
    if (options.padRight) number[1] = padRight(number[1], options.padRight);
    if (options.truncate != null) number[1] = truncate(number[1], options.truncate);
    if (number[1]) {
      output.push(options.decimal);
      output.push(number[1]);
    }


    if (options.negativeOut && includeUnits) {
      output.push(options.suffix);
    }
    if (negative && options.negative === 'R') {
      output.push('-');
    }
    if (!options.negativeOut && includeUnits) {
      output.push(options.suffix);
    }

    return output.join('');
  }

  format.negative = options.negative;
  format.negativeOut = options.negativeOut;
  format.prefix = options.prefix;
  format.suffix = options.suffix;
  format.separate = options.separate;
  format.separator = options.separator;
  format.decimal = options.decimal;
  format.padLeft = options.padLeft;
  format.padRight = options.padRight;
  format.truncate = options.truncate;

  function unformat(number, allowedSeparators) {
    allowedSeparators = allowedSeparators || [];
    if (options.allowedSeparators) {
      options.allowedSeparators.forEach(function (s) { allowedSeparators.push (s); });
    }
    allowedSeparators.push(options.separator);
    number = number.replace(options.prefix, '');
    number = number.replace(options.suffix, '');
    var newNumber = number;
    do {
      number = newNumber;
      for (var i = 0; i < allowedSeparators.length; i++) {
        newNumber = newNumber.replace(allowedSeparators[i], '');
      }
    } while (newNumber != number);
    return number;
  }
  format.unformat = unformat;

  function validate(number, allowedSeparators) {
    number = unformat(number, allowedSeparators);
    number = number.split(options.decimal);
    if (number.length > 2) {
      return false;
    } else if (options.truncate != null && number[1] && number[1].length > options.truncate) {
      return false;
    } else {
      return /^\d*\.\d*$/.test(number);
    }
  }<commit_msg>Improve validation regex<commit_after>module.exports = formatter;

function formatter(options) {
  options = options || {};
  options.negative = options.negative === 'R' ? 'R' : 'L';
  options.negativeOut = options.negativeOut === false ? false : true;
  options.prefix = options.prefix || '';
  options.suffix = options.suffix || '';
  options.separator = typeof options.separator === 'string' ? options.separator : ',';
  options.decimal = options.decimal || '.';
  
  function format(number, includeUnits, separate) {
    includeUnits = includeUnits === false ? false : true;
    separate = separate === false ? false : true;
    if (number || number === 0) {
      number = '' + number;//convert number to string if it isn't already
    } else {
      return '';
    }
    var output = [];
    var negative = number.charAt(0) === '-';
    number = number.replace(/^\-/g, '');

    if (!options.negativeOut && includeUnits) {
      output.push(options.prefix);
    }
    if (negative && options.negative === 'L') {
      output.push('-');
    }
    if (options.negativeOut && includeUnits) {
      output.push(options.prefix);
    }

    number = number.split(options.decimal);
    if (options.padLeft) number[0] = padLeft(number[0], options.padLeft);
    if (separate) number[0] = addSeparators(number[0], options.separator);
    output.push(number[0]);
    if (options.padRight) number[1] = padRight(number[1], options.padRight);
    if (options.truncate != null) number[1] = truncate(number[1], options.truncate);
    if (number[1]) {
      output.push(options.decimal);
      output.push(number[1]);
    }


    if (options.negativeOut && includeUnits) {
      output.push(options.suffix);
    }
    if (negative && options.negative === 'R') {
      output.push('-');
    }
    if (!options.negativeOut && includeUnits) {
      output.push(options.suffix);
    }

    return output.join('');
  }

  format.negative = options.negative;
  format.negativeOut = options.negativeOut;
  format.prefix = options.prefix;
  format.suffix = options.suffix;
  format.separate = options.separate;
  format.separator = options.separator;
  format.decimal = options.decimal;
  format.padLeft = options.padLeft;
  format.padRight = options.padRight;
  format.truncate = options.truncate;

  function unformat(number, allowedSeparators) {
    allowedSeparators = allowedSeparators || [];
    if (options.allowedSeparators) {
      options.allowedSeparators.forEach(function (s) { allowedSeparators.push (s); });
    }
    allowedSeparators.push(options.separator);
    number = number.replace(options.prefix, '');
    number = number.replace(options.suffix, '');
    var newNumber = number;
    do {
      number = newNumber;
      for (var i = 0; i < allowedSeparators.length; i++) {
        newNumber = newNumber.replace(allowedSeparators[i], '');
      }
    } while (newNumber != number);
    return number;
  }
  format.unformat = unformat;

  function validate(number, allowedSeparators) {
    number = unformat(number, allowedSeparators);
    number = number.split(options.decimal);
    if (number.length > 2) {
      return false;
    } else if (options.truncate != null && number[1] && number[1].length > options.truncate) {
      return false;
    } else {
      return /^-?\d+\.?\d*$/.test(number);
    }
  }
{'fraction_non_alphanumeric': 0.1551619739630639, 'fraction_numerical': 0.0051468362095065095, 'mean_word_length': 3.1975857687420586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5333476', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2001, 'n_words': 406}