utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. export const debounce = function (func, wait = 500) {
  2. let timerId;
  3. return function (...rest) {
  4. if (timerId) {
  5. clearTimeout(timerId);
  6. }
  7. timerId = setTimeout(() => {
  8. func.apply(this, rest);
  9. }, wait);
  10. };
  11. };
  12. export const classNames = function (...args) {
  13. const hasOwn = {}.hasOwnProperty;
  14. const classes = [];
  15. args.forEach((arg) => {
  16. if (!arg)
  17. return;
  18. const argType = typeof arg;
  19. if (argType === 'string' || argType === 'number') {
  20. classes.push(arg);
  21. }
  22. else if (Array.isArray(arg) && arg.length) {
  23. const inner = classNames(...arg);
  24. if (inner) {
  25. classes.push(inner);
  26. }
  27. }
  28. else if (argType === 'object') {
  29. for (const key in arg) {
  30. if (hasOwn.call(arg, key) && arg[key]) {
  31. classes.push(key);
  32. }
  33. }
  34. }
  35. });
  36. return classes.join(' ');
  37. };
  38. export const styles = function (styleObj) {
  39. return Object.keys(styleObj)
  40. .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`)
  41. .join('; ');
  42. };
  43. export const getAnimationFrame = function (cb) {
  44. return wx
  45. .createSelectorQuery()
  46. .selectViewport()
  47. .boundingClientRect()
  48. .exec(() => {
  49. cb();
  50. });
  51. };
  52. export const getRect = function (context, selector) {
  53. return new Promise((resolve) => {
  54. wx.createSelectorQuery()
  55. .in(context)
  56. .select(selector)
  57. .boundingClientRect()
  58. .exec((rect = []) => resolve(rect[0]));
  59. });
  60. };
  61. const isDef = function (value) {
  62. return value !== undefined && value !== null;
  63. };
  64. export const isNumber = function (value) {
  65. return /^\d+(\.\d+)?$/.test(value);
  66. };
  67. export const addUnit = function (value) {
  68. if (!isDef(value)) {
  69. return undefined;
  70. }
  71. value = String(value);
  72. return isNumber(value) ? `${value}px` : value;
  73. };
  74. export const getCharacterLength = (type, str, max) => {
  75. if (!str || str.length === 0) {
  76. return {
  77. length: 0,
  78. characters: '',
  79. };
  80. }
  81. if (type === 'maxcharacter') {
  82. let len = 0;
  83. for (let i = 0; i < str.length; i += 1) {
  84. let currentStringLength = 0;
  85. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  86. currentStringLength = 2;
  87. }
  88. else {
  89. currentStringLength = 1;
  90. }
  91. if (len + currentStringLength > max) {
  92. return {
  93. length: len,
  94. characters: str.slice(0, i),
  95. };
  96. }
  97. len += currentStringLength;
  98. }
  99. return {
  100. length: len,
  101. characters: str,
  102. };
  103. }
  104. else if (type === 'maxlength') {
  105. const length = str.length > max ? max : str.length;
  106. return {
  107. length,
  108. characters: str.slice(0, length),
  109. };
  110. }
  111. return {
  112. length: str.length,
  113. characters: str,
  114. };
  115. };
  116. export const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
  117. export const equal = (v1, v2) => {
  118. if (Array.isArray(v1) && Array.isArray(v2)) {
  119. if (v1.length !== v2.length)
  120. return false;
  121. return v1.every((item, index) => equal(item, v2[index]));
  122. }
  123. return v1 === v2;
  124. };
  125. export const clone = (val) => {
  126. if (Array.isArray(val)) {
  127. return val.map((item) => clone(item));
  128. }
  129. return val;
  130. };
  131. export const getInstance = function (context, selector) {
  132. if (!context) {
  133. const pages = getCurrentPages();
  134. const page = pages[pages.length - 1];
  135. context = page.$$basePage || page;
  136. }
  137. const instance = context ? context.selectComponent(selector) : null;
  138. if (!instance) {
  139. console.warn('未找到组件,请检查selector是否正确');
  140. return null;
  141. }
  142. return instance;
  143. };