utils.wxs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* utils */
  2. /**
  3. * addUnit */
  4. // 为 css 添加单位
  5. function addUnit(value) {
  6. // prettier-ignore
  7. var REGEXP = getRegExp('^-?\d+(.\d+)?$');
  8. if (value == null) {
  9. return undefined;
  10. }
  11. return REGEXP.test('' + value) ? value + 'px' : value;
  12. }
  13. function isString(string) {
  14. return string && string.constructor === 'String';
  15. }
  16. function isArray(array) {
  17. return array && array.constructor === 'Array';
  18. }
  19. function isObject(obj) {
  20. return obj && obj.constructor === 'Object';
  21. }
  22. function isBoolean(value) {
  23. return typeof value === 'boolean';
  24. }
  25. var isNoEmptyObj = function (obj) {
  26. return isObject(obj) && JSON.stringify(obj) !== '{}';
  27. };
  28. function includes(arr, value) {
  29. if (!arr || !isArray(arr)) return false;
  30. var i = 0;
  31. var len = arr.length;
  32. for (; i < len; i++) {
  33. if (arr[i] === value) return true;
  34. }
  35. return false;
  36. }
  37. function cls(base, arr) {
  38. var res = [base];
  39. var i = 0;
  40. for (var size = arr.length; i < size; i++) {
  41. var item = arr[i];
  42. if (item && item.constructor === 'Array') {
  43. var key = arr[i][0];
  44. var value = arr[i][1];
  45. if (value) {
  46. res.push(base + '--' + key);
  47. }
  48. } else if (typeof item === 'string' || typeof item === 'number') {
  49. if (item) {
  50. res.push(base + '--' + item);
  51. }
  52. }
  53. }
  54. return res.join(' ');
  55. }
  56. function getBadgeAriaLabel(options) {
  57. var maxCount = options.maxCount || 99;
  58. if (options.dot) {
  59. return '有新的消息';
  60. }
  61. if (options.count === '...') {
  62. return '有很多消息';
  63. }
  64. if (isNaN(options.count)) {
  65. return options.count;
  66. }
  67. var str1 = '有' + maxCount + '+条消息';
  68. var str2 = '有' + options.count + '条消息';
  69. return Number(options.count) > maxCount ? str1 : str2;
  70. }
  71. function endsWith(str, endStr) {
  72. return str.slice(-endStr.length) === endStr ? str : str + endStr;
  73. }
  74. function keys(obj) {
  75. return JSON.stringify(obj)
  76. .replace(getRegExp('{|}|"', 'g'), '')
  77. .split(',')
  78. .map(function (item) {
  79. return item.split(':')[0];
  80. });
  81. }
  82. function kebabCase(str) {
  83. return str
  84. .replace(getRegExp('[A-Z]', 'g'), function (ele) {
  85. return '-' + ele;
  86. })
  87. .toLowerCase();
  88. }
  89. function _style(styles) {
  90. if (isArray(styles)) {
  91. return styles
  92. .filter(function (item) {
  93. return item != null && item !== '';
  94. })
  95. .map(function (item) {
  96. return isArray(item) ? _style(item) : endsWith(item, ';');
  97. })
  98. .join(' ');
  99. }
  100. if (isObject(styles)) {
  101. return keys(styles)
  102. .filter(function (key) {
  103. return styles[key] != null && styles[key] !== '';
  104. })
  105. .map(function (key) {
  106. return [kebabCase(key), [styles[key]]].join(':');
  107. })
  108. .join(';');
  109. }
  110. return styles;
  111. }
  112. function isValidIconName(str) {
  113. // prettier-ignore
  114. return getRegExp('^[A-Za-z0-9\-]+$').test(str);
  115. }
  116. module.exports = {
  117. addUnit: addUnit,
  118. isString: isString,
  119. isArray: isArray,
  120. isObject: isObject,
  121. isBoolean: isBoolean,
  122. isNoEmptyObj: isNoEmptyObj,
  123. includes: includes,
  124. cls: cls,
  125. getBadgeAriaLabel: getBadgeAriaLabel,
  126. _style: _style,
  127. isValidIconName: isValidIconName,
  128. };