-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 an optional limit argument to split filter #1801
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR! The typo in the PR title should be corrected to "Optional" I guess. @gooroodev, can you review this too please? |
lib/liquid/standardfilters.rb
Outdated
def split(input, pattern) | ||
input.to_s.split(pattern.to_s) | ||
def split(input, pattern, limit = nil) | ||
limit = limit.respond_to?(:to_i) ? limit.to_i : 0 |
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 think we should use Liquid::Utils.to_integer
here.
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.
Oh! I didn't know. I will update the PR ASAP.
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.
As per my research Liquid::Utils.to_integer
might not be helpful since it doesn't return 0 if an empty string gets passed. Not sure if I am too sensitive here or the 'to_integer' method needs some update.
What do you suggest? @ianks
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.
Sorry I meant Liquid::Utils.to_number
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 made the requested change. Also I updated the method docs.
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 wonder if it is necessary to ensure any input for limit
gets converted to integer.
I think using to_integer
is good enough and it throws a good error.
I raise this issue here because it might be better to be more strict at first and open up if future. Otherwise, it becomes a breaking change if we want to strict it later.
f206f7e
to
ffcfa27
Compare
This PR adds an optional limit argument to the
split
filter in Liquid to make it work similar to the Rubysplit
method.Closes #1800