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

implement a split filter #517

Merged
merged 5 commits into from
Jun 11, 2024
Merged

implement a split filter #517

merged 5 commits into from
Jun 11, 2024

Conversation

wolfv
Copy link
Contributor

@wolfv wolfv commented Jun 4, 2024

I think it's kinda useful, especially as a counterpart to join.

@@ -557,6 +557,23 @@ mod builtins {
}
}

/// Split a string with a separator
Copy link
Owner

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?

@@ -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> {
Copy link
Owner

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).

Copy link
Contributor Author

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!


match val.as_str() {
Some(s) => {
let elements = s.split(split).map(Value::from).collect::<Vec<Value>>();
Copy link
Owner

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

@mitsuhiko mitsuhiko merged commit d47f4ba into mitsuhiko:main Jun 11, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants