Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

_flatten.js 986B

12345678910111213141516171819202122232425262728293031
  1. import getLength from './_getLength.js';
  2. import isArrayLike from './_isArrayLike.js';
  3. import isArray from './isArray.js';
  4. import isArguments from './isArguments.js';
  5. // Internal implementation of a recursive `flatten` function.
  6. export default 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. }