From 41a0bf3e0a303b72da8ec86de76334cb347f59e9 Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Mon, 27 May 2024 09:05:14 -1000 Subject: [PATCH] allow escaped newline and tab join separator (#884) --- src/builtins/filters/array.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/builtins/filters/array.rs b/src/builtins/filters/array.rs index f6986be1..f050a6a1 100644 --- a/src/builtins/filters/array.rs +++ b/src/builtins/filters/array.rs @@ -50,7 +50,13 @@ pub fn last(value: &Value, _: &HashMap) -> Result { pub fn join(value: &Value, args: &HashMap) -> Result { let arr = try_get_value!("join", "value", Vec, value); let sep = match args.get("sep") { - Some(val) => try_get_value!("truncate", "sep", String, val), + Some(val) => { + let s = try_get_value!("truncate", "sep", String, val); + // When reading from a file, it will escape `\n` to `\\n` for example so we need + // to replace double escape. In practice it might cause issues if someone wants to join + // with `\\n` for real but that seems pretty unlikely + s.replace("\\n", "\n").replace("\\t", "\t") + } None => String::new(), }; @@ -387,6 +393,14 @@ mod tests { assert_eq!(result.unwrap(), to_value("").unwrap()); } + #[test] + fn test_join_newlines_and_tabs() { + let mut args = HashMap::new(); + args.insert("sep".to_owned(), to_value(",\\n\\t").unwrap()); + let result = join(&to_value(vec!["Cats", "Dogs"]).unwrap(), &args); + assert_eq!(result.unwrap(), to_value("Cats,\n\tDogs").unwrap()); + } + #[test] fn test_sort() { let v = to_value(vec![3, -1, 2, 5, 4]).unwrap();