util.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * 工具类
  3. */
  4. /**
  5. * 格式化日期格式 (用于兼容ios Date对象)
  6. */
  7. export const formatDate = (time) => {
  8. // 将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
  9. return time.replace(/\-/g, "/");
  10. }
  11. /**
  12. * 对象转URL
  13. * @param {object} obj
  14. */
  15. export const urlEncode = (obj = {}) => {
  16. const result = []
  17. for (const key in obj) {
  18. const item = obj[key]
  19. if (!item) {
  20. continue
  21. }
  22. if (isArray(item)) {
  23. item.forEach(val => {
  24. result.push(key + '=' + val)
  25. })
  26. } else {
  27. result.push(key + '=' + item)
  28. }
  29. }
  30. return result.join('&')
  31. }
  32. /**
  33. * 遍历对象
  34. */
  35. export const objForEach = (obj, callback) => {
  36. Object.keys(obj).forEach((key) => {
  37. callback(obj[key], key)
  38. });
  39. }
  40. /**
  41. * 是否在数组内
  42. */
  43. export const inArray = (search, array) => {
  44. for (var i in array) {
  45. if (array[i] == search) return true
  46. }
  47. return false
  48. }
  49. /**
  50. * 对Date的扩展,将 Date 转化为指定格式的String
  51. * 月(Y)、月(m)、日(d)、小时(H)、分(M)、秒(S) 可以用 1-2 个占位符,
  52. * 例子:
  53. * dateFormat('YYYY-mm-dd HH:MM:SS', new Date()) ==> 2020-01-01 08:00:00
  54. */
  55. export const dateFormat = (fmt, date) => {
  56. const opt = {
  57. "Y+": date.getFullYear().toString(), // 年
  58. "m+": (date.getMonth() + 1).toString(), // 月
  59. "d+": date.getDate().toString(), // 日
  60. "H+": date.getHours().toString(), // 时
  61. "M+": date.getMinutes().toString(), // 分
  62. "S+": date.getSeconds().toString() // 秒
  63. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  64. };
  65. let ret
  66. for (let k in opt) {
  67. ret = new RegExp("(" + k + ")").exec(fmt)
  68. if (ret) {
  69. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  70. };
  71. };
  72. return fmt
  73. }
  74. /**
  75. * 判断是否为空对象
  76. * @param {*} object 源对象
  77. */
  78. export const isEmptyObject = (object) => {
  79. return Object.keys(object).length === 0
  80. }
  81. /**
  82. * 判断是否为对象
  83. * @param {*} object
  84. */
  85. export const isObject = (object) => {
  86. return Object.prototype.toString.call(object) === '[object Object]'
  87. }
  88. /**
  89. * 判断是否为数组
  90. * @param {*} array
  91. */
  92. export const isArray = (array) => {
  93. return Object.prototype.toString.call(array) === '[object Array]'
  94. }
  95. /**
  96. * 判断是否为空
  97. * @param {*} object 源对象
  98. */
  99. export const isEmpty = (value) => {
  100. if (isArray(value)) {
  101. return value.length === 0
  102. }
  103. if (isObject(value)) {
  104. return isEmptyObject(value)
  105. }
  106. return !value
  107. }
  108. /**
  109. * 对象深拷贝
  110. * @param {*} obj 源对象
  111. */
  112. export const cloneObj = (obj) => {
  113. let newObj = obj.constructor === Array ? [] : {};
  114. if (typeof obj !== 'object') {
  115. return;
  116. }
  117. for (let i in obj) {
  118. newObj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i];
  119. }
  120. return newObj
  121. }
  122. // 节流函数
  123. // 思路: 第一次先设定一个变量true,
  124. // 第二次执行这个函数时,会判断变量是否true,
  125. // 是则返回。当第一次的定时器执行完函数最后会设定变量为flase。
  126. // 那么下次判断变量时则为flase,函数会依次运行。
  127. export function throttle(fn, delay = 100) {
  128. // 首先设定一个变量,在没有执行我们的定时器时为null
  129. var timer = null
  130. return function() {
  131. // 当我们发现这个定时器存在时,则表示定时器已经在运行中,需要返回
  132. if (timer) return
  133. timer = setTimeout(() => {
  134. fn.apply(this, arguments)
  135. timer = null
  136. }, delay)
  137. }
  138. }
  139. // 防抖函数
  140. // 首次运行时把定时器赋值给一个变量, 第二次执行时,
  141. // 如果间隔没超过定时器设定的时间则会清除掉定时器,
  142. // 重新设定定时器, 依次反复, 当我们停止下来时,
  143. // 没有执行清除定时器, 超过一定时间后触发回调函数。
  144. // 参考文档:https://segmentfault.com/q/1010000021145192
  145. export function debounce(fn, delay) {
  146. let timer
  147. return function() {
  148. const that = this
  149. const _args = arguments // 存一下传入的参数
  150. if (timer) {
  151. clearTimeout(timer)
  152. }
  153. timer = setTimeout(function() {
  154. fn.apply(that, _args)
  155. }, delay)
  156. }
  157. }
  158. /**
  159. * 数组交集
  160. * @param {Array} 数组1
  161. * @param {Array} 数组2
  162. * @return {Array}
  163. */
  164. export const arrayIntersect = (array1, array2) => {
  165. return array1.filter(val => array2.indexOf(val) > -1)
  166. }