-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 method for first char uppercased in String #505
Conversation
/// "hello world".firstCharacterUppercased -> Optional("Hello world") | ||
/// "".firstCharacterUppercased -> nil | ||
/// | ||
public var firstCharacterUppercased: 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.
Why not just use capitalized
, or better yet localizedCapitalized
?
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.
Because capitalize will uppercase first characters in every word, not practical when your localized strings are in the form of a sentence message
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 using localizedCapitalized in this code is simply because localizedCapitalized's deployment target is 9.0
Generated by 🚫 Danger |
Is the unit test for SwifterSwiftTests passed in current master? I didn't change that file though o.O |
/// "hello world".firstCharacterUppercased -> Optional("Hello world") | ||
/// "".firstCharacterUppercased -> nil | ||
/// | ||
public var firstCharacterUppercased: 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.
I don't think this should return nil
for an empty string, it should just return the empty string. That would be more consistent with the other methods. E.g. "".uppercased()
returns ""
not nil
.
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.
Shall I change the return type to String than an optional string then?
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.
Yep 👍
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.
Don't forget to update your tests, too.
…to be non-optional
Codecov Report
@@ Coverage Diff @@
## master #505 +/- ##
==========================================
+ Coverage 91.7% 93.87% +2.17%
==========================================
Files 64 66 +2
Lines 2760 2791 +31
==========================================
+ Hits 2531 2620 +89
+ Misses 229 171 -58
Continue to review full report at Codecov.
|
@@ -50,6 +50,11 @@ final class StringExtensionsTests: XCTestCase { | |||
XCTAssertEqual("Hello".firstCharacterAsString, "H") | |||
} | |||
|
|||
func testFirstCharacterUppercased() { | |||
XCTAssertNotNil("hello world".firstCharacterUppercased) |
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 isn't a useful test, since the return value of the method isn't optional. Better to test the empty string.
/// "".firstCharacterUppercased -> "" | ||
/// | ||
public var firstCharacterUppercased: String { | ||
guard let first = first else { return "" } |
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 return self
, not return ""
. I know that it's most likely to be the same, but you may catch some edge-cases where there's an unfinished unicode character or something.
@@ -51,7 +51,7 @@ final class StringExtensionsTests: XCTestCase { | |||
} | |||
|
|||
func testFirstCharacterUppercased() { | |||
XCTAssertNotNil("hello world".firstCharacterUppercased) | |||
XCTAssertNotNil("".firstCharacterUppercased) |
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.
Again, checking that it's not nil
is always going to be true
. I meant more like XCTAssertEqual("".firstCharacterUppercased, "")
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.
Damn lol I totally misunderstood you, my bad.
/// "hello world".firstCharacterUppercased -> "Hello world" | ||
/// "".firstCharacterUppercased -> "" | ||
/// | ||
public var firstCharacterUppercased: 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.
Actually, I think we should make this a method and not a property. Most String
operations in Swift are methods because they're not constant time operations
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.
capitalized is a property though, but then again uppercased is () :x
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.
Let's follow the same pattern as uppercased
@happiehappie
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.
Agreed.
- A copy of a string with its first letter uppercased isn't a property of the string, it's a transformation, so semantically it follows that it's a function.
- I believe we have an agreed standard that anything that isn't constant time is a function, not a property.
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.
See comments
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.
Thoughts?
/// "hello world".firstCharacterUppercased -> "Hello world" | ||
/// "".firstCharacterUppercased -> "" | ||
/// | ||
public var firstCharacterUppercased: 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.
capitalized is a property though, but then again uppercased is () :x
@happiehappie still think this should be a |
Any update on this one @happiehappie? |
I'll push in coming days, was travelling~ |
@happiehappie still willing to make the pull request? |
Hey @happiehappie :)) Can you update this branch with master? |
@@ -105,7 +105,7 @@ public extension String { | |||
return String(first) | |||
} | |||
|
|||
#if canImport(Foundation) | |||
#if canImport(Foundation) |
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.
Do we really need this check here? 🤔 I mean ... String is a type from the stdlib and to me since there's no bridging to NSString or usage of any foundation method it seems like there's no need for that :))
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.
String
actually has some extensions in Foundation
, e.g. when using Data
, which is part of Foundation
, not the stdlib. Still, we should only be using this check for those situations where we use Foundation
functions/classes.
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.
Hmmmm. Guess that doesn't hurt
Thank you for contributing to SwifterSwift! I've invited you to join the SwifterSwift GitHub organization - no pressure to accept! If you'd like more information on what that means, check out our contributing guidelines. Feel free to reach out if you have any questions! 😃 |
Checklist