Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pad filter #921

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ Converts a string to uppercase.
#### wordcount
Returns the number of words in a string.

#### pad
Pad a string to a given length with a given fill character.

#### capitalize
Returns the string with all its characters lowercased apart from the first char which is uppercased.

Expand Down
51 changes: 51 additions & 0 deletions src/builtins/filters/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,38 @@ pub fn split(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
Ok(to_value(s.split(&pat).collect::<Vec<_>>()).unwrap())
}

/// Pad the given string to a given length with a given fill character.
pub fn pad(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("pad", "value", String, value);

let len = match args.get("len") {
Some(val) => try_get_value!("pad", "len", usize, val),
None => return Err(Error::msg("Filter `pad` expected an arg called `len`")),
};

let fill = match args.get("fill") {
Some(val) => try_get_value!("pad", "fill", String, val),
None => " ".to_string(),
};

let before = match args.get("before") {
Some(val) => try_get_value!("pad", "before", bool, val),
None => false,
};

let diff = len.checked_sub(s.len()).unwrap_or(0);
let mut output: String = String::with_capacity(std::cmp::max(s.len(), len));
if !before {
output.push_str(&s);
};
output.push_str(&fill.repeat(std::cmp::max(0, diff)));
if before {
output.push_str(&s);
};

Ok(to_value(output).unwrap())
}

/// Convert the value to a signed integer number
pub fn int(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let default = match args.get("default") {
Expand Down Expand Up @@ -809,6 +841,25 @@ mod tests {
}
}

#[test]
fn test_pad() {
let tests: Vec<(&str, i32, &str, bool, &str)> = vec![
("abc", 4, " ", false, "abc "),
("abc", 4, " ", true, " abc"),
("abc", 1, " ", false, "abc"),
("abc", 1, " ", true, "abc"),
("abc", 5, "-", false, "abc--"),
];
for (input, len, fill, before, expected) in tests {
let mut args = HashMap::new();
args.insert("len".to_string(), to_value(len).unwrap());
args.insert("fill".to_string(), to_value(fill).unwrap());
args.insert("before".to_string(), to_value(before).unwrap());
let result = pad(&to_value(input).unwrap(), &args).unwrap();
assert_eq!(result, expected);
}
}

#[test]
fn test_xml_escape() {
let tests = vec![
Expand Down
1 change: 1 addition & 0 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ impl Tera {
self.register_filter("slugify", string::slugify);
self.register_filter("addslashes", string::addslashes);
self.register_filter("split", string::split);
self.register_filter("pad", string::pad);
self.register_filter("int", string::int);
self.register_filter("float", string::float);

Expand Down
Loading