utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // for (let i = 0; i < args.length; i++) {
  17. // eslint-disable-next-line
  18. // const arg = args[i]
  19. if (!arg)
  20. return;
  21. const argType = typeof arg;
  22. if (argType === 'string' || argType === 'number') {
  23. classes.push(arg);
  24. }
  25. else if (Array.isArray(arg) && arg.length) {
  26. const inner = classNames(...arg);
  27. if (inner) {
  28. classes.push(inner);
  29. }
  30. }
  31. else if (argType === 'object') {
  32. // eslint-disable-next-line
  33. for (const key in arg) {
  34. if (hasOwn.call(arg, key) && arg[key]) {
  35. classes.push(key);
  36. }
  37. }
  38. }
  39. });
  40. return classes.join(' ');
  41. };
  42. export const styles = function (styleObj) {
  43. return Object.keys(styleObj)
  44. .map((styleKey) => `${styleKey}: ${styleObj[styleKey]}`)
  45. .join('; ');
  46. };
  47. export const requestAnimationFrame = function (cb) {
  48. return wx
  49. .createSelectorQuery()
  50. .selectViewport()
  51. .boundingClientRect()
  52. .exec(() => {
  53. cb();
  54. });
  55. };
  56. const isDef = function (value) {
  57. return value !== undefined && value !== null;
  58. };
  59. export const isNumber = function (value) {
  60. return /^\d+(\.\d+)?$/.test(value);
  61. };
  62. export const addUnit = function (value) {
  63. if (!isDef(value)) {
  64. return undefined;
  65. }
  66. value = String(value);
  67. return isNumber(value) ? `${value}px` : value;
  68. };
  69. /**
  70. * 计算字符串字符的长度并可以截取字符串。
  71. * @param str 传入字符串
  72. * @param maxCharacter 规定最大字符串长度
  73. * @returns 当没有传入maxCharacter时返回字符串字符长度,当传入maxCharacter时返回截取之后的字符串和长度。
  74. */
  75. export const getCharacterLength = (str, maxCharacter) => {
  76. const hasMaxCharacter = typeof maxCharacter === 'number';
  77. if (!str || str.length === 0) {
  78. if (hasMaxCharacter) {
  79. return {
  80. length: 0,
  81. characters: str,
  82. };
  83. }
  84. return {
  85. length: 0,
  86. characters: '',
  87. };
  88. }
  89. let len = 0;
  90. // eslint-disable-next-line no-plusplus
  91. for (let i = 0; i < str.length; i++) {
  92. let currentStringLength = 0;
  93. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) === 94) {
  94. currentStringLength = 2;
  95. }
  96. else {
  97. currentStringLength = 1;
  98. }
  99. if (hasMaxCharacter && len + currentStringLength > maxCharacter) {
  100. return {
  101. length: len,
  102. characters: str.slice(0, i),
  103. };
  104. }
  105. len += currentStringLength;
  106. }
  107. if (hasMaxCharacter) {
  108. return {
  109. length: len,
  110. characters: str,
  111. };
  112. }
  113. return {
  114. length: len,
  115. characters: '',
  116. };
  117. };
  118. //# sourceMappingURL=utils.js.map