From 7dc6a544b6e7e22e09e0ea8df22faa619658a504 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sat, 10 Jun 2023 15:26:13 +0000 Subject: [PATCH] Enhancement: split function now allows to remove empty values via new third part 'remove-empty' #863 --- CHANGELOG.md | 1 + docs/_includes/content.md | 3 +++ examples/functions.toml | 9 +++++++ src/lib/functions/split_func.rs | 22 ++++++++++++--- src/lib/functions/split_func_test.rs | 40 +++++++++++++++++++++++++++- 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e4e57eb..3c87cad6 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/_includes/content.md b/docs/_includes/content.md index 8c5b2f16..7062e83c 100755 --- a/docs/_includes/content.md +++ b/docs/_includes/content.md @@ -2722,6 +2722,9 @@ args = ["${MULTIPLE_VALUES}"] [cargo-make] INFO - Build Done in 0 seconds. ``` +The split function also supports optional third *mode* attribute.
+If mode: *remove-empty* the output will not include empty values. + #### GetAt diff --git a/examples/functions.toml b/examples/functions.toml index 91ac4f04..202cece6 100644 --- a/examples/functions.toml +++ b/examples/functions.toml @@ -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}"] diff --git a/src/lib/functions/split_func.rs b/src/lib/functions/split_func.rs index 0993ea1a..0ee82d33 100644 --- a/src/lib/functions/split_func.rs +++ b/src/lib/functions/split_func.rs @@ -10,12 +10,19 @@ mod split_func_test; use envmnt; pub(crate) fn invoke(function_args: &Vec) -> Vec { - 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) -> Vec { 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![] } diff --git a/src/lib/functions/split_func_test.rs b/src/lib/functions/split_func_test.rs index b697481c..c042fb28 100644 --- a/src/lib/functions/split_func_test.rs +++ b/src/lib/functions/split_func_test.rs @@ -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");