2019-10-13 01:22:09 +00:00
|
|
|
//
|
|
|
|
// Extensions.swift
|
2021-09-21 03:10:24 +00:00
|
|
|
// Twigs
|
2019-10-13 01:22:09 +00:00
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 10/12/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2021-12-08 02:31:52 +00:00
|
|
|
import SwiftUI
|
2019-10-13 01:22:09 +00:00
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
extension Date {
|
|
|
|
static var firstOfMonth: Date {
|
|
|
|
get {
|
|
|
|
return Calendar.current.dateComponents([.calendar, .year,.month], from: Date()).date!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static let localeDateFormatter: DateFormatter = {
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
dateFormatter.locale = Locale.current
|
|
|
|
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yyyyMMdd", options: 0, locale: Locale.current)
|
|
|
|
return dateFormatter
|
|
|
|
}()
|
|
|
|
|
|
|
|
func toLocaleString() -> String {
|
|
|
|
return Date.localeDateFormatter.string(from: self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 01:22:09 +00:00
|
|
|
extension Int {
|
2021-10-22 02:43:01 +00:00
|
|
|
func toDecimalString() -> String {
|
|
|
|
return String(format: "%.2f", Double(self) / 100.0)
|
|
|
|
}
|
|
|
|
|
2021-09-21 01:59:13 +00:00
|
|
|
func toCurrencyString() -> String {
|
2019-10-13 01:22:09 +00:00
|
|
|
let currencyFormatter = NumberFormatter()
|
|
|
|
currencyFormatter.locale = Locale.current
|
|
|
|
currencyFormatter.numberStyle = .currency
|
|
|
|
let doubleSelf = Double(self) / 100.0
|
2021-09-21 01:59:13 +00:00
|
|
|
return currencyFormatter.string(from: NSNumber(value: doubleSelf)) ?? ""
|
2019-10-13 01:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:31:52 +00:00
|
|
|
extension View {
|
|
|
|
func eraseToAnyView() -> AnyView {
|
|
|
|
return AnyView(self)
|
|
|
|
}
|
|
|
|
}
|
2022-01-03 17:56:43 +00:00
|
|
|
|
|
|
|
extension Array where Element: Identifiable {
|
|
|
|
mutating func remove(byId id: Element.ID) -> Element? {
|
|
|
|
if let index = firstIndex(where: { $0.id == id} ) {
|
|
|
|
return remove(at: index)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filter(withoutId id: Element.ID) -> [Element] {
|
|
|
|
var updated = self
|
|
|
|
_ = updated.remove(byId: id)
|
|
|
|
return updated
|
|
|
|
}
|
|
|
|
}
|