Enhancement: split function now allows to remove empty values via new third part 'remove-empty' #863

This commit is contained in:
sagie gur ari
2023-06-10 15:26:13 +00:00
parent dd4c3499c3
commit 7dc6a544b6
5 changed files with 71 additions and 4 deletions
+1
View File
@@ -2,6 +2,7 @@
### v0.36.10
* Enhancement: split function now allows to remove empty values via new third part 'remove-empty' #863
* Enhancement: list-steps command should group aliases with original commands #862 (thanks @xxchan)
### v0.36.9 (2023-06-05)
+3
View File
@@ -2722,6 +2722,9 @@ args = ["${MULTIPLE_VALUES}"]
[cargo-make] INFO - Build Done in 0 seconds.
```
The split function also supports optional third *mode* attribute.<br>
If mode: *remove-empty* the output will not include empty values.
<a name="usage-functions-getat"></a>
#### GetAt
+9
View File
@@ -4,6 +4,7 @@ skip_core_tasks = true
[env]
MULTIPLE_VALUES = "1|2|3|4"
MULTIPLE_VALUES_WITH_EMPTY_CELLS = "1|2|||3|4"
TRIM_VALUE = " 123 "
CARGO_MAKE_CLIPPY_ARGS = ""
@@ -14,6 +15,14 @@ CARGO_MAKE_CLIPPY_ARGS = "--all-features -- -D warnings"
command = "echo"
args = ["@@split(MULTIPLE_VALUES,|)"]
[tasks.split-remove-empty]
command = "echo"
args = ["@@split(MULTIPLE_VALUES_WITH_EMPTY_CELLS,|,remove-empty)"]
[tasks.split-with-empty]
command = "echo"
args = ["@@split(MULTIPLE_VALUES_WITH_EMPTY_CELLS,|,default)"]
[tasks.no-split]
command = "echo"
args = ["${MULTIPLE_VALUES}"]
+19 -3
View File
@@ -10,12 +10,19 @@ mod split_func_test;
use envmnt;
pub(crate) fn invoke(function_args: &Vec<String>) -> Vec<String> {
if function_args.len() != 2 {
error!("split expects only 2 arguments (environment variable name, split by character)");
let args_count = function_args.len();
if args_count < 2 || args_count > 3 {
error!("split expects two or three arguments (environment variable name, split by character, optional mode: default, remove-empty)");
}
let env_key = function_args[0].clone();
let split_by = function_args[1].clone();
let mode_name = if args_count == 3 {
&function_args[2]
} else {
"default"
};
let remove_empty = mode_name == "remove-empty";
if split_by.len() != 1 {
error!("split expects a single character separator");
@@ -28,7 +35,16 @@ pub(crate) fn invoke(function_args: &Vec<String>) -> Vec<String> {
if value.len() > 0 {
let splitted = value.split(split_by_char);
splitted.map(|str_value| str_value.to_string()).collect()
splitted
.map(|str_value| str_value.to_string())
.filter(|string_value| {
if remove_empty && string_value.is_empty() {
false
} else {
true
}
})
.collect()
} else {
vec![]
}
+39 -1
View File
@@ -13,7 +13,12 @@ fn split_invoke_empty() {
#[should_panic]
fn split_invoke_invalid_too_many_args() {
test::on_test_startup();
invoke(&vec!["TEST".to_string(), "1".to_string(), "2".to_string()]);
invoke(&vec![
"TEST".to_string(),
"1".to_string(),
"2".to_string(),
"3".to_string(),
]);
}
#[test]
@@ -47,6 +52,39 @@ fn split_invoke_exists_splitted_space() {
assert_eq!(output, vec!["1", "2", "3", "4"]);
}
#[test]
fn split_invoke_exists_splitted_with_empty_value() {
envmnt::set("TEST_SPLIT_VALUE_WITH_EMPTY_VALUE", "1;2;3;;4");
let mut output = invoke(&vec![
"TEST_SPLIT_VALUE_WITH_EMPTY_VALUE".to_string(),
";".to_string(),
]);
assert_eq!(output, vec!["1", "2", "3", "", "4"]);
output = invoke(&vec![
"TEST_SPLIT_VALUE_WITH_EMPTY_VALUE".to_string(),
";".to_string(),
"default".to_string(),
]);
assert_eq!(output, vec!["1", "2", "3", "", "4"]);
}
#[test]
fn split_invoke_exists_splitted_with_empty_value_removed() {
envmnt::set("TEST_SPLIT_VALUE_WITH_EMPTY_VALUE_REMOVED", "1;2;3;;4");
let output = invoke(&vec![
"TEST_SPLIT_VALUE_WITH_EMPTY_VALUE_REMOVED".to_string(),
";".to_string(),
"remove-empty".to_string(),
]);
assert_eq!(output, vec!["1", "2", "3", "4"]);
}
#[test]
fn split_invoke_exists_not_splitted() {
envmnt::set("TEST_SPLIT_VALUE_NOT_SPLITTED", "1,2,3,4");