-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Added delimit & sort functions, tests and docs #683
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
Conversation
tpl/template.go
Outdated
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.
Couldn’t that be:
func Delimit(seq interface{}, delimiter, last... string) (string, error) {My thought there is that delimiter should always be a string, and it should be possible with last... to support a “last delimiter” that would fix the “and” problem.
|
The optional "last" would support a specific (and popular) style guide in English, and would be useless to my Norwegian (I would like "første, andre og tredje (first, second and third)). So if you could come up with a "optional last delimiter" syntax it would be perfecto. |
|
In my mind, it would be used like: |
|
Yes, that would work. |
|
It would still work for you the way it's described. |
|
Wow, this got popular all of a sudden. :) |
|
@bjornerik Should the keys be sorted or the values be sorted? |
|
I just pushed the tested changes for adding a last parameter. I'm not 100% sold on the sorting idea. I like that it makes it testable, but other than that, sorting by keys or values just doesn't seem to add value. |
|
When dealing with a {
"a": "apple",
"b": "bacon",
"c": "chocolate",
}you will have a roughly random ordering each time:
It only affects maps (and interfaces?), not sequences. |
|
I get that the order isn't guaranteed, but that's kind of my point. When your data is coming into the delimit function, you've already lost any type of manual ordering, so whether the output is random or sorted, it doesn't seem like it'd matter to you. I guess the only use case I can think of is if you're outputting taxonomies and want the terms in alphabetical order... That would mean sorting by values and not keys for it to make sense on the front end. |
|
Ah. I see what you’re saying. I’d say leave it unsorted, and we can have a separate call to sort a sequence-able object, resulting in |
|
I like the idea of having a custom sort function that can be chained. That already exists for taxonomies, so I think it could be extracted out and generalized for any of the range queries. |
|
Nah, maybe it's just me ... but: I like having automated tests for things; so in this case either:
Having the user take an extra step to get to what should be default sounds fishy. |
|
I don’t have a super strong opinion either way. Testability is good, but I think your better argument is related to page caching—it would be bad if, every time your site was regenerated by Hugo, an indeterminate number of pages that used I still think that |
|
It seems like I'll have to discard the keys and return a slice of the values, otherwise the sort won't be maintained. How does that fit into the context of chained functions? |
|
The next function in the chain would have to handle a slice ... but I guess that is what you would want in this case. Not sure what is correct here, but a sorted slice of values, keys or "key:value" ... |
|
I'm thinking that the function signature will be something like: A blank field will mean that I sort by the keys, otherwise I'll reflect and sort on whatever field they specify. The return interface{} will be a slice of the values from the original sequence. |
|
A little confused by this ... the delimit function returns a string ... so what is the input t Sort? It cannot be the output of Delimit? |
|
I think that this is actually pretty easy to resolve:
To sort based on the values of the map, we need to implement is the behaviour that I’m suggesting be the default. It may be easier to think of this in pipe format: |
|
Ok, I just pushed with a full sort implementation that lets you sort by keys, values or map/struct fields. It has two optional parameters:
|
|
This works great now. The only thing left in my mind as a possible enhancement is to add a third optional parameter that will change sort from returning a slice of the values to returning a slice of the keys. Is that something you'd see yourselves needing? |
|
Very hard to read with so many commits. I would recommend you squash them into one and do a push -f. |
|
I actually did squash them two commits ago and then didn't push them up correctly, hence the next merge. :) I'm resquashing now. |
207abff to
949c5a8
Compare
|
Done :) |
|
This looks great. I’m putting it into my local version and will be trying it out in places that I’m doing some similar stuff in |
949c5a8 to
db6b0f7
Compare
|
@spf13 Can we get this merged now that it's passing all the tests? |
|
Rebase with forced push vs merge, that's the question. I have no idea how the history will look if this was merged into master, but I guess not very pretty. It would be good if you could squash these into one. |
a498b80 to
1de2b31
Compare
|
Ok, I squashed the commits. |
|
It seems like it should be pretty clean to merge at this point. |
- Depends on the delimit function from gohugoio/hugo#683. - Depends on the apply function from gohugoio/hugo#706.
|
I have just made changes to |
- Depends on the delimit function from gohugoio/hugo#683. - Depends on the apply function from gohugoio/hugo#706.
|
@derekperkins This is a fantastic addition. I'm really happy with the feedback and the way it turned out. Keep the great contributions coming. Merged as 717f702 |
With the introduction of delimit in gohugoio#683 it is possible to use it instead of range while also removing the last comma in keywords tag.
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Added a new function, delimit, that takes a slice, array or map and returns a string with all the values delimited by the specified string.