main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
947 bytes
|
Line | |
---|
1 | import isArrayLike from '../isArrayLike';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * `makeFlat` is a helper function that returns a one-level or fully recursive
|
---|
5 | * function based on the flag passed in.
|
---|
6 | *
|
---|
7 | * @func makeFlat
|
---|
8 | * @memberOf RA
|
---|
9 | *
|
---|
10 | * @category List
|
---|
11 | * @param {!bool} = should recursively flatten
|
---|
12 | * @param {!Array} = the nested list to be flattened
|
---|
13 | * @return {!Array} = the flattened list
|
---|
14 | * @sig Bool -> List -> List
|
---|
15 | *
|
---|
16 | */
|
---|
17 | const makeFlat = (recursive) =>
|
---|
18 | function flatt(list) {
|
---|
19 | let value;
|
---|
20 | let jlen;
|
---|
21 | let j;
|
---|
22 | const result = [];
|
---|
23 | let idx = 0;
|
---|
24 |
|
---|
25 | while (idx < list.length) {
|
---|
26 | if (isArrayLike(list[idx])) {
|
---|
27 | value = recursive ? flatt(list[idx]) : list[idx];
|
---|
28 | j = 0;
|
---|
29 | jlen = value.length;
|
---|
30 | while (j < jlen) {
|
---|
31 | result[result.length] = value[j];
|
---|
32 | j += 1;
|
---|
33 | }
|
---|
34 | } else {
|
---|
35 | result[result.length] = list[idx];
|
---|
36 | }
|
---|
37 | idx += 1;
|
---|
38 | }
|
---|
39 | return result;
|
---|
40 | };
|
---|
41 |
|
---|
42 | export default makeFlat;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.