StringUtil.js 588 B

12345678910111213141516171819202122232425
  1. export const getStrFullLength = (str = '') =>
  2. str.split('').reduce((pre, cur) => {
  3. const charCode = cur.charCodeAt(0)
  4. if (charCode >= 0 && charCode <= 128) {
  5. return pre + 1
  6. }
  7. return pre + 2
  8. }, 0)
  9. export const cutStrByFullLength = (str = '', maxLength) => {
  10. let showLength = 0
  11. return str.split('').reduce((pre, cur) => {
  12. const charCode = cur.charCodeAt(0)
  13. if (charCode >= 0 && charCode <= 128) {
  14. showLength += 1
  15. } else {
  16. showLength += 2
  17. }
  18. if (showLength <= maxLength) {
  19. return pre + cur
  20. }
  21. return pre
  22. }, '')
  23. }