util.js 1007 B

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