utils.wxs 2.8 KB

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