util.js

  1. /* eslint-disable no-unused-vars */
  2. /**
  3. * 通用函数。
  4. * @module common
  5. */
  6. /**
  7. * @static
  8. * @description 函数柯里化。
  9. * @example
  10. * function add(a, b) {
  11. * return a + b
  12. * }
  13. * var curriedAdd = curry(add)
  14. * var addFive = curriedAdd(5)
  15. * var result = [0, 1, 2, 3, 4, 5].map(addFive)
  16. * // [5, 6, 7, 8, 9, 10]
  17. * @param {function} fn 需要柯里化的函数,函数“无默认值的参数”个数不为0。
  18. * @return {function} 柯里化后的函数。
  19. */
  20. function curry (fn) {
  21. // fn的无默认值参数的个数
  22. if (fn.length === 0) {
  23. return fn
  24. }
  25. function _curried (depth, args) {
  26. return function (newArgument) {
  27. // 需要传入fn需要的参数个数,才会返回值
  28. if (depth - 1 === 0) {
  29. return fn(...args, newArgument)
  30. }
  31. return _curried(depth - 1, [...args, newArgument])
  32. }
  33. }
  34. return _curried(fn.length, [])
  35. }
  36. /**
  37. * @static
  38. * @description 最大重复数数量
  39. * @example
  40. * countMaxDuplicateNumber([1,2,2,5,5,5])
  41. * // 3
  42. * @param {Array<Number>} array 数字数组
  43. * @return {Number} 数量
  44. */
  45. function countMaxDuplicateNumber (array) {
  46. if (!array.length) {
  47. return 0
  48. }
  49. const sortedAry = array.sort((a, b) => {
  50. return a - b
  51. })
  52. let count = 1
  53. let max = 1
  54. for (let i = 1; i < sortedAry.length; i++) {
  55. if (sortedAry[i - 1] === sortedAry[i]) {
  56. max++
  57. } else {
  58. if (max > count) {
  59. count = max
  60. }
  61. max = 1
  62. }
  63. }
  64. return count
  65. }
  66. /**
  67. * @static
  68. * @description requestAnimationFrame 节流函数。
  69. * @param {Function} fn 数字数组。
  70. * @return {Function} 节流函数。
  71. */
  72. function throttle (fn) {
  73. let curTick = false
  74. const that = this
  75. const params = [...arguments]
  76. params.shift()
  77. return function () {
  78. const curParams = [...arguments]
  79. if (!curTick) {
  80. curTick = true
  81. requestAnimationFrame(() => {
  82. fn.apply(that, [...curParams, params])
  83. curTick = false
  84. })
  85. }
  86. }
  87. }
  88. /**
  89. * @static
  90. * @description 依次按序执行 promise,全部调用完毕后返回一个新的 promise。
  91. * @example
  92. * function getA() {
  93. * return new Promise((resolve, reject) => {
  94. * get('/user/a', (status, res) => {
  95. * if (status == 200) {
  96. * resolve(res)
  97. * } else {
  98. * reject('error')
  99. * }
  100. * })
  101. * })
  102. * }
  103. *
  104. * function getB(dataA) {
  105. * return new Promise((resolve, reject) => {
  106. * get('/user/b', dataA, (status, res) => {
  107. * if (status == 200) {
  108. * resolve(res)
  109. * } else {
  110. * reject('error')
  111. * }
  112. * })
  113. *
  114. * })
  115. * }
  116. *
  117. * function getC(dataB) {
  118. * return new Promise((resolve, reject) => {
  119. * get('/user/c', dataB, (status, res) => {
  120. * if (status == 200) {
  121. * resolve(res)
  122. * } else {
  123. * reject('error')
  124. * }
  125. * })
  126. *
  127. * })
  128. * }
  129. *
  130. * promiseOrder([getA,getB,getC]).then((res)=>{
  131. * console.log('success');
  132. * console.log(res);
  133. * })
  134. * @param {Arry} pFAry 数组,元素是函数,函数返回 promise 对象。
  135. * 函数接收上一次 promise resolve 的参数。
  136. * @returns {Promise} 新的 fulfilled 状态 promise 对象。
  137. * 传递最后一次 promise resolve 的参数。
  138. */
  139. function promiseOrder (pFAry) {
  140. return new Promise((resolve, reject) => {
  141. let promise = Promise.resolve()
  142. // 由于要最后一个promise执行完毕,才resolve,这里需要pFAry.length + 1
  143. for (let i = 0; i < pFAry.length + 1; i++) {
  144. promise = promise
  145. .then((res) => {
  146. if (i === pFAry.length) {
  147. resolve(res)
  148. } else {
  149. return pFAry[i](res)
  150. }
  151. })
  152. .catch((error) => {
  153. reject(error)
  154. // 捕获错误后由于下一次循环依然会添加then,需要返回一个pending状态的promise来中断后面的所有操作
  155. return new Promise(() => {})
  156. })
  157. }
  158. })
  159. }
  160. export {
  161. curry,
  162. throttle,
  163. countMaxDuplicateNumber,
  164. promiseOrder
  165. }