In a Visual Studio Code Snippet I'm writing I want to convert a snake case string to camel case.
From the docs I know that the syntax is
'${' var '/' regex '/' (format | text)+ '/' options '}'
so I've come up with this :
${TM_FILENAME_BASE/([a-z])([a-z]*)_+([a-z])([a-z]*)_+/${1:/upcase}$2${3:/upcase}$4/}
This code however works only for strings with 2 elements (for exemple "carrot_cake") while I would like to process strings with arbitrary number of elements ("blueberry_pie_with_a_cup_of_coffee").
I guess that some kind of recursion is needed in 'regex'
and 'format'
, but I have no idea of what to do.
How does one match arbitrary number of pattern occurrences?
To transform an arbitrary number of "_" separated words into CamelCase try:
"camelCase": {
"prefix": "_cc",
"body": [
"${TM_FILENAME_BASE/([a-z]*)_+([a-z]*)/${1:/capitalize}${2:/capitalize}/g}"
],
"description": "Transform to camel case"
},
carrot_cake.txt
-> CarrotCake
blueberry_pie_with_a_cup_of_coffee.js
-> BlueberryPieWithACupOfCoffee
[I assume CamelCase is the form you want, there are others, such as camelCase.]
For camelCase:
"${TM_FILENAME_BASE/([a-z]*)_*([a-z]*)/$1${2:/capitalize}/g}"
Note that the "g" flag at the end is doing most of that work for you in getting however many matches there are beyond the two explicitly captured.
I left the capture groups as ([a-z]*)
as you had them. You may want to use ([A-Za-z0-9]*)
for more flexibility.