tools.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. export var camelToKebab = function (camelStr) {
  2. const isUpper = camelStr[0].charCodeAt(0) >= 65 && camelStr[0].charCodeAt(0) <= 90;
  3. const handleStr = camelStr.replace(/([A-Z])/g, "-$1").toLowerCase();
  4. let kebabStr = handleStr;
  5. if (isUpper) {
  6. kebabStr = handleStr.slice(1);
  7. }
  8. const newKebabArr = [];
  9. const kebabSplitArr = kebabStr.split("-");
  10. kebabSplitArr.forEach((item, index) => {
  11. if (item.length > 1) {
  12. newKebabArr.push(item);
  13. } else {
  14. let combineStr = "";
  15. const subKebabArr = kebabSplitArr.slice(index);
  16. for (let i = 0; i < subKebabArr.length; i++) {
  17. if (subKebabArr[i].length > 1)
  18. break;
  19. combineStr += subKebabArr[i];
  20. }
  21. newKebabArr.push(combineStr);
  22. kebabSplitArr.splice(index + 1, combineStr.length - 1);
  23. }
  24. });
  25. return newKebabArr.join("-");
  26. };
  27. export var isNum = function (num) {
  28. return typeof num === "number" && !isNaN(num);
  29. };
  30. export var toStyleStr = (styleObj, unit = "rpx") => {
  31. if (Array.isArray(styleObj)) {
  32. return styleObj.join(";");
  33. } else {
  34. if (typeof styleObj === "object" && styleObj !== null) {
  35. let style = [];
  36. for (const k in styleObj) {
  37. if (typeof k === "string") {
  38. let val = styleObj[k];
  39. if (val !== void 0) {
  40. if (typeof val === "number") {
  41. if (addUnitAttr.includes(k)) {
  42. val = val + unit;
  43. }
  44. }
  45. style.push(`${camelToKebab(k)}:${val}`);
  46. }
  47. }
  48. }
  49. return style.length > 0 ? style.join(";") + ";" : "";
  50. } else {
  51. return styleObj;
  52. }
  53. }
  54. };
  55. function isObject(value) {
  56. var type = typeof value;
  57. return value != null && (type == 'object' || type == 'function');
  58. }
  59. function toNumber(value) {
  60. if (typeof value == 'number') {
  61. return value;
  62. }
  63. if (isSymbol(value)) {
  64. return NAN;
  65. }
  66. if (isObject(value)) {
  67. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  68. value = isObject(other) ? (other + '') : other;
  69. }
  70. if (typeof value != 'string') {
  71. return value === 0 ? value : +value;
  72. }
  73. value = value.replace(reTrim, '');
  74. var isBinary = reIsBinary.test(value);
  75. return (isBinary || reIsOctal.test(value))
  76. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  77. : (reIsBadHex.test(value) ? NAN : +value);
  78. }
  79. export function debounce(func, wait, options) {
  80. var lastArgs,
  81. lastThis,
  82. maxWait,
  83. result,
  84. timerId,
  85. lastCallTime,
  86. lastInvokeTime = 0,
  87. leading = false,
  88. maxing = false,
  89. trailing = true;
  90. if (typeof func != 'function') {
  91. throw new TypeError(FUNC_ERROR_TEXT);
  92. }
  93. wait = toNumber(wait) || 0;
  94. if (isObject(options)) {
  95. leading = !!options.leading;
  96. maxing = 'maxWait' in options;
  97. maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
  98. trailing = 'trailing' in options ? !!options.trailing : trailing;
  99. }
  100. function invokeFunc(time) {
  101. var args = lastArgs,
  102. thisArg = lastThis;
  103. lastArgs = lastThis = undefined;
  104. lastInvokeTime = time;
  105. result = func.apply(thisArg, args);
  106. return result;
  107. }
  108. function leadingEdge(time) {
  109. // Reset any `maxWait` timer.
  110. lastInvokeTime = time;
  111. // Start the timer for the trailing edge.
  112. timerId = setTimeout(timerExpired, wait);
  113. // Invoke the leading edge.
  114. return leading ? invokeFunc(time) : result;
  115. }
  116. function remainingWait(time) {
  117. var timeSinceLastCall = time - lastCallTime,
  118. timeSinceLastInvoke = time - lastInvokeTime,
  119. timeWaiting = wait - timeSinceLastCall;
  120. return maxing
  121. ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke)
  122. : timeWaiting;
  123. }
  124. function shouldInvoke(time) {
  125. var timeSinceLastCall = time - lastCallTime,
  126. timeSinceLastInvoke = time - lastInvokeTime;
  127. // Either this is the first call, activity has stopped and we're at the
  128. // trailing edge, the system time has gone backwards and we're treating
  129. // it as the trailing edge, or we've hit the `maxWait` limit.
  130. return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
  131. (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait));
  132. }
  133. function timerExpired() {
  134. var time = now();
  135. if (shouldInvoke(time)) {
  136. return trailingEdge(time);
  137. }
  138. // Restart the timer.
  139. timerId = setTimeout(timerExpired, remainingWait(time));
  140. }
  141. function trailingEdge(time) {
  142. timerId = undefined;
  143. // Only invoke if we have `lastArgs` which means `func` has been
  144. // debounced at least once.
  145. if (trailing && lastArgs) {
  146. return invokeFunc(time);
  147. }
  148. lastArgs = lastThis = undefined;
  149. return result;
  150. }
  151. function cancel() {
  152. if (timerId !== undefined) {
  153. clearTimeout(timerId);
  154. }
  155. lastInvokeTime = 0;
  156. lastArgs = lastCallTime = lastThis = timerId = undefined;
  157. }
  158. function flush() {
  159. return timerId === undefined ? result : trailingEdge(now());
  160. }
  161. function debounced() {
  162. var time = now(),
  163. isInvoking = shouldInvoke(time);
  164. lastArgs = arguments;
  165. lastThis = this;
  166. lastCallTime = time;
  167. if (isInvoking) {
  168. if (timerId === undefined) {
  169. return leadingEdge(lastCallTime);
  170. }
  171. if (maxing) {
  172. // Handle invocations in a tight loop.
  173. timerId = setTimeout(timerExpired, wait);
  174. return invokeFunc(lastCallTime);
  175. }
  176. }
  177. if (timerId === undefined) {
  178. timerId = setTimeout(timerExpired, wait);
  179. }
  180. return result;
  181. }
  182. debounced.cancel = cancel;
  183. debounced.flush = flush;
  184. return debounced;
  185. }