Add more string manipulation functions (#998)
This commit is contained in:
parent
a306a47898
commit
d6d4b01af4
@ -854,10 +854,14 @@ The executable is at: /bin/just
|
|||||||
|
|
||||||
==== String Manipulation
|
==== String Manipulation
|
||||||
|
|
||||||
- `uppercase(s)` - Convert `s` to uppercase.
|
|
||||||
- `lowercase(s)` - Convert `s` to lowercase.
|
- `lowercase(s)` - Convert `s` to lowercase.
|
||||||
- `trim(s)` - Remove leading and trailing whitespace from `s`.
|
|
||||||
- `replace(s, from, to)` - Replace all occurrences of `from` in `s` to `to`.
|
- `replace(s, from, to)` - Replace all occurrences of `from` in `s` to `to`.
|
||||||
|
- `trim(s)` - Remove leading and trailing whitespace from `s`.
|
||||||
|
- `trim_end_match(s, pat)` - Remove suffix of `s` matching `pat`.
|
||||||
|
- `trim_end_matches(s, pat)` - Repeatedly remove suffixes of `s` matching `pat`.
|
||||||
|
- `trim_start_match(s, pat)` - Remove prefix of `s` matching `pat`.
|
||||||
|
- `trim_start_matches(s, pat)` - Repeatedly remove prefixes of `s` matching `pat`.
|
||||||
|
- `uppercase(s)` - Convert `s` to uppercase.
|
||||||
|
|
||||||
==== Dotenv Integration
|
==== Dotenv Integration
|
||||||
|
|
||||||
|
@ -28,6 +28,10 @@ lazy_static! {
|
|||||||
("parent_directory", Unary(parent_directory)),
|
("parent_directory", Unary(parent_directory)),
|
||||||
("replace", Ternary(replace)),
|
("replace", Ternary(replace)),
|
||||||
("trim", Unary(trim)),
|
("trim", Unary(trim)),
|
||||||
|
("trim_end_match", Binary(trim_end_match)),
|
||||||
|
("trim_end_matches", Binary(trim_end_matches)),
|
||||||
|
("trim_start_match", Binary(trim_start_match)),
|
||||||
|
("trim_start_matches", Binary(trim_start_matches)),
|
||||||
("uppercase", Unary(uppercase)),
|
("uppercase", Unary(uppercase)),
|
||||||
("without_extension", Unary(without_extension)),
|
("without_extension", Unary(without_extension)),
|
||||||
]
|
]
|
||||||
@ -197,6 +201,22 @@ fn trim(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
|||||||
Ok(s.trim().to_owned())
|
Ok(s.trim().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trim_end_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
|
Ok(s.strip_suffix(pat).unwrap_or(s).to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trim_end_matches(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
|
Ok(s.trim_end_matches(pat).to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trim_start_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
|
Ok(s.strip_prefix(pat).unwrap_or(s).to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trim_start_matches(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
|
Ok(s.trim_start_matches(pat).to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
fn uppercase(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
fn uppercase(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
||||||
Ok(s.to_uppercase())
|
Ok(s.to_uppercase())
|
||||||
}
|
}
|
||||||
|
@ -302,3 +302,37 @@ test! {
|
|||||||
stdout: "foofoofoo\n",
|
stdout: "foofoofoo\n",
|
||||||
stderr: "echo foofoofoo\n",
|
stderr: "echo foofoofoo\n",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn assert_eval_eq(expression: &str, result: &str) {
|
||||||
|
Test::new()
|
||||||
|
.justfile(format!("x := {}", expression))
|
||||||
|
.args(&["--evaluate", "x"])
|
||||||
|
.stdout(result)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_end_matches() {
|
||||||
|
assert_eval_eq("trim_end_matches('foo', 'o')", "f");
|
||||||
|
assert_eval_eq("trim_end_matches('fabab', 'ab')", "f");
|
||||||
|
assert_eval_eq("trim_end_matches('fbaabab', 'ab')", "fba");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_end_match() {
|
||||||
|
assert_eval_eq("trim_end_match('foo', 'o')", "fo");
|
||||||
|
assert_eval_eq("trim_end_match('fabab', 'ab')", "fab");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_start_matches() {
|
||||||
|
assert_eval_eq("trim_start_matches('oof', 'o')", "f");
|
||||||
|
assert_eval_eq("trim_start_matches('ababf', 'ab')", "f");
|
||||||
|
assert_eval_eq("trim_start_matches('ababbaf', 'ab')", "baf");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_start_match() {
|
||||||
|
assert_eval_eq("trim_start_match('oof', 'o')", "of");
|
||||||
|
assert_eval_eq("trim_start_match('ababf', 'ab')", "abf");
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user