Cotton code
32 lines · 1.3 KB · SwiftRaw
// A person on a card: who, and the picture to draw when there is one.import Foundation/// A person on a card: who, and the picture to draw when there is one.public struct CardPerson: Equatable, Sendable {    public var id: String    public var name: String    public var pictureKey: String?    public init(id: String, name: String, pictureKey: String?) {        self.id = id        self.name = name        self.pictureKey = pictureKey    }    /// A name's initials for a print with no picture: the first letter of its    /// first and last words ("Asha Rao" → "AR", "Asha" → "A"); nil for no name.    public static func initials(_ name: String) -> String? {        let words = name.split(whereSeparator: { $0.isWhitespace }).filter { $0.first?.isLetter == true }        guard let first = words.first?.first else { return nil }        let last = words.count > 1 ? words.last?.first.map { String($0) } ?? "" : ""        return (String(first) + last).uppercased()    }    /// The name you would use out loud: the first word of a whole name; a    /// single-word name is itself.    public static func firstName(_ fullName: String) -> String {        let parts = fullName.split(whereSeparator: \.isWhitespace)        return parts.first.map(String.init) ?? fullName    }}