utils.wxs 3.0 KB

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