-
Notifications
You must be signed in to change notification settings - Fork 225
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
Fixed using spaces in filter expressions and variables lists #178
Conversation
140f880
to
d1507d3
Compare
Sources/Tokenizer.swift
Outdated
} | ||
} | ||
|
||
// joins back components around characters used in variables lists and filters | ||
private func smartJoin(_ components: [String]) -> [String] { |
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.
Does this have performance implications?
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.
@yonaskolb thanks for pointing it out =) I've reimplemented it, should be faster now.
8ffc3a1
to
2fcb427
Compare
@kylef can we merge this? I believe this will improve templates readability |
0218a91
to
118c3f7
Compare
Sources/Tokenizer.swift
Outdated
let specialCharacters = ",|:" | ||
func appendWord(_ word: String) { | ||
if components.count > 0 && | ||
(specialCharacters.characters.contains(components.last!.characters.last!) || |
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.
😱 all those force unwraps 😢
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.
🤷♂️ it's all safe 🙄
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.
if let lastCompChar = components.last?.characters.last, (specialCharacters.contains(lastCompChar) || …)
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.
ok, ok, pony saved 🦄 😄
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.
@AliSoftware done
Sources/Tokenizer.swift
Outdated
} else { | ||
components.append(word) | ||
} | ||
} |
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.
Wouldn't it be more logical to use a Set<Character>
here which would seem more appropriate and already handle all that append-without-duplicates logic for you already?
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.
That's not exactly what we are doing here. We are checking if a word is a special character (for the case when it is surrounded by spaces, i.e. in |
), or if previous word ends with a special character, in which case we concatenate last and new word, so ars| default
or ars | default
(but not ars |default
) ends up as ars|default
.
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.
Ah right gotcha
$0.it("can render a filter") { | ||
let templateString = "{% for article in ars|default:articles %}" + | ||
$0.it("can render a filter with spaces") { | ||
let templateString = "{% for article in ars | default: a, b , articles %}" + |
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.
Not sure I'm familiar with that default filter accepting 3 arguments?
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.
It supports variadic fallbacks for the default value. If a
and b
are not defined then articles would be there.
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.
Cool! TIL 👍
CHANGELOG.md
Outdated
@@ -24,6 +24,7 @@ | |||
- Integer literals now resolve into Int values, not Float | |||
- Fixed accessing properties of optional properties via reflection | |||
- No longer render optional values in arrays as `Optional(..)` | |||
- Adds support for using spaces in filter expression |
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 this is an enhancement not a bug fix, it was never a bug it just wasn't designed to work like that before.
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 only considered is as a bug as docs for for
tag actually use spaces, but I move it to new features section.
@AliSoftware @yonaskolb is it good to go? |
I'm still not a fan of force-unwrap even especially when we have better constructs to use instead |
When parsing filter expression spaces were not trimmed in filter name and parameters. Also this fixes using spaces in any other nodes where smart split by spaces is used, i.e. in for node. It allows use of
a, b
ora , b
, but nota ,b
. The same about|
and:
separators. So this also fixes #169