# 驼峰转换

# 目录结构

  |-- src
  |--  |-- utils
  |--  |--  |   |--camelCase.js
1
2
3

# 使用

  import { camelizeKeys, decamelizeKeys } from '@/utils/camelCase'
  // 转驼峰
  decamelizeKeys(request.data)
  // 驼峰转下划线
  camelizeKeys(data)
1
2
3
4
5

# 实现 (opens new window)

  import {
    isFunction,
    isObject,
    isArray,
    isDate,
    isRegExp,
    isBoolean,
    isNumberical
  } from './type'

  // camelize('hello_world') -> 'helloWorld'
  export function camelize (string) {
    if (isNumberical(string)) {
      return string
    }

    string = string.replace(/[-_\s]+(.)?/g, function (match, chr) {
      return chr ? chr.toUpperCase() : ''
    })

    return string.substr(0, 1).toLowerCase() + string.substr(1)
  }

  // pascalize('hello_world') -> 'HelloWorld'
  export function pascalize (string) {
    const camelized = camelize(string)
    return camelized.substr(0, 1).toUpperCase() + camelized.substr(1)
  }

  function seperateWords (string, options) {
    options = options || {}
    const separator = options.separator || '_'
    const split = options.split || /(?=[A-Z])/

    return string.split(split).join(separator)
  }

  /**
   * decamelize('helloWorld') -> 'hello_world'
   * decamelize('HelloWorld') -> 'hello_world'
   * decamelize('helloWorld', { separator: '-' }) -> 'hello-world'
   */
  export function decamelize (string, options) {
    return seperateWords(string, options).toLowerCase()
  }

  function processor (convert, options) {
    const callback = options && 'process' in options ? options.process : options

    if (typeof (callback) !== 'function') {
      return convert
    }

    return function (string, options) {
      return callback(string, convert, options)
    }
  }

  /* eslint-disable */
  function processKeys (convert, obj, options) {
    if (!isObject(obj) || isDate(obj) || isRegExp(obj) || isBoolean(obj) || isFunction(obj)) {
      return obj
    }

    let output
    let i = 0
    let l = 0

    if (isArray(obj)) {
      output = []
      for (l = obj.length; i < l; i++) {
        output.push(processKeys(convert, obj[i], options))
      }
    } else {
      output = {}
      for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
          output[convert(key, options)] = processKeys(convert, obj[key], options)
        }
      }
    }
    return output
  }

  /**
   * camelizeKeys({ attr_one: 'foo', attr_two: 'bar' }) ->
   * { attrOne: 'foo', attrTwo: 'bar' }
   *
   * camelizeKeys([{ attr_one: 'foo' }, { attr_one: 'bar' }]) ->
   * [{ attrOne: 'foo' }, { attrOne: 'bar' }]
   */
  export function camelizeKeys (object, options) {
    return processKeys(processor(camelize, options), object)
  }

  /**
   * decamelizeKeys({ attrOne: 'foo', attrTwo: 'bar' }) ->
   * { attr_one: 'foo', attr_two: 'bar' }
   */
  export function decamelizeKeys (object, options) {
    return processKeys(processor(decamelize, options), object, options)
  }

  /**
   * pascalizeKeys({ attr_one: 'foo', attr_two: 'bar' }) ->
   * { AttrOne: 'foo', AttrTwo: 'bar' }
   */
  export function pascalizeKeys (object, options) {
    return processKeys(processor(pascalize, options), object)
  }

  /**
   * pascalizeKeys({ AttrOne: 'foo', AttrTwo: 'bar' }) ->
   * { attr_one: 'foo', attr_two: 'bar' }
   */
  export function depascalizeKeys () {
    return decamelizeKeys.apply(this, arguments)
  }
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
最后更新时间: 12/6/2020, 7:51:40 PM