Convert camelCaseText to Sentence Case Text

后端 未结 20 2156
闹比i
闹比i 2020-11-28 03:44

How can I convert a string either like \'helloThere\' or \'HelloThere\' to \'Hello There\' in JavaScript?

20条回答
  •  -上瘾入骨i
    2020-11-28 04:19

    I didn't try everyone's answer, but the few solutions I tinkered with did not match all of my requirements.

    I was able to come up with something that did...

    export const jsObjToCSSString = (o={}) =>
        Object.keys(o)
              .map(key => ({ key, value: o[key] }))
              .map(({key, value}) =>
                  ({
                    key: key.replace( /([A-Z])/g, "-$1").toLowerCase(),
                    value
                  })
              )
              .reduce(
                  (css, {key, value}) => 
                      `${css} ${key}: ${value}; `.trim(), 
                  '')
    

提交回复
热议问题