helpers/helpers.js
function _groupBy (identifiers, groupByFields) {
  /* don't modify the input array */
  groupByFields = groupByFields.slice(0)

  groupByFields.forEach(function (group) {
    var groupValues = identifiers
      .filter(function (identifier) {
        /* exclude constructors from grouping.. re-implement to work off a `null` group value */
        return identifier.kind !== 'constructor'
      })
      .map(function (i) { return i[group] })
      .reduce(unique, [])
    if (groupValues.length <= 1) groupByFields = groupByFields.reduce(without(group), [])
  })
  identifiers = _addGroup(identifiers, groupByFields)

  var inserts = []
  var prevGroup = []
  var level = 0
  identifiers.forEach(function (identifier, index) {
    if (!deepEqual(identifier._group, prevGroup)) {
      var common = commonSequence(identifier._group, prevGroup)
      level = common.length
      identifier._group.forEach(function (group, i) {
        if (group !== common[i] && group !== null) {
          inserts.push({
            index: index,
            _title: group,
            level: level++
          })
        }
      })
    }
    identifier.level = level
    prevGroup = identifier._group
    delete identifier._group
  })

  /* insert title items */
  inserts.reverse().forEach(function (insert) {
    identifiers.splice(insert.index, 0, { _title: insert._title, level: insert.level })
  })
  return identifiers
}
function _groupBy (identifiers, groupByFields) {
  /* don't modify the input array */
  groupByFields = groupByFields.slice(0)

  groupByFields.forEach(function (group) {
    var groupValues = identifiers
      .filter(function (identifier) {
        /* exclude constructors from grouping.. re-implement to work off a `null` group value */
        return identifier.kind !== 'constructor'
      })
      .map(function (i) { return i[group] })
      .reduce(unique, [])
    if (groupValues.length <= 1) groupByFields = groupByFields.reduce(without(group), [])
  })
  identifiers = _addGroup(identifiers, groupByFields)

  var inserts = []
  var prevGroup = []
  var level = 0
  identifiers.forEach(function (identifier, index) {
    if (!deepEqual(identifier._group, prevGroup)) {
      var common = commonSequence(identifier._group, prevGroup)
      level = common.length
      identifier._group.forEach(function (group, i) {
        if (group !== common[i] && group !== null) {
          inserts.push({
            index: index,
            _title: group,
            level: level++
          })
        }
      })
    }
    identifier.level = level
    prevGroup = identifier._group
    delete identifier._group
  })

  /* insert title items */
  inserts.reverse().forEach(function (insert) {
    identifiers.splice(insert.index, 0, { _title: insert._title, level: insert.level })
  })
  return identifiers
}
function _groupBy ( identifiers , groupByFields ) { /* don't modify the input array */ groupByFields = groupByFields . slice ( 0 ) groupByFields . forEach ( function ( group ) { var groupValues = identifiers . filter ( function ( identifier ) { /* exclude constructors from grouping.. re-implement to work off a `null` group value */ return identifier . kind !== 'constructor' } ) . map ( function ( i ) { return i [ group ] } ) . reduce ( unique , [ ] ) if ( groupValues . length <= 1 ) groupByFields = groupByFields . reduce ( without ( group ) , [ ] ) } ) identifiers = _addGroup ( identifiers , groupByFields ) var inserts = [ ] var prevGroup = [ ] var level = 0 identifiers . forEach ( function ( identifier , index ) { if ( ! deepEqual ( identifier . _group , prevGroup ) ) { var common = commonSequence ( identifier . _group , prevGroup ) level = common . length identifier . _group . forEach ( function ( group , i ) { if ( group !== common [ i ] && group !== null ) { inserts . push ( { index : index , _title : group , level : level ++ } ) } } ) } identifier . level = level prevGroup = identifier . _group delete identifier . _group } ) /* insert title items */ inserts . reverse ( ) . forEach ( function ( insert ) { identifiers . splice ( insert . index , 0 , { _title : insert . _title , level : insert . level } ) } ) return identifiers }
takes the children of this, groups them, inserts group headings..
takes the children of this groups them inserts group headings ..