import Foundation /// The names that contact samples are made from, read from contacts.json. public struct ContactNames: Codable, Equatable, Sendable { public var streets: [String] public var cities: [String] public var firstNames: [String] public var lastNames: [String] public init(streets: [String], cities: [String], firstNames: [String], lastNames: [String]) { self.streets = streets self.cities = cities self.firstNames = firstNames self.lastNames = lastNames } public var isComplete: Bool { ![streets, cities, firstNames, lastNames].contains { $0.isEmpty } } /// The names of contacts.json; empty lists when the file is missing or unreadable. public static let bundled: ContactNames = { let empty = ContactNames(streets: [], cities: [], firstNames: [], lastNames: []) guard let url = LoremText.fileURL(named: "contacts.json"), let data = try? Data(contentsOf: url) else { return empty } return (try? JSONDecoder().decode(ContactNames.self, from: data)) ?? empty }() } /// The telephone numbers, addresses and e-mail addresses at the end of lorem text. public struct ContactSample: Equatable, Sendable { public static let count = 4 public static let emailDomains = ["example.nl", "example.com", "example.org"] public static let excludedPostcodeLetters: Set = ["SS", "SD", "SA"] static let areaCodes3 = ["010", "020", "023", "024", "026", "030", "033", "035", "036", "038", "040", "043", "045", "046", "050", "053", "055", "058", "070", "071", "072", "073", "075", "076"] static let areaCodes4 = ["0118", "0162", "0172", "0222", "0251", "0294", "0313", "0318", "0341", "0413", "0475", "0488", "0513", "0546", "0591"] public var phones: [String] public var addresses: [String] public var emails: [String] public init(phones: [String], addresses: [String], emails: [String]) { self.phones = phones self.addresses = addresses self.emails = emails } /// The three groups, each one item per line. public var blocks: [String] { [phones, addresses, emails].map { $0.joined(separator: "\n") } } /// Four distinct items per group; nil when `names` has an empty list or too few names for four e-mails. public static func pick(from names: ContactNames, using generator: inout G) -> ContactSample? { guard names.isComplete else { return nil } let kinds = (PhoneKind.allCases + [PhoneKind.allCases.randomElement(using: &generator)!]).shuffled(using: &generator) var kind = kinds.makeIterator() guard let phones = distinct(using: &generator, { phone(kind: kind.next() ?? .mobile, using: &$0) }), let addresses = distinct(using: &generator, { address(from: names, using: &$0) }), let emails = distinct(using: &generator, { email(from: names, using: &$0) }) else { return nil } return ContactSample(phones: phones, addresses: addresses, emails: emails) } private static func distinct(using generator: inout G, _ make: (inout G) -> String) -> [String]? { var items: [String] = [] for _ in 0..<200 where items.count < count { let item = make(&generator) if !items.contains(item) { items.append(item) } } return items.count == count ? items : nil } private static func digits(_ length: Int, using generator: inout G) -> String { String(Int.random(in: 1...9, using: &generator)) + (1..(kind: PhoneKind, using generator: inout G) -> String { switch kind { case .landline: if Bool.random(using: &generator) { return "\(areaCodes3.randomElement(using: &generator)!)-\(digits(7, using: &generator))" } return "\(areaCodes4.randomElement(using: &generator)!)-\(digits(6, using: &generator))" case .international: return "+31 6 \(digits(8, using: &generator))" case .mobile: return "06-\(digits(8, using: &generator))" } } private static func address(from names: ContactNames, using generator: inout G) -> String { let street = names.streets.randomElement(using: &generator)! let number = Int.random(in: 1...250, using: &generator) let suffix = Int.random(in: 0..<5, using: &generator) == 0 ? String("abc".randomElement(using: &generator)!) : "" let letters = Array("ABCDEGHJKLMNPRSTVWXZ") var pair = "" repeat { pair = String([letters.randomElement(using: &generator)!, letters.randomElement(using: &generator)!]) } while excludedPostcodeLetters.contains(pair) let city = names.cities.randomElement(using: &generator)! return "\(street) \(number)\(suffix), \(Int.random(in: 1000...9999, using: &generator)) \(pair) \(city)" } private static func email(from names: ContactNames, using generator: inout G) -> String { let first = mailPart(names.firstNames.randomElement(using: &generator)!) let last = mailPart(names.lastNames.randomElement(using: &generator)!) return "\(first).\(last)@\(emailDomains.randomElement(using: &generator)!)" } /// Lower case ASCII letters only: `van den Berg` becomes `vandenberg`, `Zoƫ` becomes `zoe`. static func mailPart(_ name: String) -> String { let folded = name.folding(options: [.diacriticInsensitive, .caseInsensitive], locale: Locale(identifier: "en_US_POSIX")) return String(folded.lowercased().unicodeScalars.filter { ("a"..."z").contains($0) }.map(Character.init)) } }