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.

135 lines
3.4 KiB

3 years ago
  1. 'use strict'
  2. // utils is a library of generic helper functions non-specific to axios
  3. var toString = Object.prototype.toString
  4. /**
  5. * Determine if a value is an Array
  6. *
  7. * @param {Object} val The value to test
  8. * @returns {boolean} True if value is an Array, otherwise false
  9. */
  10. export function isArray (val) {
  11. return toString.call(val) === '[object Array]'
  12. }
  13. /**
  14. * Determine if a value is an Object
  15. *
  16. * @param {Object} val The value to test
  17. * @returns {boolean} True if value is an Object, otherwise false
  18. */
  19. export function isObject (val) {
  20. return val !== null && typeof val === 'object'
  21. }
  22. /**
  23. * Determine if a value is a Date
  24. *
  25. * @param {Object} val The value to test
  26. * @returns {boolean} True if value is a Date, otherwise false
  27. */
  28. export function isDate (val) {
  29. return toString.call(val) === '[object Date]'
  30. }
  31. /**
  32. * Determine if a value is a URLSearchParams object
  33. *
  34. * @param {Object} val The value to test
  35. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  36. */
  37. export function isURLSearchParams (val) {
  38. return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams
  39. }
  40. /**
  41. * Iterate over an Array or an Object invoking a function for each item.
  42. *
  43. * If `obj` is an Array callback will be called passing
  44. * the value, index, and complete array for each item.
  45. *
  46. * If 'obj' is an Object callback will be called passing
  47. * the value, key, and complete object for each property.
  48. *
  49. * @param {Object|Array} obj The object to iterate
  50. * @param {Function} fn The callback to invoke for each item
  51. */
  52. export function forEach (obj, fn) {
  53. // Don't bother if no value provided
  54. if (obj === null || typeof obj === 'undefined') {
  55. return
  56. }
  57. // Force an array if not already something iterable
  58. if (typeof obj !== 'object') {
  59. /*eslint no-param-reassign:0*/
  60. obj = [obj]
  61. }
  62. if (isArray(obj)) {
  63. // Iterate over array values
  64. for (var i = 0, l = obj.length; i < l; i++) {
  65. fn.call(null, obj[i], i, obj)
  66. }
  67. } else {
  68. // Iterate over object keys
  69. for (var key in obj) {
  70. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  71. fn.call(null, obj[key], key, obj)
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * 是否为boolean
  78. * @param val
  79. * @returns {boolean}
  80. */
  81. export function isBoolean(val) {
  82. return typeof val === 'boolean'
  83. }
  84. /**
  85. * 是否为真正的对象{} new Object
  86. * @param {any} obj - 检测的对象
  87. * @returns {boolean}
  88. */
  89. export function isPlainObject(obj) {
  90. return Object.prototype.toString.call(obj) === '[object Object]'
  91. }
  92. /**
  93. * Function equal to merge with the difference being that no reference
  94. * to original objects is kept.
  95. *
  96. * @see merge
  97. * @param {Object} obj1 Object to merge
  98. * @returns {Object} Result of all merge properties
  99. */
  100. export function deepMerge(/* obj1, obj2, obj3, ... */) {
  101. let result = {}
  102. function assignValue(val, key) {
  103. if (typeof result[key] === 'object' && typeof val === 'object') {
  104. result[key] = deepMerge(result[key], val)
  105. } else if (typeof val === 'object') {
  106. result[key] = deepMerge({}, val)
  107. } else {
  108. result[key] = val
  109. }
  110. }
  111. for (let i = 0, l = arguments.length; i < l; i++) {
  112. forEach(arguments[i], assignValue)
  113. }
  114. return result
  115. }
  116. export function isUndefined (val) {
  117. return typeof val === 'undefined'
  118. }