'use strict'

const takeWhile = require('./take-while.js')

module.exports = tree

/**
 * Creates a tree of nested arrays based on `arr` data and `fn` criteria.
 * Elements are compared against their left neighbour using `fn` which is
 * invoked with these arguments: (current, next). If the invocation returns
 * truthy `next`, will be nested into `current`, `next` will be placed at the
 * same level of`current` otherwise.
 *
 * @static
 * @param {Array} arr The array to structure as tree.
 * @param {Function} fn The function invoked per item in the array.
 * @returns {Array} Returns an array of arrays representing the tree.
 * @example
 *
 * const levels = [
 *   5,
 *   3,
 *   2,
 *   5,
 *   4
 * ]
 *
 * function comparator (current, next) {
 *   return next < current
 * }
 *
 * tree(levels, comparator)
 * // => [ [ 5, [ [ 3, [ [ 2 ] ] ] ] ], [ 5, [ [ 4 ] ] ] ]
 */
function tree ([current, ...rest], fn) {
  const children = takeWhile(fn.bind(null, current), rest)
  const result = children.length ? [current, tree(children, fn)] : [current]

  return rest.length - children.length
    ? [result, ...tree(rest.slice(children.length), fn)]
    : [result]
}

@@ -317,16 +317,16 @@
 uthy
+,
  %60next%60
-,
  wil
@@ -398,16 +398,17 @@
 level of
+ 
 %60current

Fix some doc typos in tree.js