Skip to content

Commit

Permalink
Add capitalize(s) function (#1375)
Browse files Browse the repository at this point in the history
`capitalize(s)` converts the first character of s to uppercase
and the rest to lowercase.
  • Loading branch information
femnad authored Oct 25, 2022
1 parent ffb5479 commit aaef61b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,8 @@ The executable is at: /bin/just

#### String Manipulation

- `capitalize(s)`<sup>master</sup> - 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.
Expand Down
2 changes: 1 addition & 1 deletion extras/just.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -79,6 +80,21 @@ fn arch(_context: &FunctionContext) -> Result<String, String> {
Ok(target::arch().to_owned())
}

fn capitalize(_context: &FunctionContext, s: &str) -> Result<String, String> {
Ok(
s.chars()
.enumerate()
.flat_map(|(i, c)| {
if i == 0 {
c.to_uppercase().collect::<Vec<_>>()
} else {
c.to_lowercase().collect::<Vec<_>>()
}
})
.collect(),
)
}

fn clean(_context: &FunctionContext, path: &str) -> Result<String, String> {
Ok(Path::new(path).lexiclean().to_str().unwrap().to_owned())
}
Expand Down
10 changes: 10 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit aaef61b

Please sign in to comment.