baseFlatten.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var arrayPush = require('./arrayPush'),
  2. isArguments = require('../lang/isArguments'),
  3. isArray = require('../lang/isArray'),
  4. isArrayLike = require('./isArrayLike'),
  5. isObjectLike = require('./isObjectLike');
  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-like objects.
  14. * @param {Array} [result=[]] The initial result value.
  15. * @returns {Array} Returns the new flattened array.
  16. */
  17. function baseFlatten(array, isDeep, isStrict, result) {
  18. result || (result = []);
  19. var index = -1,
  20. length = array.length;
  21. while (++index < length) {
  22. var value = array[index];
  23. if (isObjectLike(value) && isArrayLike(value) &&
  24. (isStrict || isArray(value) || isArguments(value))) {
  25. if (isDeep) {
  26. // Recursively flatten arrays (susceptible to call stack limits).
  27. baseFlatten(value, isDeep, isStrict, result);
  28. } else {
  29. arrayPush(result, value);
  30. }
  31. } else if (!isStrict) {
  32. result[result.length] = value;
  33. }
  34. }
  35. return result;
  36. }
  37. module.exports = baseFlatten;