UNPKG

ramda-adjunct

Version:

Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.

46 lines (36 loc) 1.55 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } import { curry } from 'ramda'; import _makeFlat from 'ramda/src/internal/_makeFlat'; var flatten1 = _makeFlat(false); /** * Flattens the list to the specified depth. * * @func flattenDepth * @memberOf RA * @since {@link https://char0n.github.io/ramda-adjunct/2.19.0|v2.19.0} * @category List * @sig Number -> [a] -> [b] * @param {!number} depth The maximum recursion depth * @param {!Array} list The array to flatten * @return {!Array} Returns the new flattened array * @see {@link http://ramdajs.com/docs/#flatten|R.flatten}, {@link http://ramdajs.com/docs/#unnest|R.unnest} * @example * * RA.flattenDepth( * 2, * [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10] * ); //=> [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10]; */ var flattenDepth = curry(function (depth, list) { var currentDept = depth; var flatList = _toConsumableArray(list); while (currentDept > 0) { flatList = flatten1(flatList); currentDept -= 1; } return flatList; }); export default flattenDepth;