2a5421c975e074bc39dff2283bb87a34c6d87c10
[platform/framework/web/crosswalk-tizen.git] /
1 var isArguments = require('../lang/isArguments'),
2     isArray = require('../lang/isArray'),
3     isLength = require('./isLength'),
4     isObjectLike = require('./isObjectLike');
5
6 /**
7  * The base implementation of `_.flatten` with added support for restricting
8  * flattening and specifying the start index.
9  *
10  * @private
11  * @param {Array} array The array to flatten.
12  * @param {boolean} isDeep Specify a deep flatten.
13  * @param {boolean} isStrict Restrict flattening to arrays and `arguments` objects.
14  * @returns {Array} Returns the new flattened array.
15  */
16 function baseFlatten(array, isDeep, isStrict) {
17   var index = -1,
18       length = array.length,
19       resIndex = -1,
20       result = [];
21
22   while (++index < length) {
23     var value = array[index];
24
25     if (isObjectLike(value) && isLength(value.length) && (isArray(value) || isArguments(value))) {
26       if (isDeep) {
27         // Recursively flatten arrays (susceptible to call stack limits).
28         value = baseFlatten(value, isDeep, isStrict);
29       }
30       var valIndex = -1,
31           valLength = value.length;
32
33       result.length += valLength;
34       while (++valIndex < valLength) {
35         result[++resIndex] = value[valIndex];
36       }
37     } else if (!isStrict) {
38       result[++resIndex] = value;
39     }
40   }
41   return result;
42 }
43
44 module.exports = baseFlatten;