_flatten.js 1014 B

12345678910111213141516171819202122232425262728293031323334
  1. var _getLength = require('./_getLength.js');
  2. var _isArrayLike = require('./_isArrayLike.js');
  3. var isArray = require('./isArray.js');
  4. var isArguments = require('./isArguments.js');
  5. // Internal implementation of a recursive `flatten` function.
  6. function flatten(input, depth, strict, output) {
  7. output = output || [];
  8. if (!depth && depth !== 0) {
  9. depth = Infinity;
  10. } else if (depth <= 0) {
  11. return output.concat(input);
  12. }
  13. var idx = output.length;
  14. for (var i = 0, length = _getLength(input); i < length; i++) {
  15. var value = input[i];
  16. if (_isArrayLike(value) && (isArray(value) || isArguments(value))) {
  17. // Flatten current level of array or arguments object.
  18. if (depth > 1) {
  19. flatten(value, depth - 1, strict, output);
  20. idx = output.length;
  21. } else {
  22. var j = 0, len = value.length;
  23. while (j < len) output[idx++] = value[j++];
  24. }
  25. } else if (!strict) {
  26. output[idx++] = value;
  27. }
  28. }
  29. return output;
  30. }
  31. module.exports = flatten;