1 | import { curry } from 'ramda';
|
---|
2 |
|
---|
3 | import _makeFlat from './internal/makeFlat';
|
---|
4 |
|
---|
5 | const flatten1 = _makeFlat(false);
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Flattens the list to the specified depth.
|
---|
9 | *
|
---|
10 | * @func flattenDepth
|
---|
11 | * @memberOf RA
|
---|
12 | * @since {@link https://char0n.github.io/ramda-adjunct/2.19.0|v2.19.0}
|
---|
13 | * @category List
|
---|
14 | * @sig Number -> [a] -> [b]
|
---|
15 | * @param {!number} depth The maximum recursion depth
|
---|
16 | * @param {!Array} list The array to flatten
|
---|
17 | * @return {!Array} Returns the new flattened array
|
---|
18 | * @see {@link http://ramdajs.com/docs/#flatten|R.flatten}, {@link http://ramdajs.com/docs/#unnest|R.unnest}
|
---|
19 | * @example
|
---|
20 | *
|
---|
21 | * RA.flattenDepth(
|
---|
22 | * 2,
|
---|
23 | * [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10]
|
---|
24 | * ); //=> [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10];
|
---|
25 | */
|
---|
26 | const flattenDepth = curry((depth, list) => {
|
---|
27 | let currentDept = depth;
|
---|
28 | let flatList = [...list];
|
---|
29 |
|
---|
30 | while (currentDept > 0) {
|
---|
31 | flatList = flatten1(flatList);
|
---|
32 |
|
---|
33 | currentDept -= 1;
|
---|
34 | }
|
---|
35 |
|
---|
36 | return flatList;
|
---|
37 | });
|
---|
38 |
|
---|
39 | export default flattenDepth;
|
---|