mirror of
https://github.com/veops/oneterm.git
synced 2025-10-05 15:27:01 +08:00
48 lines
1010 B
JavaScript
48 lines
1010 B
JavaScript
/**
|
|
* components util
|
|
*/
|
|
|
|
/**
|
|
* clear null objects in children
|
|
* @param children
|
|
* @returns {*[]}
|
|
*/
|
|
export function filterEmpty (children = []) {
|
|
return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
|
|
}
|
|
|
|
/**
|
|
* get string length
|
|
* English character: length 1, Chinese character: length 2
|
|
* @param {*} str
|
|
*/
|
|
export const getStrFullLength = (str = '') =>
|
|
str.split('').reduce((pre, cur) => {
|
|
const charCode = cur.charCodeAt(0)
|
|
if (charCode >= 0 && charCode <= 128) {
|
|
return pre + 1
|
|
}
|
|
return pre + 2
|
|
}, 0)
|
|
|
|
/**
|
|
* cut the string based on maxLength
|
|
* @param {*} str
|
|
* @param {*} maxLength
|
|
*/
|
|
export const cutStrByFullLength = (str = '', maxLength) => {
|
|
let showLength = 0
|
|
return str.split('').reduce((pre, cur) => {
|
|
const charCode = cur.charCodeAt(0)
|
|
if (charCode >= 0 && charCode <= 128) {
|
|
showLength += 1
|
|
} else {
|
|
showLength += 2
|
|
}
|
|
if (showLength <= maxLength) {
|
|
return pre + cur
|
|
}
|
|
return pre
|
|
}, '')
|
|
}
|