Skip to content

Instantly share code, notes, and snippets.

@dineybomfim
dineybomfim / PhoneNumberFormatter.swift
Last active January 16, 2024 11:56
The ultimate, free to use, no dependency, runtime automatically update, worldwide global Phone Number Formatter, works for any country and every single particular national phone number pattern, including carrier codes, international codes and country codes.
/*
* PhoneNumberFormatter.swift
*
* Created by Diney Bomfim on 12/12/22.
*
* Swift code that automatically loads the global phone format list, parses it and formats a given input.
* For everyone that believes Phone Number Formatting should be open source, global standard and easy to use.
* This is based on Google Project https://github.com/google/libphonenumber. Updated constantly by Google engineers.
*/
@dineybomfim
dineybomfim / phone_numbers_metadata
Created December 11, 2022 16:50
Worlwide phone patterns
{"phoneNumberMetadata": {"territories": {"territory": [{"id": "AC","countryCode": "247","internationalPrefix": "00","generalDesc": {"nationalNumberPattern": "(?:[01589]\\d|[46])\\d{4}"},"fixedLine": {"possibleLengths": {"national": "5"},"exampleNumber": "62889","nationalNumberPattern": "6[2-467]\\d{3}"},"mobile": {"possibleLengths": {"national": "5"},"exampleNumber": "40123","nationalNumberPattern": "4\\d{4}"},"uan": {"possibleLengths": {"national": "6"},"exampleNumber": "542011","nationalNumberPattern": "(?:0[1-9]|[1589]\\d)\\d{4}"}},{"id": "AD","countryCode": "376","internationalPrefix": "00","availableFormats": {"numberFormat": [{"pattern": "(\\d{3})(\\d{3})","leadingDigits": "[135-9]","format": "$1 $2"},{"pattern": "(\\d{4})(\\d{4})","leadingDigits": "1","format": "$1 $2"},{"pattern": "(\\d{3})(\\d{3})(\\d{3})","leadingDigits": "6","format": "$1 $2 $3"}]},"generalDesc": {"nationalNumberPattern": "(?:1|6\\d)\\d{7}|[135-9]\\d{5}"},"noInternationalDialling": {"possibleLengths": {"national": "8"},"nationalNum
@dineybomfim
dineybomfim / LocaleAlphaCode.swift
Last active June 12, 2025 15:09
Have you ever wondered how to convert and/or use alpha 2 and alpha 3 country codes seamlessly with native Swift Locale? Here is the answer: There is a tricky, very cost-efficient that can guarantee to find valid ISO Alpha-2 and Alpha-3 country codes (ISO 3166 international standard). NO EXTERNAL FILE IS NEEDED!
public extension Locale {
private static var availableRegions: [Locale] = { Locale.availableIdentifiers.map { Locale(identifier: $0) } }()
init?(isoCode: String, from: Locale = .autoupdatingCurrent) {
guard let locale = from.locale(isoCode: isoCode) else { return nil }
self = locale
}
func alpha2Code(from isoCode: String) -> String? {
@dineybomfim
dineybomfim / AnyCodable.swift
Last active December 12, 2022 12:32
Wondering how to parse dynamic JSON with Swift Codable without dealing with `init(from:Decoder)` and `encode(to: Encoder)`? The answer is `AnyCodable`
// Full Swift Codable object compatible with dynamic JSON structures. Able to encode and decode any kind of JSON.
//
// How to use it?
//
// Try it out on your playground at the end of the file:
public struct AnyCodable {
// MARK: - Properties