Skip to content

Commit

Permalink
allow escaped newline and tab join separator (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfeld authored May 27, 2024
1 parent 9653290 commit 41a0bf3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/builtins/filters/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ pub fn last(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
pub fn join(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let arr = try_get_value!("join", "value", Vec<Value>, 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(),
};

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 41a0bf3

Please sign in to comment.