Add trim_end(s)
and trim_start(s)
functions (#999)
This commit is contained in:
parent
d6d4b01af4
commit
6786bb0953
@ -857,8 +857,10 @@ The executable is at: /bin/just
|
|||||||
- `lowercase(s)` - Convert `s` to lowercase.
|
- `lowercase(s)` - Convert `s` to lowercase.
|
||||||
- `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(s)` - Remove leading and trailing whitespace from `s`.
|
||||||
|
- `trim_end(s)` - Remove trailing whitespace from `s`.
|
||||||
- `trim_end_match(s, pat)` - Remove suffix of `s` matching `pat`.
|
- `trim_end_match(s, pat)` - Remove suffix of `s` matching `pat`.
|
||||||
- `trim_end_matches(s, pat)` - Repeatedly remove suffixes of `s` matching `pat`.
|
- `trim_end_matches(s, pat)` - Repeatedly remove suffixes of `s` matching `pat`.
|
||||||
|
- `trim_start(s)` - Remove leading whitespace from `s`.
|
||||||
- `trim_start_match(s, pat)` - Remove prefix 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`.
|
- `trim_start_matches(s, pat)` - Repeatedly remove prefixes of `s` matching `pat`.
|
||||||
- `uppercase(s)` - Convert `s` to uppercase.
|
- `uppercase(s)` - Convert `s` to uppercase.
|
||||||
|
@ -28,8 +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", Unary(trim_end)),
|
||||||
("trim_end_match", Binary(trim_end_match)),
|
("trim_end_match", Binary(trim_end_match)),
|
||||||
("trim_end_matches", Binary(trim_end_matches)),
|
("trim_end_matches", Binary(trim_end_matches)),
|
||||||
|
("trim_start", Unary(trim_start)),
|
||||||
("trim_start_match", Binary(trim_start_match)),
|
("trim_start_match", Binary(trim_start_match)),
|
||||||
("trim_start_matches", Binary(trim_start_matches)),
|
("trim_start_matches", Binary(trim_start_matches)),
|
||||||
("uppercase", Unary(uppercase)),
|
("uppercase", Unary(uppercase)),
|
||||||
@ -201,6 +203,10 @@ fn trim(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
|||||||
Ok(s.trim().to_owned())
|
Ok(s.trim().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trim_end(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
||||||
|
Ok(s.trim_end().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
fn trim_end_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
fn trim_end_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
Ok(s.strip_suffix(pat).unwrap_or(s).to_owned())
|
Ok(s.strip_suffix(pat).unwrap_or(s).to_owned())
|
||||||
}
|
}
|
||||||
@ -209,6 +215,10 @@ fn trim_end_matches(_context: &FunctionContext, s: &str, pat: &str) -> Result<St
|
|||||||
Ok(s.trim_end_matches(pat).to_owned())
|
Ok(s.trim_end_matches(pat).to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trim_start(_context: &FunctionContext, s: &str) -> Result<String, String> {
|
||||||
|
Ok(s.trim_start().to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
fn trim_start_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
fn trim_start_match(_context: &FunctionContext, s: &str, pat: &str) -> Result<String, String> {
|
||||||
Ok(s.strip_prefix(pat).unwrap_or(s).to_owned())
|
Ok(s.strip_prefix(pat).unwrap_or(s).to_owned())
|
||||||
}
|
}
|
||||||
|
@ -308,6 +308,7 @@ fn assert_eval_eq(expression: &str, result: &str) {
|
|||||||
.justfile(format!("x := {}", expression))
|
.justfile(format!("x := {}", expression))
|
||||||
.args(&["--evaluate", "x"])
|
.args(&["--evaluate", "x"])
|
||||||
.stdout(result)
|
.stdout(result)
|
||||||
|
.unindent_stdout(false)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,3 +337,13 @@ fn trim_start_match() {
|
|||||||
assert_eval_eq("trim_start_match('oof', 'o')", "of");
|
assert_eval_eq("trim_start_match('oof', 'o')", "of");
|
||||||
assert_eval_eq("trim_start_match('ababf', 'ab')", "abf");
|
assert_eval_eq("trim_start_match('ababf', 'ab')", "abf");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_start() {
|
||||||
|
assert_eval_eq("trim_start(' f ')", "f ");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_end() {
|
||||||
|
assert_eval_eq("trim_end(' f ')", " f");
|
||||||
|
}
|
||||||
|
@ -47,6 +47,7 @@ pub(crate) struct Test {
|
|||||||
pub(crate) stdout: String,
|
pub(crate) stdout: String,
|
||||||
pub(crate) suppress_dotenv_load_warning: bool,
|
pub(crate) suppress_dotenv_load_warning: bool,
|
||||||
pub(crate) tempdir: TempDir,
|
pub(crate) tempdir: TempDir,
|
||||||
|
pub(crate) unindent_stdout: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Test {
|
impl Test {
|
||||||
@ -68,6 +69,7 @@ impl Test {
|
|||||||
stdout: String::new(),
|
stdout: String::new(),
|
||||||
suppress_dotenv_load_warning: true,
|
suppress_dotenv_load_warning: true,
|
||||||
tempdir,
|
tempdir,
|
||||||
|
unindent_stdout: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,6 +149,11 @@ impl Test {
|
|||||||
tree.instantiate(&self.tempdir.path()).unwrap();
|
tree.instantiate(&self.tempdir.path()).unwrap();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn unindent_stdout(mut self, unindent_stdout: bool) -> Self {
|
||||||
|
self.unindent_stdout = unindent_stdout;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Test {
|
impl Test {
|
||||||
@ -156,7 +163,11 @@ impl Test {
|
|||||||
fs::write(self.justfile_path(), justfile).unwrap();
|
fs::write(self.justfile_path(), justfile).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdout = unindent(&self.stdout);
|
let stdout = if self.unindent_stdout {
|
||||||
|
unindent(&self.stdout)
|
||||||
|
} else {
|
||||||
|
self.stdout
|
||||||
|
};
|
||||||
let stderr = unindent(&self.stderr);
|
let stderr = unindent(&self.stderr);
|
||||||
|
|
||||||
let mut dotenv_path = self.tempdir.path().to_path_buf();
|
let mut dotenv_path = self.tempdir.path().to_path_buf();
|
||||||
|
Loading…
Reference in New Issue
Block a user