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

Add method for first char uppercased in String #505

Merged
merged 11 commits into from
Aug 11, 2018

Conversation

happiehappie
Copy link
Member

@happiehappie happiehappie commented Jun 20, 2018

Checklist

  • ☑︎ I checked the Contributing Guidelines before creating this request.
  • ☑︎ New extensions are written in Swift 4.
  • ☑︎ New extensions support iOS 8.0+ / tvOS 9.0+ / macOS 10.10+ / watchOS 2.0+.
  • ☑︎ I have added tests for new extensions, and they passed.
  • ☑︎ All extensions have a clear comments explaining their functionality, all parameters and return type in English.
  • ☑︎ All extensions are declared as public.
  • ☑︎ I have added a changelog entry describing my changes.

@happiehappie happiehappie changed the title Add Add method for first char uppercased in String Jun 20, 2018
/// "hello world".firstCharacterUppercased -> Optional("Hello world")
/// "".firstCharacterUppercased -> nil
///
public var firstCharacterUppercased: String? {
Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Member Author

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

@SwifterSwiftBot
Copy link

SwifterSwiftBot commented Jun 20, 2018

4 Messages
📖 Thank you for submitting a pull request to SwifterSwift. The team will review your submission as soon as possible.
📖 iOS: Executed 495 tests, with 0 failures (0 unexpected) in 3.368 (3.813) seconds
📖 tvOS: Executed 465 tests, with 0 failures (0 unexpected) in 2.067 (2.501) seconds
📖 macOS: Executed 334 tests, with 0 failures (0 unexpected) in 1.308 (1.647) seconds

Generated by 🚫 Danger

@happiehappie
Copy link
Member Author

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

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.

Copy link
Member Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 👍

Copy link
Contributor

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.

@codecov
Copy link

codecov bot commented Jun 20, 2018

Codecov Report

Merging #505 into master will increase coverage by 2.17%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#ios 93.87% <100%> (+2.17%) ⬆️
#osx 88.96% <100%> (+2.62%) ⬆️
#tvos 88.96% <100%> (+2.62%) ⬆️
Impacted Files Coverage Δ
...rces/Extensions/SwiftStdlib/StringExtensions.swift 100% <100%> (ø) ⬆️
.../Extensions/SwiftStdlib/DictionaryExtensions.swift 100% <0%> (ø) ⬆️
...urces/Extensions/SwiftStdlib/ArrayExtensions.swift 100% <0%> (ø) ⬆️
...tStdlib/RangeReplaceableCollectionExtensions.swift 100% <0%> (ø)
...SwiftStdlib/RandomAccessCollectionExtensions.swift 100% <0%> (ø)
Sources/Extensions/Shared/ColorExtensions.swift 98.05% <0%> (+0.97%) ⬆️
Sources/Extensions/UIKit/UIImageExtensions.swift 97.82% <0%> (+2.27%) ⬆️
.../Extensions/UIKit/UIViewControllerExtensions.swift 91.89% <0%> (+3%) ⬆️
.../Extensions/UIKit/UICollectionViewExtensions.swift 77.55% <0%> (+4.08%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a695f4c...4896f87. Read the comment docs.

@@ -50,6 +50,11 @@ final class StringExtensionsTests: XCTestCase {
XCTAssertEqual("Hello".firstCharacterAsString, "H")
}

func testFirstCharacterUppercased() {
XCTAssertNotNil("hello world".firstCharacterUppercased)
Copy link
Contributor

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

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

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, "")

Copy link
Member Author

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.

guykogus
guykogus previously approved these changes Jun 20, 2018
/// "hello world".firstCharacterUppercased -> "Hello world"
/// "".firstCharacterUppercased -> ""
///
public var firstCharacterUppercased: String {
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

  1. 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.
  2. I believe we have an agreed standard that anything that isn't constant time is a function, not a property.

Copy link
Member

@SD10 SD10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments

Copy link
Member Author

@happiehappie happiehappie left a 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 {
Copy link
Member Author

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

@guykogus
Copy link
Contributor

@happiehappie still think this should be a func, not a var.

@SD10
Copy link
Member

SD10 commented Jul 14, 2018

Any update on this one @happiehappie?

@happiehappie
Copy link
Member Author

I'll push in coming days, was travelling~

marcocapano
marcocapano previously approved these changes Aug 1, 2018
@marcocapano
Copy link
Member

@happiehappie still willing to make the pull request?

@LucianoPAlmeida
Copy link
Member

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)
Copy link
Member

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

Copy link
Contributor

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.

Copy link
Member

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

@SD10 SD10 merged commit 58be3ee into SwifterSwift:master Aug 11, 2018
@SD10
Copy link
Member

SD10 commented Aug 11, 2018

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! 😃

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.

6 participants