Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code with SwiftFormat and removed empty catches #726

Merged
merged 1 commit into from Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 7 additions & 32 deletions .swiftformat
@@ -1,33 +1,8 @@
--allman false
--binarygrouping none
--closingparen balanced
--commas inline
--conflictmarkers reject
--decimalgrouping none
--elseposition same-line
--empty void
--exponentcase lowercase
--exponentgrouping disabled
--fractiongrouping disabled
--fragment false
--header ignore
--hexgrouping none
--hexliteralcase uppercase
--ifdef outdent
--importgrouping alphabetized
--indent 4
--indentcase false
--linebreaks lf
--octalgrouping none
--operatorfunc spaced
--patternlet inline
--swiftversion 5.3
--xcodeindentation enabled
--disable unusedArguments, redundantReturn, redundantSelf, trailingCommas, trailingClosures, wrapArguments, wrapMultilineStatementBraces
--enable blockComments, isEmpty
--emptybraces spaced
--ifdef no-indent
--importgrouping testable-first
--ranges no-space
--self insert
--selfrequired
--semicolons inline
--stripunusedargs closure-only
--trailingclosures
--trimwhitespace always
--wraparguments before-first
--wrapcollections before-first
--xcodeindentation disabled
14 changes: 7 additions & 7 deletions PhoneNumberKit/Bundle+Resources.swift
@@ -1,6 +1,6 @@
import Foundation

private class CurrentBundleFinder {}
private class CurrentBundleFinder { }

// The custom bundle locator code is needed to work around a bug in Xcode
// where SwiftUI previews in an SPM module will crash if they try to use
Expand All @@ -12,15 +12,15 @@ extension Bundle {
#if DEBUG && SWIFT_PACKAGE
let bundleName = "PhoneNumberKit_PhoneNumberKit"
let candidates = [
/* Bundle should be present here when the package is linked into an App. */
// Bundle should be present here when the package is linked into an App.
Bundle.main.resourceURL,
/* Bundle should be present here when the package is linked into a framework. */
// Bundle should be present here when the package is linked into a framework.
Bundle(for: CurrentBundleFinder.self).resourceURL,
/* For command-line tools. */
// For command-line tools.
Bundle.main.bundleURL,
/* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */
// Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/").
Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),
/* Bundle should be present here when running previews from a framework which imports framework whick imports PhoneNumberKit package (this is the path to "…/Debug-iphonesimulator/"). */
// Bundle should be present here when running previews from a framework which imports framework whick imports PhoneNumberKit package (this is the path to "…/Debug-iphonesimulator/").
Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent()
]
for candidate in candidates {
Expand All @@ -30,7 +30,7 @@ extension Bundle {
}
}
#endif

#if SWIFT_PACKAGE
return Bundle.module
#else
Expand Down
62 changes: 29 additions & 33 deletions PhoneNumberKit/Constants.swift
Expand Up @@ -19,18 +19,16 @@ enum PhoneNumberCountryCodeSource {

// MARK: Public Enums

/**
Enumeration for parsing error types

- GeneralError: A general error occured.
- InvalidCountryCode: A country code could not be found or the one found was invalid
- InvalidNumber: The string provided is not a number
- TooLong: The string provided is too long to be a valid number
- TooShort: The string provided is too short to be a valid number
- Deprecated: The method used was deprecated
- metadataNotFound: PhoneNumberKit was unable to read the included metadata
- ambiguousNumber: The string could not be resolved to a single valid number
*/
/// Enumeration for parsing error types
///
/// - GeneralError: A general error occured.
/// - InvalidCountryCode: A country code could not be found or the one found was invalid
/// - InvalidNumber: The string provided is not a number
/// - TooLong: The string provided is too long to be a valid number
/// - TooShort: The string provided is too short to be a valid number
/// - Deprecated: The method used was deprecated
/// - metadataNotFound: PhoneNumberKit was unable to read the included metadata
/// - ambiguousNumber: The string could not be resolved to a single valid number
public enum PhoneNumberError: Error, Equatable {
case generalError
case invalidCountryCode
Expand Down Expand Up @@ -63,21 +61,19 @@ public enum PhoneNumberFormat {
case national // 06 89 12 34 56
}

/**
Phone number type enumeration
- fixedLine: Fixed line numbers
- mobile: Mobile numbers
- fixedOrMobile: Either fixed or mobile numbers if we can't tell conclusively.
- pager: Pager numbers
- personalNumber: Personal number numbers
- premiumRate: Premium rate numbers
- sharedCost: Shared cost numbers
- tollFree: Toll free numbers
- voicemail: Voice mail numbers
- vOIP: Voip numbers
- uan: UAN numbers
- unknown: Unknown number type
*/
/// Phone number type enumeration
/// - fixedLine: Fixed line numbers
/// - mobile: Mobile numbers
/// - fixedOrMobile: Either fixed or mobile numbers if we can't tell conclusively.
/// - pager: Pager numbers
/// - personalNumber: Personal number numbers
/// - premiumRate: Premium rate numbers
/// - sharedCost: Shared cost numbers
/// - tollFree: Toll free numbers
/// - voicemail: Voice mail numbers
/// - vOIP: Voip numbers
/// - uan: UAN numbers
/// - unknown: Unknown number type
public enum PhoneNumberType: String, Codable {
case fixedLine
case mobile
Expand All @@ -95,13 +91,13 @@ public enum PhoneNumberType: String, Codable {
}

public enum PossibleLengthType: String, Codable {
case national
case localOnly
case national
case localOnly
}

// MARK: Constants

struct PhoneNumberConstants {
enum PhoneNumberConstants {
static let defaultCountry = "US"
static let defaultExtnPrefix = " ext. "
static let longPhoneNumber = "999999999999999"
Expand All @@ -118,14 +114,14 @@ struct PhoneNumberConstants {
static let separatorBeforeNationalNumber = " "
}

struct PhoneNumberPatterns {
enum PhoneNumberPatterns {
// MARK: Patterns

static let firstGroupPattern = "(\\$\\d)"
static let fgPattern = "\\$FG"
static let npPattern = "\\$NP"

static let allNormalizationMappings = ["0":"0", "1":"1", "2":"2", "3":"3", "4":"4", "5":"5", "6":"6", "7":"7", "8":"8", "9":"9", "٠":"0", "١":"1", "٢":"2", "٣":"3", "٤":"4", "٥":"5", "٦":"6", "٧":"7", "٨":"8", "٩":"9", "۰":"0", "۱":"1", "۲":"2", "۳":"3", "۴":"4", "۵":"5", "۶":"6", "۷":"7", "۸":"8", "۹":"9", "*":"*", "#":"#", ",":",", ";":";"]
static let allNormalizationMappings = ["0": "0", "1": "1", "2": "2", "3": "3", "4": "4", "5": "5", "6": "6", "7": "7", "8": "8", "9": "9", "٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4", "٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9", "۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4", "۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9", "*": "*", "#": "#", ",": ",", ";": ";"]
static let capturingDigitPattern = "([0-90-9٠-٩۰-۹])"

static let extnPattern = "(?:;ext=([0-90-9٠-٩۰-۹]{1,7})|[ \\t,]*(?:e?xt(?:ensi(?:ó?|ó))?n?|e?xtn?|[,xxX##~~;]|int|anexo|int)[:\\..]?[ \\t,-]*([0-90-9٠-٩۰-۹]{1,7})#?|[- ]+([0-90-9٠-٩۰-۹]{1,5})#)$"
Expand Down Expand Up @@ -153,6 +149,6 @@ struct PhoneNumberPatterns {
static let validStartPattern = "[++0-90-9٠-٩۰-۹]"

static let validPhoneNumberPattern = "^[0-90-9٠-٩۰-۹]{2}$|^[++]*(?:[-x\u{2010}-\u{2015}\u{2212}\u{30FC}\u{FF0D}-\u{FF0F} \u{00A0}\u{00AD}\u{200B}\u{2060}\u{3000}()\u{FF08}\u{FF09}\u{FF3B}\u{FF3D}.\\[\\]/~\u{2053}\u{223C}\u{FF5E}*]*[0-9\u{FF10}-\u{FF19}\u{0660}-\u{0669}\u{06F0}-\u{06F9}]){3,}[-x\u{2010}-\u{2015}\u{2212}\u{30FC}\u{FF0D}-\u{FF0F} \u{00A0}\u{00AD}\u{200B}\u{2060}\u{3000}()\u{FF08}\u{FF09}\u{FF3B}\u{FF3D}.\\[\\]/~\u{2053}\u{223C}\u{FF5E}*A-Za-z0-9\u{FF10}-\u{FF19}\u{0660}-\u{0669}\u{06F0}-\u{06F9}]*(?:(?:;ext=([0-90-9٠-٩۰-۹]{1,7})|[ \\t,]*(?:e?xt(?:ensi(?:ó?|ó))?n?|e?xtn?|[,xxX##~~;]|int|anexo|int)[:\\..]?[ \\t,-]*([0-90-9٠-٩۰-۹]{1,7})#?|[- ]+([0-90-9٠-٩۰-۹]{1,5})#)?$)?[,;]*$"

static let countryCodePatten = "^[a-zA-Z]{2}$"
}
6 changes: 2 additions & 4 deletions PhoneNumberKit/Formatter.swift
Expand Up @@ -106,10 +106,8 @@ final class Formatter {
}

public extension PhoneNumber {
/**
Adjust national number for display by adding leading zero if needed. Used for basic formatting functions.
- Returns: A string representing the adjusted national number.
*/
/// Adjust national number for display by adding leading zero if needed. Used for basic formatting functions.
/// - Returns: A string representing the adjusted national number.
func adjustedNationalNumber() -> String {
if self.leadingZero == true {
return "0" + String(nationalNumber)
Expand Down
10 changes: 5 additions & 5 deletions PhoneNumberKit/MetadataManager.swift
@@ -1,5 +1,5 @@
//
// Metadata.swift
// MetadataManager.swift
// PhoneNumberKit
//
// Created by Roy Marmelstein on 03/10/2015.
Expand All @@ -10,7 +10,7 @@ import Foundation

final class MetadataManager {
private(set) var territories = [MetadataTerritory]()

private var territoriesByCode = [UInt64: [MetadataTerritory]]()
private var mainTerritoryByCode = [UInt64: MetadataTerritory]()
private var territoriesByCountry = [String: MetadataTerritory]()
Expand Down Expand Up @@ -70,7 +70,7 @@ final class MetadataManager {
/// - parameter code: international country code (e.g 44 for the UK).
///
/// - returns: optional array of MetadataTerritory objects.
internal func filterTerritories(byCode code: UInt64) -> [MetadataTerritory]? {
func filterTerritories(byCode code: UInt64) -> [MetadataTerritory]? {
return self.territoriesByCode[code]
}

Expand All @@ -79,7 +79,7 @@ final class MetadataManager {
/// - parameter country: ISO 3166 compliant region code (e.g "GB" for the UK).
///
/// - returns: A MetadataTerritory object.
internal func filterTerritories(byCountry country: String) -> MetadataTerritory? {
func filterTerritories(byCountry country: String) -> MetadataTerritory? {
return self.territoriesByCountry[country.uppercased()]
}

Expand All @@ -88,7 +88,7 @@ final class MetadataManager {
/// - parameter code: An international country code (e.g 1 for the US).
///
/// - returns: A MetadataTerritory object.
internal func mainTerritory(forCode code: UInt64) -> MetadataTerritory? {
func mainTerritory(forCode code: UInt64) -> MetadataTerritory? {
return self.mainTerritoryByCode[code]
}
}
80 changes: 37 additions & 43 deletions PhoneNumberKit/MetadataTypes.swift
Expand Up @@ -8,30 +8,28 @@

import Foundation

/**
MetadataTerritory object
- Parameter codeID: ISO 3166 compliant region code
- Parameter countryCode: International country code
- Parameter internationalPrefix: International prefix. Optional.
- Parameter mainCountryForCode: Whether the current metadata is the main country for its country code.
- Parameter nationalPrefix: National prefix
- Parameter nationalPrefixFormattingRule: National prefix formatting rule
- Parameter nationalPrefixForParsing: National prefix for parsing
- Parameter nationalPrefixTransformRule: National prefix transform rule
- Parameter emergency: MetadataPhoneNumberDesc for emergency numbers
- Parameter fixedLine: MetadataPhoneNumberDesc for fixed line numbers
- Parameter generalDesc: MetadataPhoneNumberDesc for general numbers
- Parameter mobile: MetadataPhoneNumberDesc for mobile numbers
- Parameter pager: MetadataPhoneNumberDesc for pager numbers
- Parameter personalNumber: MetadataPhoneNumberDesc for personal number numbers
- Parameter premiumRate: MetadataPhoneNumberDesc for premium rate numbers
- Parameter sharedCost: MetadataPhoneNumberDesc for shared cost numbers
- Parameter tollFree: MetadataPhoneNumberDesc for toll free numbers
- Parameter voicemail: MetadataPhoneNumberDesc for voice mail numbers
- Parameter voip: MetadataPhoneNumberDesc for voip numbers
- Parameter uan: MetadataPhoneNumberDesc for uan numbers
- Parameter leadingDigits: Optional leading digits for the territory
*/
/// MetadataTerritory object
/// - Parameter codeID: ISO 3166 compliant region code
/// - Parameter countryCode: International country code
/// - Parameter internationalPrefix: International prefix. Optional.
/// - Parameter mainCountryForCode: Whether the current metadata is the main country for its country code.
/// - Parameter nationalPrefix: National prefix
/// - Parameter nationalPrefixFormattingRule: National prefix formatting rule
/// - Parameter nationalPrefixForParsing: National prefix for parsing
/// - Parameter nationalPrefixTransformRule: National prefix transform rule
/// - Parameter emergency: MetadataPhoneNumberDesc for emergency numbers
/// - Parameter fixedLine: MetadataPhoneNumberDesc for fixed line numbers
/// - Parameter generalDesc: MetadataPhoneNumberDesc for general numbers
/// - Parameter mobile: MetadataPhoneNumberDesc for mobile numbers
/// - Parameter pager: MetadataPhoneNumberDesc for pager numbers
/// - Parameter personalNumber: MetadataPhoneNumberDesc for personal number numbers
/// - Parameter premiumRate: MetadataPhoneNumberDesc for premium rate numbers
/// - Parameter sharedCost: MetadataPhoneNumberDesc for shared cost numbers
/// - Parameter tollFree: MetadataPhoneNumberDesc for toll free numbers
/// - Parameter voicemail: MetadataPhoneNumberDesc for voice mail numbers
/// - Parameter voip: MetadataPhoneNumberDesc for voip numbers
/// - Parameter uan: MetadataPhoneNumberDesc for uan numbers
/// - Parameter leadingDigits: Optional leading digits for the territory
public struct MetadataTerritory: Decodable {
public let codeID: String
public let countryCode: UInt64
Expand All @@ -58,13 +56,11 @@ public struct MetadataTerritory: Decodable {
public let leadingDigits: String?
}

/**
MetadataPhoneNumberDesc object
- Parameter exampleNumber: An example phone number for the given type. Optional.
- Parameter nationalNumberPattern: National number regex pattern. Optional.
- Parameter possibleNumberPattern: Possible number regex pattern. Optional.
- Parameter possibleLengths: Possible phone number lengths. Optional.
*/
/// MetadataPhoneNumberDesc object
/// - Parameter exampleNumber: An example phone number for the given type. Optional.
/// - Parameter nationalNumberPattern: National number regex pattern. Optional.
/// - Parameter possibleNumberPattern: Possible number regex pattern. Optional.
/// - Parameter possibleLengths: Possible phone number lengths. Optional.
public struct MetadataPhoneNumberDesc: Decodable {
public let exampleNumber: String?
public let nationalNumberPattern: String?
Expand All @@ -77,17 +73,15 @@ public struct MetadataPossibleLengths: Decodable {
let localOnly: String?
}

/**
MetadataPhoneNumberFormat object
- Parameter pattern: Regex pattern. Optional.
- Parameter format: Formatting template. Optional.
- Parameter intlFormat: International formatting template. Optional.

- Parameter leadingDigitsPatterns: Leading digits regex pattern. Optional.
- Parameter nationalPrefixFormattingRule: National prefix formatting rule. Optional.
- Parameter nationalPrefixOptionalWhenFormatting: National prefix optional bool. Optional.
- Parameter domesticCarrierCodeFormattingRule: Domestic carrier code formatting rule. Optional.
*/
/// MetadataPhoneNumberFormat object
/// - Parameter pattern: Regex pattern. Optional.
/// - Parameter format: Formatting template. Optional.
/// - Parameter intlFormat: International formatting template. Optional.
///
/// - Parameter leadingDigitsPatterns: Leading digits regex pattern. Optional.
/// - Parameter nationalPrefixFormattingRule: National prefix formatting rule. Optional.
/// - Parameter nationalPrefixOptionalWhenFormatting: National prefix optional bool. Optional.
/// - Parameter domesticCarrierCodeFormattingRule: Domestic carrier code formatting rule. Optional.
public struct MetadataPhoneNumberFormat: Decodable {
public let pattern: String?
public let format: String?
Expand All @@ -99,6 +93,6 @@ public struct MetadataPhoneNumberFormat: Decodable {
}

/// Internal object for metadata parsing
internal struct PhoneNumberMetadata: Decodable {
struct PhoneNumberMetadata: Decodable {
var territories: [MetadataTerritory]
}