inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /**
I 2  * components util
3  */
4
5 /**
6  * 清理空值,对象
7  * @param children
8  * @returns {*[]}
9  */
10 export function filterEmpty (children = []) {
11   return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
12 }
13
14 /**
15  * 获取字符串长度,英文字符 长度1,中文字符长度2
16  * @param {*} str
17  */
18 export const getStrFullLength = (str = '') =>
19   str.split('').reduce((pre, cur) => {
20     const charCode = cur.charCodeAt(0)
21     if (charCode >= 0 && charCode <= 128) {
22       return pre + 1
23     }
24     return pre + 2
25   }, 0)
26
27 /**
28  * 截取字符串,根据 maxLength 截取后返回
29  * @param {*} str
30  * @param {*} maxLength
31  */
32 export const cutStrByFullLength = (str = '', maxLength) => {
33   let showLength = 0
34   return str.split('').reduce((pre, cur) => {
35     const charCode = cur.charCodeAt(0)
36     if (charCode >= 0 && charCode <= 128) {
37       showLength += 1
38     } else {
39       showLength += 2
40     }
41     if (showLength <= maxLength) {
42       return pre + cur
43     }
44     return pre
45   }, '')
46 }