Compare commits

...

2 Commits

Author SHA1 Message Date
0895013cdc updated README with comments
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2025-01-20 19:43:01 -06:00
9849636a1b added telephone number formatter
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2025-01-20 19:42:42 -06:00
3 changed files with 43 additions and 2 deletions

View File

@ -0,0 +1,40 @@
//
// String.swift
// EmployeeDirectory
//
// Created by Matt Bruce on 1/20/25.
//
import Foundation
extension String {
/// Formats a string into a US phone number format (XXX-XXX-XXXX).
/// Non-numeric characters are removed, and formatting is applied based on the length of the string.
/// - Returns: A formatted phone number as a string.
internal func formatUSNumber() -> String {
// format the number
return format(with: "XXX-XXX-XXXX", phone: self)
}
internal func format(with mask: String, phone: String) -> String {
let numbers = filter { $0.isNumber }
var result = ""
var index = numbers.startIndex // numbers iterator
// iterate over the mask characters until the iterator of numbers ends
for ch in mask where index < numbers.endIndex {
if ch == "X" {
// mask requires a number in this place, so take the next one
result.append(numbers[index])
// move numbers iterator to the next index
index = numbers.index(after: index)
} else {
result.append(ch) // just append a mask character
}
}
return result
}
}

View File

@ -27,7 +27,7 @@ public class EmployeeCellViewModel: ObservableObject {
// Initialize properties
self.uuid = employee.uuid.uuidString
self.fullName = employee.fullName
self.phoneNumber = employee.phoneNumber
self.phoneNumber = employee.phoneNumber?.formatUSNumber()
self.emailAddress = employee.emailAddress
self.biography = employee.biography
self.team = employee.team

View File

@ -15,6 +15,7 @@
## What do you think is the weakest part of your project?
## Did you copy any code or dependencies? Please make sure to attribute them here!
### String.swift extension for dealing with TelephoneNumber from a generic regEx formatter I found on stackoverflow.
## Is there any other information youd like us to know?