Camel case and snake case: Difference between revisions

Added Lua.
(→‎{{header|Quackery}}: bug fix and tidy up)
(Added Lua.)
Line 798:
spaces => spaces
</pre>
 
=={{header|Lua}}==
 
{{works with|Lua|5.1 and later}}
 
Since the task is a bit vague this solution also has its own interpretation of several details.
 
We have the two functions toCamelCase() and fromCamelCase() that perform the conversion back and forth between camel case and non-camel case, including snake case. The test strings are assumed to potentially contain multiple names. Any character that isn't part of the matched names (including leading and trailing whitespace) is ignored/left alone. Numbers aren't mentioned in the task but the "variable_10_case"/"variable10Case" strings imply that numbers should be treated as their own words.
 
<lang Lua>local function escapeForPattern(str)
return (str:gsub("[-+*^?$.%%()[%]]", "%%%0"))
end
 
local function toCamelCase(str, separator)
local escapedSeparator = escapeForPattern(separator)
local namePattern = "%l%w*"..escapedSeparator.."[%w"..escapedSeparator.."]*%w" -- Starting with a lower case character and containing the separator character.
local separatorPattern = escapedSeparator.."(%w)" -- Discard the separator and capture the alphanumeric character.
 
return (str:gsub(namePattern, function(name)
return (name:gsub(separatorPattern, string.upper)) -- The captured character will be the one argument for string.upper().
end))
end
 
local function fromCamelCase(str, separator)
local namePattern = "%l+[%u%d]%w*" -- Starting with a lower case character and containing an upper case character or digit.
local separatorPattern1 = "(%l)([%u%d])" -- Lower case character followed by upper case character or digit.
local separatorPattern2 = "(%d)(%a)" -- Digit followed by alphanumeric character.
 
return (str:gsub(namePattern, function(name)
return (name
:gsub(separatorPattern1, function(char1,char2) return char1..separator..char2:lower() end)
:gsub(separatorPattern2, function(char1,char2) return char1..separator..char2:lower() end)
)
end))
end</lang>
 
'''Tests:'''
 
<lang Lua>local function utf8Length(str)
local len = 0
for char in str:gmatch"[\1-\127\194-\244][\128-\191]*" do
len = len + 1
end
return len
end
 
local tests = {
"snakeCase", "snake_case",
"variable_10_case", "variable10Case",
"ɛrgo rE tHis",
"hurry-up-joe!",
"c://my-docs/happy_Flag-Day/12.doc",
" spaces ",
" internal space ",
}
 
local function convert(label, converter)
print(label..":")
for _, str in ipairs(tests) do
print((" "):rep(35-utf8Length(str)) .. str .. " => " .. converter(str))
end
print()
end
 
convert("snake_case to camelCase", function(str) return toCamelCase (str, "_") end)
convert("space case to camelCase", function(str) return toCamelCase (str, " ") end)
convert("kebab-case to camelCase", function(str) return toCamelCase (str, "-") end)
convert("camelCase to snake_case", function(str) return fromCamelCase(str, "_") end)</lang>
 
{{out}}
 
<pre>
snake_case to camelCase:
snakeCase => snakeCase
snake_case => snakeCase
variable_10_case => variable10Case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgo rE tHis
hurry-up-joe! => hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc => c://my-docs/happyFlag-Day/12.doc
spaces => spaces
internal space => internal space
 
space case to camelCase:
snakeCase => snakeCase
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgoRETHis
hurry-up-joe! => hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc => c://my-docs/happy_Flag-Day/12.doc
spaces => spaces
internal space => internalSpace
 
kebab-case to camelCase:
snakeCase => snakeCase
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgo rE tHis
hurry-up-joe! => hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc => c://myDocs/happy_FlagDay/12.doc
spaces => spaces
internal space => internal space
 
camelCase to snake_case:
snakeCase => snake_case
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable_10_case
ɛrgo rE tHis => ɛrgo r_e t_his
hurry-up-joe! => hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc => c://my-docs/happy_Flag-Day/12.doc
spaces => spaces
internal space => internal space
</pre>
 
'''Notes:'''
 
* We don't check if names have preceding alphanumeric characters (e.g. "10kN" will have the name "kN").
* We don't handle consecutive upper case characters (e.g. "newUIBox").
* We don't handle non-ASCII characters because of Lua's limited support for them ("ɛ" just happen to work here).
 
=={{header|Perl}}==
Anonymous user