function flatten (array, isRectangular = false) {
  if (!Array.isArray(array)) {
    // if not an array, return as is
    return array
  }
  if (typeof isRectangular !== 'boolean') {
    throw new TypeError('Boolean expected for second argument of flatten')
  }
  const flat = []

  if (isRectangular) {
    _flattenRectangular(array)
  } else {
    _flatten(array)
  }

  return flat

  function _flatten (array) {
    for (let i = 0; i < array.length; i++) {
      const item = array[i]
      if (Array.isArray(item)) {
        _flatten(item)
      } else {
        flat.push(item)
      }
    }
  }

  function _flattenRectangular (array) {
    if (Array.isArray(array[0])) {
      for (let i = 0; i < array.length; i++) {
        _flattenRectangular(array[i])
      }
    } else {
      for (let i = 0; i < array.length; i++) {
        flat.push(array[i])
      }
    }
  }
}
Flatten a multi dimensional array, put all elements in a one dimensional
array
@param {Array} array   A multi dimensional array
@param {boolean} isRectangular Optional. If the array is rectangular (not jagged)
@return {Array}        The flattened array (1 dimensional)