You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flatten.js 671B

1234567891011121314151617181920212223242526
  1. const isArrayLike = require('./is-array-like');
  2. const isArguments = require('./is-arguments');
  3. const forEach = require('./for-each');
  4. const flatten = (input, shallow, strict, output = []) => {
  5. let idx = output.length;
  6. forEach(input, value => {
  7. if (isArrayLike(value) && (Array.isArray(value) || isArguments(value))) {
  8. if (shallow) {
  9. let j = 0;
  10. const len = value.length;
  11. while (j < len) output[idx++] = value[j++];
  12. } else {
  13. flatten(value, shallow, strict, output);
  14. idx = output.length;
  15. }
  16. } else if (!strict) {
  17. output[idx++] = value;
  18. }
  19. });
  20. return output;
  21. };
  22. module.exports = (array, shallow) => flatten(array, shallow, false);