Blame view

addons/exam/uniapp/tuniao-ui/libs/function/string.js 1.63 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  /**

   * 去掉字符串中空格

   * 

   * @param {String} str 待处理的字符串

   * @param {String} type 处理类型

   */

  function trim(str, type = 'both') {

    if (type === 'both') {

      return str.replace(/^\s+|\s+$/g, "")

    } else if (type === 'left') {

      return str.replace(/^\s*/g, "")

    } else if (type === 'right') {

      return str.replace(/(\s*$)/g, "")

    } else if (type === 'all') {

      return str.replace(/\s+/g, "")

    } else {

      return str

    }

  }

  

  /**

   * 获取带单位的长度值

   * 

   * @param {String} value 待处理的值

   * @param {String} unit 单位

   */

  function getLengthUnitValue(value, unit = 'rpx') {

    if (!value) {

      return ''

    }

    if (/(%|px|rpx|auto)$/.test(value)) return value

    else return value + unit

  }

  

  /**

   * 将驼峰命名的字符串转换为指定连接符来进行连接

   * 

   * @param {Object} string 待转换的字符串

   * @param {Object} replace 进行连接的字符

   */

  function humpConvertChar(string, replace = '_') {

    if (!string || !replace) {

      return ''

    }

    return string.replace(/([A-Z])/g, `${replace}$1`).toLowerCase()

  }

  

  /**

   * 将用指定连接符来进行连接的字符串转为驼峰命名的字符串

   * 

   * @param {Object} string 待转换的字符串

   * @param {Object} replace 进行连接的字符

   */

  function charConvertHump(string, replace = '_') {

    if (!string || !replace) {

      return ''

    }

    let reg = RegExp(replace + "(\\w)", "g")

    return string.replace(reg, function(all, letter) {

      return letter.toUpperCase()

    })

  }

  

  export default {

    trim,

    getLengthUnitValue,

    humpConvertChar,

    charConvertHump

  }