Skip to content

Commit

Permalink
Render arrays without trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
aromeronavia committed Jun 13, 2023
1 parent 23ca8d7 commit 7d78793
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/helpers/helper_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,16 @@ mod test {
assert_eq!("else block", rendered);
}

#[test]
fn test_render_array_without_trailig_commas() {
let reg = Registry::new();
let template = "Array: {{array}}";
let input = json!({"array": [1, 2, 3]});
let rendered = reg.render_template(template, &input);

assert_eq!("Array: [1, 2, 3]", rendered.unwrap());
}

#[test]
fn test_recursion() {
let mut reg = Registry::new();
Expand All @@ -534,15 +544,15 @@ mod test {
"string": "hi"
});
let expected_output = "(\
array: [42, [object], [[], ], ] (\
array: [42, [object], [[]]] (\
0: 42 (), \
1: [object] (wow: cool (), ), \
2: [[], ] (0: [] (), ), \
2: [[]] (0: [] (), ), \
), \
object: [object] (\
a: [object] (\
b: c (), \
d: [e, ] (0: e (), ), \
d: [e] (0: e (), ), \
), \
), \
string: hi (), \
Expand Down
9 changes: 6 additions & 3 deletions src/json/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ impl JsonRender for Json {
Json::Array(ref a) => {
let mut buf = String::new();
buf.push('[');
for i in a.iter() {
buf.push_str(i.render().as_ref());
buf.push_str(", ");
for (i, value) in a.iter().enumerate() {
buf.push_str(value.render().as_ref());

if i < a.len() - 1 {
buf.push_str(", ");
}
}
buf.push(']');
buf
Expand Down

0 comments on commit 7d78793

Please sign in to comment.