-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move formatLinkLabel utility into its own class, with a test
- Loading branch information
1 parent
787cbd5
commit ce7c8f8
Showing
4 changed files
with
48 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |