You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.1 KiB
130 lines
3.1 KiB
//
|
|
// Formatters.swift
|
|
// TournamentStats
|
|
//
|
|
// Created by Laurent Morvillier on 03/06/2019.
|
|
// Copyright © 2019 Stax River. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Locale {
|
|
|
|
func countryCode(from countryName: String) -> String? {
|
|
return NSLocale.isoCountryCodes.first { (code) -> Bool in
|
|
let name = self.localizedString(forRegionCode: code)
|
|
return name == countryName
|
|
}
|
|
}
|
|
|
|
func flagoji(from countryName: String) -> String? {
|
|
|
|
var adaptedCountryName = countryName
|
|
switch countryName {
|
|
case "Russian Federation":
|
|
adaptedCountryName = "Russia"
|
|
case "China":
|
|
adaptedCountryName = "China mainland"
|
|
default:
|
|
break
|
|
}
|
|
|
|
if let countryCode = self.countryCode(from: adaptedCountryName) {
|
|
let base : UInt32 = 127397
|
|
var s = ""
|
|
for v in countryCode.uppercased().unicodeScalars {
|
|
s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
|
|
}
|
|
return s
|
|
}
|
|
return nil
|
|
}
|
|
|
|
}
|
|
|
|
class Formatter {
|
|
|
|
static var currency: NumberFormatter {
|
|
let c = NumberFormatter()
|
|
c.locale = Locale(identifier: "en_US")
|
|
c.usesGroupingSeparator = true
|
|
c.groupingSeparator = ","
|
|
c.decimalSeparator = "."
|
|
c.currencySymbol = "$"
|
|
c.maximumFractionDigits = 0
|
|
c.numberStyle = .currency
|
|
return c
|
|
}
|
|
|
|
static var importData: NumberFormatter {
|
|
let formatter = NumberFormatter()
|
|
formatter.usesGroupingSeparator = true
|
|
formatter.groupingSeparator = ","
|
|
formatter.decimalSeparator = "."
|
|
formatter.numberStyle = .decimal
|
|
return formatter
|
|
}
|
|
|
|
static var basicDate: DateFormatter {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "dd/MM/yyyy"
|
|
return formatter
|
|
}
|
|
|
|
static var fileDate: DateFormatter {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "yyyy_MM_dd"
|
|
return formatter
|
|
}
|
|
|
|
}
|
|
|
|
extension Date {
|
|
|
|
var year: String {
|
|
let dateFormatter = DateFormatter()
|
|
dateFormatter.dateFormat = "YYYY"
|
|
return dateFormatter.string(from: self)
|
|
}
|
|
|
|
}
|
|
|
|
extension Int {
|
|
|
|
var currencyFormatted: String {
|
|
return Formatter.currency.string(from: NSNumber(value: self)) ?? ""
|
|
}
|
|
|
|
var rankFormatted: String {
|
|
|
|
switch self {
|
|
case 11:
|
|
return "11th"
|
|
case 12:
|
|
return "12th"
|
|
case 13:
|
|
return "13th"
|
|
default:
|
|
let unit = "\(self)"
|
|
let n = Int(String(unit.last!))
|
|
switch n {
|
|
case 1:
|
|
return "\(self)st"
|
|
case 2:
|
|
return "\(self)nd"
|
|
case 3:
|
|
return "\(self)rd"
|
|
default:
|
|
return "\(self)th"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Double {
|
|
|
|
var currencyFormatted: String {
|
|
return Formatter.currency.string(from: NSNumber(value: self)) ?? ""
|
|
}
|
|
|
|
}
|
|
|