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

Filter: Only remove multiple spaces #48

Merged
merged 4 commits into from
Jun 4, 2017

Conversation

djbe
Copy link
Member

@djbe djbe commented May 30, 2017

Modify the removeNewlines filter to only remove whitespace characters:

  • if they're a newline
  • if there are multiple consecutive whitespace chars
  • if they're at either end of the string (trimming)

Remaining single whitespace characters are converted to a space .

@djbe djbe added this to the 2.0.0 milestone May 30, 2017

| 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 ` |
Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Contributor

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"
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Member Author

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.

@djbe
Copy link
Member Author

djbe commented May 31, 2017

Hmmm, instead of doing multiple regexes, would it be easier (for the removewhitespaces case) to:

  • split based on newlines
  • trim each line
  • join again

@AliSoftware
Copy link
Contributor

As long as the test cases still passes when you change the implementation… 😉
I'm not sure what would be faster, but RegExes even if powerful are also generally slower indeed, so might be worth it yeah

@djbe
Copy link
Member Author

djbe commented May 31, 2017

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:

  • I don't want any spaces at all, anywhere
  • Just remove the newlines and leading whitespaces
  • ...?

@AliSoftware
Copy link
Contributor

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?

@djbe
Copy link
Member Author

djbe commented May 31, 2017

Hmmm no then I need to change the code a bit. Right now it either:

  • only removes newlines and nothing else
  • removes newlines and multiple consecutive spaces + replaces single whitespaces with a space

@djbe
Copy link
Member Author

djbe commented May 31, 2017

Instead of a boolean parameter, should we maybe accept a string to represent a bunch of cases ("all", "leading", ...)?

@AliSoftware
Copy link
Contributor

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.

@djbe djbe force-pushed the feature/only-remove-multiple-spaces branch from 6d0e343 to 35af75f Compare May 31, 2017 10:51
@@ -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"
Copy link
Contributor

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?


return result
switch mode {
case "all":
Copy link
Contributor

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: 
  }
}

@djbe djbe force-pushed the feature/only-remove-multiple-spaces branch from 1b9fd2f to 7b22f79 Compare May 31, 2017 23:06
@djbe
Copy link
Member Author

djbe commented May 31, 2017

Before you ask, the reason that enum is not inside the function is because swiftlint complains that such things can be at most 1 level deep (nesting) 🤷‍♂️

@djbe djbe force-pushed the feature/only-remove-multiple-spaces branch from 7b22f79 to 4a648ab Compare May 31, 2017 23:17
case .leading:
return string
.components(separatedBy: .newlines)
.map { String($0.unicodeScalars.drop(while: { CharacterSet.whitespaces.contains($0) })) }
Copy link
Contributor

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 {
Copy link
Contributor

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

@djbe djbe force-pushed the feature/only-remove-multiple-spaces branch from 4a648ab to 856b15d Compare June 1, 2017 07:27
@djbe djbe merged commit 4a73848 into master Jun 4, 2017
@djbe djbe deleted the feature/only-remove-multiple-spaces branch June 4, 2017 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants