How to resolve the algorithm Camel case and snake case step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Camel case and snake case step by step in the jq programming language
Table of Contents
Problem Statement
Two common conventions for naming of computer program variables are Snake Case and Camel Case. Snake case variables are generally all lower case, with an underscore between words in the variable, as in snake_case_variable'. Camel case variables are generally lower case first (except in some Pascal conventions or with class names in many other languages), with captalization of the initial letter of the words within the variable, as in 'camelCaseVariable'. Leading underscores are not used in such variables except as part of a different naming convention, usually for special internal or system variables. White space is not permitted as part of camel case or snake case variable names.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Camel case and snake case step by step in the jq programming language
Source code in the jq programming language
def isUpper: explode[0] | 65 <= . and . <= 90;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
# "White space is not permitted as part of camel case or snake case variable names."
def trim: sub("^\\s+";"") | sub("\\s+$";"");
def toCamel:
trim as $snake
| { camel: "", underscore : false}
| reduce ($snake|explode[]|[.]|implode) as $c (.;
if ["_", "-", " "]|index($c)
then .underscore = true
elif .underscore
then .camel += ($c|ascii_upcase)
| .underscore = false
else .camel += $c
end)
| .camel;
def toSnake:
(trim | gsub("\\s"; "_")) as $camel
| reduce ($camel|explode[1:][]|[.]|implode) as $c (
$camel[0:1];
if $c|isUpper
then ($c|ascii_downcase) as $lc
| if (.[-1:] | (. == "_" or . == "-"))
then . + $lc
else . + "_" + $lc
end
else . + $c
end );
def tests: [
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "
];
" === to_snake_case ===",
(tests[] | "\(lpad(33)) -> \(toSnake)"),
"",
" === toCamelCase ===",
(tests[] | "\(lpad(33)) -> \(toCamel)")
You may also check:How to resolve the algorithm Break OO privacy step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Oz programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the zkl programming language
You may also check:How to resolve the algorithm Even or odd step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the OxygenBasic programming language