-
Notifications
You must be signed in to change notification settings - Fork 58
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
Filter: Only remove multiple spaces #48
Conversation
Documentation/filters-strings.md
Outdated
|
||
| Input | Output | | ||
|-----------------------|-----------------------| | ||
| ` \ntest` | ` test` | | ||
| `test \n\t ` | `test \t ` | | ||
| `test\n test` | `test test` | | ||
| `\r\ntest\n test\n` | `test test` | | ||
| ` test test ` | ` test test ` | |
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 why the beginning & end whitespaces would be kept there? I'd expect them to be removed by .trimmingCharacters(in: .whitespacesAndNewlines)
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.
This is for the false case, where only newlines are removed.This is
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, right.
"test3 \n ": "test3", | ||
"test4\n \ntest": "test4test", | ||
"\n test5\n \ntest test \n ": "test5test test", | ||
"test6\ntest": "test6test" |
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.
Maybe add that " test test ":"test test"
here to confirm my suggestion about that it should remove the leading/trailing whitespaces in the examples you put in the md docs above
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.
Still, I like that the examples we give in the MD documentation are used in the tests as well, if only to be sure that we are consistent with what we state in the doc and that those cases are indeed checked
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'll add that specific case, but I'll number it (easier for debugging if needed) like the rest.
Hmmm, instead of doing multiple regexes, would it be easier (for the removewhitespaces case) to:
|
As long as the test cases still passes when you change the implementation… 😉 |
I'm just wondering what would be useful for users (and our use cases). The main one will be the ability to write things like macros on multiple lines but still have the result be one line. This might mean multiple things though:
|
That's pretty much the use cases I see indeed. Creating some identifier or word from a complex expression (= no spaces at all anywhere) or formatting the template nicely (remove newlines and leading spaces per lines), so I think this covers all we need for now? |
Hmmm no then I need to change the code a bit. Right now it either:
|
Instead of a boolean parameter, should we maybe accept a string to represent a bunch of cases ("all", "leading", ...)? |
Mmmh I was gonna say YAGNI, but given that the different cases are not that straightforward to explain ("remove whitespaces except this but that…"), a string instead of a bool might be more explicit anyway, so yeah, go for it. |
6d0e343
to
35af75f
Compare
Sources/Filters+Strings.swift
Outdated
@@ -120,13 +120,23 @@ extension Filters { | |||
} | |||
|
|||
static func removeNewlines(_ value: Any?, arguments: [Any?]) throws -> Any? { | |||
let removeSpaces = try Filters.parseBool(from: arguments, index: 0, required: false) ?? true | |||
let mode = arguments.first as? String ?? "all" |
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.
Shouldn't we throw if the argument is not a valid string, like removeNewlines:42
?
So if there's no argument at all (arguments.first == nil
), agreed that all
should be the default. But if the argument isn't castable as a as? String
, maybe throw?
Sources/Filters+Strings.swift
Outdated
|
||
return result | ||
switch mode { | ||
case "all": |
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.
Use some constants instead of magic strings here? Even if those constants are scoped inside the func.
e.g.
static func removeNewlines(_ value: Any? arguments: [Any?]) throws -> Any? {
enum Modes: String {
case all, leading
}
let modeArg = String(describing: arguments.first) ?? Modes.all.rawValue
guard let mode = Modes(rawValue: modeArg) else {
throw Filters.Error.invalidOption(option: modeArg)
}
switch mode {
case .all: …
case .leading: …
}
}
1b9fd2f
to
7b22f79
Compare
Before you ask, the reason that enum is not inside the function is because |
7b22f79
to
4a648ab
Compare
Sources/Filters+Strings.swift
Outdated
case .leading: | ||
return string | ||
.components(separatedBy: .newlines) | ||
.map { String($0.unicodeScalars.drop(while: { CharacterSet.whitespaces.contains($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.
Codebeat seems to be unhappy with nesting too deep here, maybe separate the closure used in that map in a private function to make it happy?
XCTAssertEqual(value, Test.bar) | ||
} | ||
|
||
func testParseEnum_WithBazString() throws { |
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.
Add a test with a non-zero index argument too
4a648ab
to
856b15d
Compare
Modify the
removeNewlines
filter to only remove whitespace characters:Remaining single whitespace characters are converted to a space
.