From aaef61b9087a82db82411ed9e71d0f24e3b5096e Mon Sep 17 00:00:00 2001 From: Cihan Demirci Date: Tue, 25 Oct 2022 04:39:40 +0100 Subject: [PATCH] Add `capitalize(s)` function (#1375) `capitalize(s)` converts the first character of s to uppercase and the rest to lowercase. --- README.md | 2 ++ extras/just.sublime-syntax | 2 +- src/function.rs | 16 ++++++++++++++++ tests/functions.rs | 10 ++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97c9220..098c454 100644 --- a/README.md +++ b/README.md @@ -1142,6 +1142,8 @@ The executable is at: /bin/just #### String Manipulation +- `capitalize(s)`master - Convert first character of `s` to uppercase and the rest to lowercase. + - `lowercase(s)` - Convert `s` to lowercase. - `quote(s)` - Replace all single quotes with `'\''` and prepend and append single quotes to `s`. This is sufficient to escape special characters for many shells, including most Bourne shell descendants. diff --git a/extras/just.sublime-syntax b/extras/just.sublime-syntax index 8b0442c..b5525a6 100644 --- a/extras/just.sublime-syntax +++ b/extras/just.sublime-syntax @@ -31,7 +31,7 @@ contexts: - match: '\}\}' pop: true functions: - - match: \b(arch|os|os_family|env_var|env_var_or_default|invocation_directory|justfile|justfile_directory|just_executable|lowercase|quote|replace|trim|trim_end|trim_end_match|trim_end_matches|trim_start|trim_start_match|trim_start_matches|uppercase|absolute_path|extension|file_name|file_stem|parent_directory|without_extension|join|clean|path_exists|error|sha256|sha256_file|uuid)\b(?=\() + - match: \b(arch|os|os_family|env_var|env_var_or_default|invocation_directory|justfile|justfile_directory|just_executable|lowercase|quote|replace|trim|trim_end|trim_end_match|trim_end_matches|trim_start|trim_start_match|trim_start_matches|uppercase|absolute_path|extension|file_name|file_stem|parent_directory|without_extension|join|clean|path_exists|error|sha256|sha256_file|uuid|capitalize)\b(?=\() scope: entity.name.function.just keywords: - match: \b(if|else|while)\b diff --git a/src/function.rs b/src/function.rs index 6ea88f4..d2932f0 100644 --- a/src/function.rs +++ b/src/function.rs @@ -16,6 +16,7 @@ lazy_static! { pub(crate) static ref TABLE: BTreeMap<&'static str, Function> = vec![ ("absolute_path", Unary(absolute_path)), ("arch", Nullary(arch)), + ("capitalize", Unary(capitalize)), ("clean", Unary(clean)), ("env_var", Unary(env_var)), ("env_var_or_default", Binary(env_var_or_default)), @@ -79,6 +80,21 @@ fn arch(_context: &FunctionContext) -> Result { Ok(target::arch().to_owned()) } +fn capitalize(_context: &FunctionContext, s: &str) -> Result { + Ok( + s.chars() + .enumerate() + .flat_map(|(i, c)| { + if i == 0 { + c.to_uppercase().collect::>() + } else { + c.to_lowercase().collect::>() + } + }) + .collect(), + ) +} + fn clean(_context: &FunctionContext, path: &str) -> Result { Ok(Path::new(path).lexiclean().to_str().unwrap().to_owned()) } diff --git a/tests/functions.rs b/tests/functions.rs index 327abe7..f2c2ae8 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -303,6 +303,16 @@ test! { stderr: "echo foofoofoo\n", } +test! { + name: capitalize, + justfile: " + foo: + echo {{ capitalize('BAR') }} + ", + stdout: "Bar\n", + stderr: "echo Bar\n", +} + fn assert_eval_eq(expression: &str, result: &str) { Test::new() .justfile(format!("x := {}", expression))