f9ab09131fe98707cb2c24f197a7fcc5a63de6f8.svn-base 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * 获取字符串的长度ascii长度为1 中文长度为2
  3. * @param str
  4. * @returns {number}
  5. */
  6. export const getStrFullLength = (str = '') =>
  7. str.split('').reduce((pre, cur) => {
  8. const charCode = cur.charCodeAt(0)
  9. if (charCode >= 0 && charCode <= 128) {
  10. return pre + 1
  11. }
  12. return pre + 2
  13. }, 0)
  14. /**
  15. * 给定一个字符串和一个长度,将此字符串按指定长度截取
  16. * @param str
  17. * @param maxLength
  18. * @returns {string}
  19. */
  20. export const cutStrByFullLength = (str = '', maxLength) => {
  21. let showLength = 0
  22. return str.split('').reduce((pre, cur) => {
  23. const charCode = cur.charCodeAt(0)
  24. if (charCode >= 0 && charCode <= 128) {
  25. showLength += 1
  26. } else {
  27. showLength += 2
  28. }
  29. if (showLength <= maxLength) {
  30. return pre + cur
  31. }
  32. return pre
  33. }, '')
  34. }
  35. // 下划线转换驼峰
  36. export function underLinetoHump(name) {
  37. return name.replace(/\_(\w)/g, function(all, letter){
  38. return letter.toUpperCase();
  39. });
  40. }
  41. // 驼峰转换下划线
  42. export function humptoUnderLine(name) {
  43. return name.replace(/([A-Z])/g,"_$1").toLowerCase();
  44. }