No Description

_flatten.js 1014B

123456789101112131415161718192021222324252627282930313233
  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;