<commit_before>"use strict"

var sortKeys = require('deep-sort-object')

/**
  * Sorts by keys using json-stable-stringify, maintaining undefined,
  * including by first key's values of first level nested arrays.
  */
function sortObject(originalSrc, options) {
  var out = {},
    mutable,
    callback

  if (typeof(options) == 'function') {
    callback = true;
  } else if(options !== undefined) {
    mutable = options.mutable !== undefined ? options.mutable : "nothing",
  }

  var src = sortKeys(originalSrc)
  Object.keys(src).forEach(function(key) {
    if (Array.isArray(src[key])) {
      out[key] = src[key].sort(firstKeyCompare)
    } else {
      out[key] = src[key]
    }
  })

  if (callback == true) return options(out)
  return out
}
 
var getFirstKey = function(obj) {
  return Object.keys(obj)[0]
}

// Sorts by first key.  If undefined, sort to bottom maintain original order. 
var firstKeyCompare = function (a, b) {
  var key = getFirstKey(a)
  
  var valueA = a[key],
         valueB = b[key]
  
  // need to delve deeper if value is non-null object
  if (a[key] && typeof(a[key]) === "object") {
    var subkey = getFirstKey(a[key])
    valueA = a[key][subkey]
    valueB = b[key] ? b[key][subkey] : null
  }
  return valueA && valueB ? compare(valueA, valueB)
               : !valueA && !valueB ? 0
                 : !valueA ? 1 : -1;
}

function compare(a, b) {
  return isNaN(a) ? a.localeCompare(b) : a-b
}

function retainUndefined(key, value) {
  return value ? value : null
}

module.exports = sortObject<commit_msg>Utilize "Sorty" for sorting objects.<commit_after>"use strict"

var sortKeys = require('deep-sort-object')
var sorty = require('sorty')

/**
  * Sorts by keys using json-stable-stringify, maintaining undefined,
  * including by first key's values of first level nested arrays.
  */
function sortObject(originalSrc, options) {
  var out = {},
    callback

  if (typeof(options) == 'function') {
    callback = true;
  }

  var src = sortKeys(originalSrc)
  Object.keys(src).forEach(function(key) {
    if (Array.isArray(src[key])) {
      out[key] = keyCompare(src[key])
    } else {
      out[key] = src[key]
    }
  })
  
  if (callback == true) return options(out)
  return out
}

 function keyCompare (arr) {
  var keyArr = []
  var objStrings = []
  var usedKeys = {}

  if (!Array.isArray(arr)) {
    Object.keys(arr).forEach(function(key) {
      if (arr[key] !== undefined && Array.isArray(arr[key])) {
        arr[key] = keyCompare(arr[key])
      }

      keyArr.push(key)
    })
  } else {
    arr.forEach(function(elmnt) {
      if (elmnt !== undefined && Array.isArray(elmnt)) {
        arr[elmnt] = keyCompare(elmnt)
      } else if((elmnt) !== undefined) {
        Object.keys(elmnt).forEach(function(key) {
          if (elmnt[key] !== undefined && Array.isArray(elmnt[key])) {
            elmnt[key] = keyCompare(elmnt[key])
        }

          if (usedKeys[key] === undefined) {
            keyArr.push(key)
            usedKeys[key] = ""
          }
        })
        objStrings.push(JSON.stringify(elmnt))

      }
    })
    sort(objStrings, keyArr)
  }
  
  return sort(arr, keyArr)
}

function sort(arr, keyArr) {
    if (Array.isArray(arr)) {
    var params = createSortyString(keyArr)

    sorty(params, arr)
  } else if (typeof(arr) === 'object') {
    var data = []
    var params = createSortyString(keyArr)
    data.push(arr)

    sorty(params, arr)
  }
  return arr
}

function createSortyString(keys) {
  var result = []
  keys.forEach(function(keyName) {
    result.push({name: keyName, dir: "asc"})
  })
  return result
}

module.exports = sortObject
{'fraction_non_alphanumeric': 0.14718019257221457, 'fraction_numerical': 0.0011004126547455295, 'mean_word_length': 2.8516949152542375, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4171944', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1217, 'n_words': 281}