utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @desc AnimationFrame简单兼容hack
  3. */
  4. export const animationFrame = () => {
  5. window.cancelAnimationFrame = (function () {
  6. return (
  7. window.cancelAnimationFrame ||
  8. window.webkitCancelAnimationFrame ||
  9. window.mozCancelAnimationFrame ||
  10. window.oCancelAnimationFrame ||
  11. window.msCancelAnimationFrame ||
  12. function (id) {
  13. return window.clearTimeout(id);
  14. }
  15. );
  16. })();
  17. window.requestAnimationFrame = (function () {
  18. return (
  19. window.requestAnimationFrame ||
  20. window.webkitRequestAnimationFrame ||
  21. window.mozRequestAnimationFrame ||
  22. window.oRequestAnimationFrame ||
  23. window.msRequestAnimationFrame ||
  24. function (callback) {
  25. return window.setTimeout(callback, 1000 / 60);
  26. }
  27. );
  28. })();
  29. };
  30. /**
  31. * @desc 判断数组是否相等
  32. * @return {Boolean}
  33. * @param arr1
  34. * @param arr2
  35. */
  36. export const arrayEqual = (arr1: Array<any>, arr2: Array<any>) => {
  37. if (arr1 === arr2) return true;
  38. if (arr1.length !== arr2.length) return false;
  39. for (let i = 0; i < arr1.length; ++i) {
  40. if (arr1[i] !== arr2[i]) return false;
  41. }
  42. return true;
  43. };
  44. /**
  45. * @desc 深浅合并拷贝
  46. */
  47. export function copyObj() {
  48. if (!Array.isArray) {
  49. // @ts-expect-error
  50. Array.isArray = function (arg) {
  51. return Object.prototype.toString.call(arg) === "[object Array]";
  52. };
  53. }
  54. let name,
  55. options,
  56. src,
  57. copy,
  58. copyIsArray,
  59. clone,
  60. i = 1,
  61. // eslint-disable-next-line prefer-rest-params
  62. target = arguments[0] || {}, // 使用||运算符,排除隐式强制类型转换为false的数据类型
  63. deep = false,
  64. // eslint-disable-next-line prefer-const
  65. len = arguments.length;
  66. if (typeof target === "boolean") {
  67. deep = target;
  68. // eslint-disable-next-line prefer-rest-params
  69. target = arguments[1] || {};
  70. i++;
  71. }
  72. if (typeof target !== "object" && typeof target !== "function") {
  73. target = {};
  74. }
  75. // 如果arguments.length === 1 或typeof arguments[0] === 'boolean',且存在arguments[1],则直接返回target对象
  76. if (i === len) {
  77. return target;
  78. }
  79. for (; i < len; i++) {
  80. //所以如果源对象中数据类型为Undefined或Null那么就会跳过本次循环,接着循环下一个源对象
  81. // eslint-disable-next-line prefer-rest-params
  82. if ((options = arguments[i]) != null) {
  83. // 如果遇到源对象的数据类型为Boolean, Number for in循环会被跳过,不执行for in循环// src用于判断target对象是否存在name属性
  84. for (name in options) {
  85. // src用于判断target对象是否存在name属性
  86. src = target[name];
  87. // 需要复制的属性当前源对象的name属性
  88. copy = options[name];
  89. // 判断copy是否是数组
  90. copyIsArray = Array.isArray(copy);
  91. // 如果是深复制且copy是一个对象或数组则需要递归直到copy成为一个基本数据类型为止
  92. if (deep && copy && (typeof copy === "object" || copyIsArray)) {
  93. if (copyIsArray) {
  94. copyIsArray = false;
  95. // 如果目标对象存在name属性且是一个数组
  96. // 则使用目标对象的name属性,否则重新创建一个数组,用于复制
  97. clone = src && Array.isArray(src) ? src : [];
  98. } else {
  99. // 如果目标对象存在name属性且是一个对象则使用目标对象的name属性,否则重新创建一个对象,用于复制
  100. clone = src && typeof src === "object" ? src : {};
  101. }
  102. // 深复制,所以递归调用copyObject函数
  103. // 返回值为target对象,即clone对象
  104. // copy是一个源对象
  105. // @ts-expect-error
  106. target[name] = copyObj(deep, clone, copy);
  107. } else if (copy !== undefined) {
  108. // 浅复制,直接复制到target对象上
  109. target[name] = copy;
  110. }
  111. }
  112. }
  113. }
  114. return target;
  115. }