CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
array-flatten.js65 linesDownload Raw Back to array-flatten
1'use strict'2 3/**4 * Expose `arrayFlatten`.5 */6module.exports = arrayFlatten7 8/**9 * Recursive flatten function with depth.10 *11 * @param  {Array}  array12 * @param  {Array}  result13 * @param  {Number} depth14 * @return {Array}15 */16function flattenWithDepth (array, result, depth) {17  for (var i = 0; i < array.length; i++) {18    var value = array[i]19 20    if (depth > 0 && Array.isArray(value)) {21      flattenWithDepth(value, result, depth - 1)22    } else {23      result.push(value)24    }25  }26 27  return result28}29 30/**31 * Recursive flatten function. Omitting depth is slightly faster.32 *33 * @param  {Array} array34 * @param  {Array} result35 * @return {Array}36 */37function flattenForever (array, result) {38  for (var i = 0; i < array.length; i++) {39    var value = array[i]40 41    if (Array.isArray(value)) {42      flattenForever(value, result)43    } else {44      result.push(value)45    }46  }47 48  return result49}50 51/**52 * Flatten an array, with the ability to define a depth.53 *54 * @param  {Array}  array55 * @param  {Number} depth56 * @return {Array}57 */58function arrayFlatten (array, depth) {59  if (depth == null) {60    return flattenForever(array, [])61  }62 63  return flattenWithDepth(array, [], depth)64}65