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.

index.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var shallowProperty = key => obj => obj == null ? void 0 : obj[key];
  2. var getLength = shallowProperty('length');
  3. const MAX_ARRAY_INDEX = 2 ** 53 - 1;
  4. var isArrayLike = (collection) => {
  5. const length = getLength(collection);
  6. return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
  7. };
  8. var isArguments = obj => toString.call(obj) === '[object Arguments]';
  9. var isObject = obj => {
  10. const type = typeof obj;
  11. return type === 'function' || type === 'object' && !!obj;
  12. };
  13. var getKeys = (obj) => {
  14. if (!isObject(obj)) return [];
  15. return Object.keys(obj);
  16. };
  17. var optimizeCb = (func, context, argCount) => {
  18. if (context === void 0) return func;
  19. switch (argCount == null ? 3 : argCount) {
  20. case 1: return value => func.call(context, value);
  21. // The 2-argument case is omitted because we’re not using it.
  22. case 3: return (value, index, collection) => func.call(context, value, index, collection);
  23. case 4: return (accumulator, value, index, collection) => func.call(context, accumulator, value, index, collection);
  24. }
  25. return (...args) => func.apply(context, args);
  26. };
  27. var forEach = (obj, iteratee, context) => {
  28. iteratee = optimizeCb(iteratee, context);
  29. if (isArrayLike(obj)) {
  30. let i = 0;
  31. for (const item of obj) {
  32. iteratee(item, i++, obj);
  33. }
  34. } else {
  35. const keys = getKeys(obj);
  36. for (const key of keys) {
  37. iteratee(obj[key], key, obj);
  38. }
  39. }
  40. return obj;
  41. };
  42. const flatten = (input, shallow, strict, output = []) => {
  43. let idx = output.length;
  44. forEach(input, value => {
  45. if (isArrayLike(value) && (Array.isArray(value) || isArguments(value))) {
  46. if (shallow) {
  47. let j = 0;
  48. const len = value.length;
  49. while (j < len) output[idx++] = value[j++];
  50. } else {
  51. flatten(value, shallow, strict, output);
  52. idx = output.length;
  53. }
  54. } else if (!strict) {
  55. output[idx++] = value;
  56. }
  57. });
  58. return output;
  59. };
  60. var flatten_1 = (array, shallow) => flatten(array, shallow, false);
  61. module.exports = flatten_1;