react-native-navigation的迁移库

utils.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var INFINITY = 1 / 0;
  2. var MAX_SAFE_INTEGER = 9007199254740991;
  3. function flattenDeep(array) {
  4. var length = array ? array.length : 0;
  5. return length ? baseFlatten(array, INFINITY) : [];
  6. }
  7. function baseFlatten(array, depth, isStrict, result) {
  8. result || (result = []);
  9. var index = -1, length = array.length;
  10. while (++index < length) {
  11. var value = array[index];
  12. if (depth > 0 && isArrayLikeObject(value) &&
  13. (isStrict || isArray(value) || isArguments(value))) {
  14. if (depth > 1) {
  15. // Recursively flatten arrays (susceptible to call stack limits).
  16. baseFlatten(value, depth - 1, isStrict, result);
  17. } else {
  18. arrayPush(result, value);
  19. }
  20. } else if (!isStrict) {
  21. result[result.length] = value;
  22. }
  23. }
  24. return result;
  25. }
  26. function isArguments(value) {
  27. // Safari 8.1 incorrectly makes `arguments.callee` enumerable in strict mode.
  28. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  29. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  30. }
  31. var isArray = Array.isArray;
  32. function isArrayLikeObject(value) {
  33. return isObjectLike(value) && isArrayLike(value);
  34. }
  35. function isObjectLike(value) {
  36. return !!value && typeof value == 'object';
  37. }
  38. function isArrayLike(value) {
  39. return value != null &&
  40. !(typeof value == 'function' && isFunction(value)) && isLength(getLength(value));
  41. }
  42. function isFunction(value) {
  43. // The use of `Object#toString` avoids issues with the `typeof` operator
  44. // in Safari 8 which returns 'object' for typed array constructors, and
  45. // PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  46. var tag = isObject(value) ? objectToString.call(value) : '';
  47. return tag == funcTag || tag == genTag;
  48. }
  49. function isLength(value) {
  50. return typeof value == 'number' &&
  51. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  52. }
  53. var getLength = baseProperty('length');
  54. function baseProperty(key) {
  55. return function(object) {
  56. return object == null ? undefined : object[key];
  57. };
  58. }
  59. function arrayPush(array, values) {
  60. var index = -1, length = values.length, offset = array.length;
  61. while (++index < length) {
  62. array[offset + index] = values[index];
  63. }
  64. return array;
  65. }
  66. module.exports = {
  67. flattenDeep: flattenDeep
  68. };