Blame view

ant-design-vue-jeecg/src/components/_util/StringUtil.js 1.09 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * 获取字符串的长度ascii长度为1 中文长度为2
 * @param str
 * @returns {number}
 */
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)

/**
 * 给定一个字符串和一个长度,将此字符串按指定长度截取
 * @param str
 * @param maxLength
 * @returns {string}
 */
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
  }, '')
}

// 下划线转换驼峰
export function underLinetoHump(name) {
肖超群 authored
39
  return name.replace(/\_(\w)/g, function (all, letter) {
肖超群 authored
40
41
42
    return letter.toUpperCase();
  });
}
肖超群 authored
43
肖超群 authored
44
45
// 驼峰转换下划线
export function humptoUnderLine(name) {
肖超群 authored
46
  return name.replace(/([A-Z])/g, "_$1").toLowerCase();
肖超群 authored
47
}