index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const utils = {
  2. throttle: function (fn, interval) {
  3. let lastRunTime = 0
  4. return function (...args) {
  5. let elapsedTime = Date.now() - lastRunTime
  6. if (elapsedTime < interval) {
  7. return
  8. }
  9. let context = this
  10. lastRunTime = Date.now()
  11. fn.apply(context, args)
  12. }
  13. },
  14. getImageUrl(name) {
  15. return new URL(`../assets/images/${name}`, import.meta.url).href
  16. },
  17. getQueryByName(key) {
  18. let querys = window.location.search.substring(1).split('&')
  19. for (let i = 0; i < querys.length; i++) {
  20. let keypair = querys[i].split('=')
  21. if (keypair.length === 2 && keypair[0] === key) {
  22. try {
  23. return decodeURIComponent(keypair[1])
  24. } catch (error) {
  25. return keypair[1]
  26. }
  27. }
  28. }
  29. return ''
  30. }
  31. }
  32. /**
  33. * 获取忽略指定属性的对象
  34. * @param {Object} obj 源对象
  35. * @param {...any} props 忽略属性
  36. */
  37. export function omit(obj, ...props) {
  38. const result = { ...obj }
  39. props.forEach(function (prop) {
  40. delete result[prop]
  41. })
  42. return result
  43. }
  44. export const objectToString = Object.prototype.toString
  45. export const toTypeString = value => objectToString.call(value)
  46. // 获取制定对象的类型比如toRawType(1) Number
  47. export const toRawType = value => toTypeString(value).slice(8, -1)
  48. /**
  49. * 判断是否函数
  50. * @param {any} target 参数对象
  51. */
  52. export const isFunction = target => toRawType(target) === 'Function'
  53. export default utils