Skip to content

Commit 554835f

Browse files
committed
refactor: component util fix
1 parent e107eeb commit 554835f

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/components/Ellipsis/Ellipsis.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import Tooltip from 'ant-design-vue/es/tooltip'
3-
import { cutStrByFullLength, getStrFullLength } from '@/components/_util/StringUtil'
3+
import { cutStrByFullLength, getStrFullLength } from '@/components/_util/util'
44
/*
55
const isSupportLineClamp = document.body.style.webkitLineClamp !== undefined;
66

src/components/_util/StringUtil.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/components/_util/util.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,37 @@
1010
export function filterEmpty (children = []) {
1111
return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
1212
}
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+
}

0 commit comments

Comments
 (0)