-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
implement a split
filter
#517
Conversation
minijinja/src/filters.rs
Outdated
@@ -557,6 +557,23 @@ mod builtins { | |||
} | |||
} | |||
|
|||
/// Split a string with a separator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an example here?
minijinja/src/filters.rs
Outdated
@@ -557,6 +557,23 @@ mod builtins { | |||
} | |||
} | |||
|
|||
/// Split a string with a separator | |||
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))] | |||
pub fn split(val: Value, split: Option<Cow<'_, str>>) -> Result<Value, Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change this filter in two ways so it's similar to the .split
function in Python:
- if
None
or an empty string is passed (or nothing at all), it splits on any whitespace (split_whitespace
). - a second parameter (
nsplits
) is added which indicates how many times the string is to be split.1
means one split (eg: two parts),2
means two splits (eg: three parts).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright - yeah, I was looking for your input before continuing. Sounds like a plan!
minijinja/src/filters.rs
Outdated
|
||
match val.as_str() { | ||
Some(s) => { | ||
let elements = s.split(split).map(Value::from).collect::<Vec<Value>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be returned as iterable rather than collected into a Vec
I think it's kinda useful, especially as a counterpart to
join
.