123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /* utils */
- /**
- * addUnit */
- // 为 css 添加单位
- function addUnit(value) {
- var REGEXP = getRegExp('^-?\d+(.\d+)?$');
- if (value == null) {
- return undefined;
- }
- return REGEXP.test('' + value) ? value + 'px' : value;
- }
- function isString(string) {
- return string && string.constructor === 'String';
- }
- function isArray(array) {
- return array && array.constructor === 'Array';
- }
- function isObject(obj) {
- return obj && obj.constructor === 'Object';
- }
- var isNoEmptyObj = function (obj) {
- return isObject(obj) && JSON.stringify(obj) !== '{}';
- };
- function includes(arr, value) {
- if (!arr || !isArray(arr)) return false;
- var i = 0;
- var len = arr.length;
- for (; i < len; i++) {
- if (arr[i] === value) return true;
- }
- return false;
- }
- function cls(base, arr) {
- var res = [base];
- var i = 0;
- for (size = arr.length; i < size; i++) {
- var item = arr[i];
- if (item && item.constructor === 'Array') {
- var key = arr[i][0];
- var value = arr[i][1];
- if (value) {
- res.push(base + '--' + key);
- }
- } else if (typeof item === 'string' || typeof item === 'number') {
- if (item) {
- res.push(base + '--' + item);
- }
- }
- }
- return res.join(' ');
- }
- function getBadgeAriaLabel(options) {
- var maxCount = options.maxCount || 99;
- if (options.dot) {
- return '有新的消息';
- }
- if (options.count === '...') {
- return '有很多消息';
- }
- if (isNaN(options.count)) {
- return options.count;
- }
- var str1 = '有' + maxCount + '+条消息';
- var str2 = '有' + options.count + '条消息';
- return Number(options.count) > maxCount ? str1 : str2;
- }
- function endsWith(str, endStr) {
- return str.slice(-endStr.length) === endStr ? str : str + endStr;
- }
- function keys(obj) {
- return JSON.stringify(obj)
- .replace(getRegExp('{|}|"', 'g'), '')
- .split(',')
- .map(function (item) {
- return item.split(':')[0];
- });
- }
- function kebabCase(str) {
- return str
- .replace(getRegExp('[A-Z]', 'g'), function (ele) {
- return '-' + ele;
- })
- .toLowerCase();
- }
- function _style(styles) {
- if (isArray(styles)) {
- return styles
- .filter(function (item) {
- return item != null && item !== '';
- })
- .map(function (item) {
- return isArray(item) ? style(item) : endsWith(item, ';');
- })
- .join(' ');
- }
- if (isObject(styles)) {
- return keys(styles)
- .filter(function (key) {
- return styles[key] != null && styles[key] !== '';
- })
- .map(function (key) {
- return [kebabCase(key), [styles[key]]].join(':');
- })
- .join(';');
- }
- return styles;
- }
- module.exports = {
- addUnit: addUnit,
- isString: isString,
- isArray: isArray,
- isObject: isObject,
- isNoEmptyObj: isNoEmptyObj,
- includes: includes,
- cls: cls,
- getBadgeAriaLabel: getBadgeAriaLabel,
- _style: _style,
- };
|