<commit_before>module.exports = flatten;

function flatten(obj) {
  var flattened = {};

  var circlular = [];
  var circLoc = [];

  function _route(prefix, value) {
    var i, len, type, keys, circularCheck, loc;

    if (value === null) {
      flattened[prefix] = null;
      return;
    }

    type = typeof value;
    if (typeof value === 'object') {
      circularCheck = circlular.indexOf(value);
      if (circularCheck >= 0) {
        loc = circLoc[circularCheck] || 'this';
        flattened[prefix] = '[Circular (' + loc + ')]';
        return;
      }
      circlular.push(value);
      circLoc.push(prefix);

      if (Array.isArray(value)) {
        len = value.length;
        if (len === 0) {
          _route(prefix + '[]', null);
        }
        for (i = 0; i < len; i++) {
          _route(prefix + '[' + i + ']', value[i]);
        }
        return;
      }
      if (value.hasOwnProperty('_bsontype')) {
        flattened[prefix] = value.toString();
        return;
      }
      keys = Object.keys(value);
      len = keys.length;
      if (prefix) {
        prefix = prefix + '.';
      }
      if (len === 0) {
        _route(prefix, null);
      }
      for (i = 0; i < len; i++) {
        _route(prefix + keys[i], value[keys[i]]);
      }
      return;
    }
    flattened[prefix] = value;
  }

  _route('', obj);

  return flattened;
}<commit_msg>fix(flatten): proper date + bson type support<commit_after>module.exports = flatten;
var debug = require('debug')('mongodb-schema:flatten');

function flatten(obj) {
  var flattened = {};

  function _route(prefix, value) {
    var i;

    if (value === null) {
      flattened[prefix] = null;
      return;
    }

    var type = typeof value;

    if (type === 'string') {
      flattened[prefix] = value;
    }
    // booleans, null and undefined
    else if (type === 'boolean' || obj === null || obj === undefined) {
      flattened[prefix] = value;
    }
    // numbers
    else if (type === 'number') {
      flattened[prefix] = value;
    }
    // dates
    else if (Object.prototype.toString.call(value) === '[object Date]') {
      flattened[prefix] = value;
    } else if (Array.isArray(value)) {
      len = value.length;
      flattened[prefix] = 'Array';

      if (len === 0) {
        _route(prefix + '[]', null);
      }
      for (i = 0; i < len; i++) {
        _route(prefix + '[' + i + ']', value[i]);
      }
    } else if (type === 'object') {
      if (value.hasOwnProperty('_bsontype')) {
        debug('_bsontype is %s', value._bsontype);
        flattened[prefix] = value;
      } else {
        var keys = Object.keys(value);
        var len = keys.length;
        if (prefix) {
          flattened[prefix] = 'Object';
        }

        if (prefix) {
          prefix = prefix + '.';
        }
        if (len === 0) {
          _route(prefix, null);
        }
        for (i = 0; i < len; i++) {
          _route(prefix + keys[i], value[keys[i]]);
        }
      }
    } else {
      throw new Error('Unknown type for ' + JSON.stringify(value));
    }
  }

  _route('', obj);

  return flattened;
}
{'fraction_non_alphanumeric': 0.16817010309278352, 'fraction_numerical': 0.0028994845360824743, 'mean_word_length': 1.9375591296121097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, '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': '862672', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1017, 'n_words': 236}