Skip to content

Commit

Permalink
Move formatLinkLabel utility into its own class, with a test
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-at-kickstarter committed May 7, 2024
1 parent 787cbd5 commit ce7c8f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,39 +282,13 @@ final class PledgePaymentMethodsViewController: UIViewController {
return nil
}

let formattedLabel = self.formatLinkLabel(paymentMethod.label)
let formattedLabel = KSRStripeLink.formatLinkLabel(paymentMethod.label)
return PaymentSheetPaymentOptionsDisplayData(
image: paymentMethod.image,
label: formattedLabel ?? paymentMethod.label
)
}

private func formatLinkLabel(_ label: String) -> String? {
// Link gives us a label like "Visa 1234"; reformat it to match our UI
do {
// Find 4 digits in the string
let regex = try NSRegularExpression(pattern: "\\d{4}")
let matches = regex.matches(
in: label,
range: NSRange(location: 0, length: label.count)
)

guard let match = matches.first else {
return nil
}

guard let range = Range(match.range, in: label) else {
return nil
}

let lastFour = label[range]
return "•••• \(lastFour)"

} catch {
return nil
}
}

private func updateAddNewPaymentMethodButtonLoading(state: Bool) {
self.dataSource.updateAddNewPaymentCardLoad(state: state)

Expand Down
8 changes: 8 additions & 0 deletions Kickstarter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,8 @@
E182E5BA2B8CDFDE0008DD69 /* AppEnvironmentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7ED1F121E830FDC00BFFA01 /* AppEnvironmentTests.swift */; };
E182E5BC2B8D36FE0008DD69 /* AppEnvironmentTests+OAuthInKeychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = E182E5BB2B8D36FE0008DD69 /* AppEnvironmentTests+OAuthInKeychain.swift */; };
E1972EA22BB4722D002517E6 /* PostCampaignCheckoutViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1972EA12BB4722D002517E6 /* PostCampaignCheckoutViewModelTests.swift */; };
E1982FE52BEABF8B0057BD09 /* KSRStripeLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1982FE32BEABF8B0057BD09 /* KSRStripeLink.swift */; };
E1982FE62BEABF8F0057BD09 /* KSRStripeLinkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1982FE22BEABF8B0057BD09 /* KSRStripeLinkTests.swift */; };
E1A149202ACDD7BF00F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryData.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A1491F2ACDD7BF00F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryData.swift */; };
E1A149222ACE013100F49709 /* FetchProjectsEnvelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A149212ACE013100F49709 /* FetchProjectsEnvelope.swift */; };
E1A149242ACE02B300F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A149232ACE02B300F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryDataTests.swift */; };
Expand Down Expand Up @@ -3162,6 +3164,8 @@
E182E5BB2B8D36FE0008DD69 /* AppEnvironmentTests+OAuthInKeychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppEnvironmentTests+OAuthInKeychain.swift"; sourceTree = "<group>"; };
E1889D8D2B6065D6004FBE21 /* CombineTestObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineTestObserverTests.swift; sourceTree = "<group>"; };
E1972EA12BB4722D002517E6 /* PostCampaignCheckoutViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostCampaignCheckoutViewModelTests.swift; sourceTree = "<group>"; };
E1982FE22BEABF8B0057BD09 /* KSRStripeLinkTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KSRStripeLinkTests.swift; path = Library/KSRStripeLinkTests.swift; sourceTree = SOURCE_ROOT; };
E1982FE32BEABF8B0057BD09 /* KSRStripeLink.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KSRStripeLink.swift; path = Library/KSRStripeLink.swift; sourceTree = SOURCE_ROOT; };
E1A1491F2ACDD7BF00F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FetchProjectsEnvelope+FetchBackerProjectsQueryData.swift"; sourceTree = "<group>"; };
E1A149212ACE013100F49709 /* FetchProjectsEnvelope.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchProjectsEnvelope.swift; sourceTree = "<group>"; };
E1A149232ACE02B300F49709 /* FetchProjectsEnvelope+FetchBackerProjectsQueryDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FetchProjectsEnvelope+FetchBackerProjectsQueryDataTests.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6120,6 +6124,8 @@
A71404381CAF215900A2795B /* KeyValueStoreType.swift */,
A71F59E71D2424CA00909BE3 /* KSCache.swift */,
A7ED1F171E830FDC00BFFA01 /* KSCacheTests.swift */,
E1982FE32BEABF8B0057BD09 /* KSRStripeLink.swift */,
E1982FE22BEABF8B0057BD09 /* KSRStripeLinkTests.swift */,
A7C725821C85D36D005A016B /* Language.swift */,
A7ED1F181E830FDC00BFFA01 /* LanguageTests.swift */,
A7C725831C85D36D005A016B /* LaunchedCountries.swift */,
Expand Down Expand Up @@ -7945,6 +7951,7 @@
59B0E07E1D147F340081D2DC /* DashboardStyles.swift in Sources */,
470B771A26FCDC8900EBD5CA /* RiskMessagingViewModel.swift in Sources */,
0169F8C11D6CA27500C8D5C5 /* RootCategory.swift in Sources */,
E1982FE52BEABF8B0057BD09 /* KSRStripeLink.swift in Sources */,
D04AAC21218BB70D00CF713E /* ChangePasswordViewModel.swift in Sources */,
8A32FE0624D233F000F79C72 /* EmptyStateViewModel.swift in Sources */,
37FEFBC8222F1E4F00FCA608 /* ProcessInfoType.swift in Sources */,
Expand Down Expand Up @@ -8044,6 +8051,7 @@
A7ED1F281E830FDC00BFFA01 /* CircleAvatarImageViewTests.swift in Sources */,
606C45F829FACD9F001BA067 /* RemoteConfigFeature+HelpersTests.swift in Sources */,
A7ED1FB81E831C5C00BFFA01 /* BackingCellViewModelTests.swift in Sources */,
E1982FE62BEABF8F0057BD09 /* KSRStripeLinkTests.swift in Sources */,
A7ED1FFD1E831C5C00BFFA01 /* ProjectUpdatesViewModelTests.swift in Sources */,
191E60262A953E2B001413B2 /* ProjectTabCheckmarkListCellViewModelTests.swift in Sources */,
A7ED1F341E830FDC00BFFA01 /* SharedFunctionsTests.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions Library/KSRStripeLink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

public class KSRStripeLink {
// Link gives us a label like "Visa 1234"; reformat it to match our UI
public static func formatLinkLabel(_ label: String) -> String? {
do {
// Find 4 digits in the string
let regex = try NSRegularExpression(pattern: "\\d{4}")
let matches = regex.matches(
in: label,
range: NSRange(location: 0, length: label.count)
)

guard let match = matches.first else {
return nil
}

guard let range = Range(match.range, in: label) else {
return nil
}

let lastFour = label[range]
return "•••• \(lastFour)"

} catch {
return nil
}
}
}
10 changes: 10 additions & 0 deletions Library/KSRStripeLinkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@testable import Library
import XCTest

final class KSRStripeLinkTests: XCTestCase {
func testFormatLinkLabel() {
XCTAssertEqual(KSRStripeLink.formatLinkLabel("Visa 1234"), "•••• 1234")
XCTAssertEqual(KSRStripeLink.formatLinkLabel("1234 Visa"), "•••• 1234")
XCTAssertEqual(KSRStripeLink.formatLinkLabel("Foobar"), nil)
}
}

0 comments on commit ce7c8f8

Please sign in to comment.