~ Extensions → `DavidFoundation` library

This commit is contained in:
David Bureš 2024-07-30 00:27:56 +02:00
parent 0428b9f8c1
commit c4670b6e3b
22 changed files with 3 additions and 428 deletions

View File

@ -7,6 +7,7 @@
import SwiftUI import SwiftUI
import UserNotifications import UserNotifications
import DavidFoundation
@main @main
struct CorkApp: App struct CorkApp: App

View File

@ -1,18 +0,0 @@
//
// Array - Get Difference.swift
// Cork
//
// Created by David Bureš on 03.09.2023.
//
import Foundation
extension Array where Element: Hashable
{
func difference(from other: [Element]) -> [Element]
{
let thisSet = Set(self)
let otherSet = Set(other)
return Array(thisSet.symmetricDifference(otherSet))
}
}

View File

@ -1,21 +0,0 @@
//
// Array - Get Second to Last Element.swift
// Cork
//
// Created by David Bureš on 17.03.2023.
//
import Foundation
extension Array
{
func penultimate() -> Element?
{
if count < 2
{
return nil
}
let index = count - 2
return self[index]
}
}

View File

@ -1,16 +0,0 @@
//
// Array - Prepend.swift
// Cork
//
// Created by David Bureš on 03.10.2023.
//
import Foundation
extension Array
{
mutating func prepend(_ element: Element)
{
return self.insert(element, at: 0)
}
}

View File

@ -1,16 +0,0 @@
//
// Binding - Reverse Bool Value.swift
// Cork
//
// Created by David Bureš on 28.09.2023.
//
import Foundation
import SwiftUI
prefix func ! (value: Binding<Bool>) -> Binding<Bool> {
Binding<Bool>(
get: { !value.wrappedValue },
set: { value.wrappedValue = !$0 }
)
}

View File

@ -1,21 +0,0 @@
//
// Date - Make Saveable in AppStorage.swift
// Cork
//
// Created by David Bureš on 18.03.2024.
//
import Foundation
extension Date: RawRepresentable
{
public var rawValue: String
{
timeIntervalSinceReferenceDate.description
}
public init?(rawValue: String)
{
self = Date(timeIntervalSinceReferenceDate: Double(rawValue) ?? 0.0)
}
}

View File

@ -1,24 +0,0 @@
//
// Image - Load from Disk.swift
// Cork
//
// Created by David Bureš on 19.10.2023.
//
import Foundation
import SwiftUI
import AppKit
extension Image
{
init?(localURL: URL)
{
guard let data = try? Data(contentsOf: localURL),
let nsImage = NSImage(data: data)
else {
return nil
}
self.init(nsImage: nsImage)
}
}

View File

@ -1,44 +0,0 @@
//
// ProcessInfo - CPU Architecture.swift
// Cork
//
// Created by David Bureš on 09.12.2023.
//
import Foundation
enum CPUArchitecture
{
case arm, intel
}
extension ProcessInfo
{
var CPUArchitecture: CPUArchitecture?
{
var sysinfo = utsname()
let result = uname(&sysinfo)
guard result == EXIT_SUCCESS else
{
return nil
}
let data = Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN))
guard let identifier = String(bytes: data, encoding: .ascii) else
{
return nil
}
let architectureString: String = identifier.trimmingCharacters(in: .controlCharacters)
if architectureString.starts(with: "arm")
{
return .arm
}
else
{
return .intel
}
}
}

View File

@ -1,22 +0,0 @@
//
// String - Contains Element in Array.swift
// Cork
//
// Created by David Bureš on 23.02.2023.
//
import Foundation
extension String
{
func containsElementFromArray(_ strings: [String]) -> Bool
{
strings.contains { contains($0) }
}
static func localizedPluralString(_ key: String, _ number: Int) -> String
{
let format = NSLocalizedString(key, comment: "")
return String.localizedStringWithFormat(format, NSNumber(value: number), number.formatted())
}
}

View File

@ -1,16 +0,0 @@
//
// String - Letters Only.swift
// Cork
//
// Created by David Bureš on 17.05.2024.
//
import Foundation
extension String
{
var onlyLetters: String
{
return String(unicodeScalars.filter(CharacterSet.letters.contains))
}
}

View File

@ -1,27 +0,0 @@
//
// String - LocalizedStringKey to String.swift
// Cork
//
// Created by David Bureš on 10.03.2024.
//
import Foundation
import SwiftUI
extension LocalizedStringKey
{
var stringKey: String?
{
Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value as? String
}
func stringValue(locale: Locale = .current) -> String?
{
guard let stringKey = stringKey else { return nil }
guard let language = locale.language.languageCode?.identifier else { return stringKey }
guard let path = Bundle.main.path(forResource: language, ofType: "lproj") else { return stringKey }
guard let bundle = Bundle(path: path) else { return stringKey }
let localizedString = NSLocalizedString(stringKey, bundle: bundle, comment: "")
return localizedString
}
}

View File

@ -1,16 +0,0 @@
//
// String - Remove Numbers.swift
// Cork
//
// Created by David Bureš on 17.05.2024.
//
import Foundation
extension String
{
func numbersRemoved() -> String
{
return self.components(separatedBy: CharacterSet.decimalDigits).joined()
}
}

View File

@ -1,21 +0,0 @@
//
// URL - Creation Date.swift
// Cork
//
// Created by David Bureš on 07.07.2024.
//
import Foundation
extension URL
{
var creationDate: Date?
{
guard let attributesOfSpecifiedURL: [FileAttributeKey : Any] = try? FileManager.default.attributesOfItem(atPath: self.path) else
{
return nil
}
return attributesOfSpecifiedURL[.creationDate] as? Date
}
}

View File

@ -1,16 +0,0 @@
//
// URL - Is Directory.swift
// Cork
//
// Created by David Bureš on 07.07.2024.
//
import Foundation
extension URL
{
var isDirectory: Bool
{
return (try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
}
}

View File

@ -1,50 +0,0 @@
//
// URL - Reveal in Finder.swift
// Cork
//
// Created by David Bureš on 23.05.2024.
//
import AppKit
import Foundation
enum FolderOpeningType
{
case openTargetItself, openParentDirectoryAndHighlightTarget
}
extension URL
{
func revealInFinder(_ folderOpeningType: FolderOpeningType)
{
switch folderOpeningType
{
case .openTargetItself:
guard let resourceValues = try? resourceValues(forKeys: [.isDirectoryKey])
else
{
NSWorkspace.shared.selectFile(self.path, inFileViewerRootedAtPath: self.deletingLastPathComponent().path)
return
}
guard let isDirectory = resourceValues.isDirectory
else
{
NSWorkspace.shared.selectFile(self.path, inFileViewerRootedAtPath: self.deletingLastPathComponent().path)
return
}
if isDirectory
{
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: self.path)
}
else
{
NSWorkspace.shared.selectFile(self.path, inFileViewerRootedAtPath: self.deletingLastPathComponent().path)
}
case .openParentDirectoryAndHighlightTarget:
NSWorkspace.shared.selectFile(self.path, inFileViewerRootedAtPath: self.deletingLastPathComponent().path)
}
}
}

View File

@ -1,24 +0,0 @@
//
// View - Conditional Modifiers.swift
// Cork
//
// Created by David Bureš on 16.02.2023.
//
import Foundation
import SwiftUI
extension View
{
@ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View
{
if condition
{
transform(self)
}
else
{
self
}
}
}

View File

@ -1,20 +0,0 @@
//
// View - Disable animations.swift
// Cork
//
// Created by David Bureš on 08.04.2024.
//
import Foundation
import SwiftUI
extension View
{
@ViewBuilder func allAnimationsDisabled() -> some View
{
self
.transaction { transaction in
transaction.animation = nil
}
}
}

View File

@ -1,18 +0,0 @@
//
// View - Fill Available Space.swift
// Cork
//
// Created by David Bureš on 30.03.2024.
//
import Foundation
import SwiftUI
extension View
{
@ViewBuilder func fillAvailableSpace() -> some View
{
self
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
}

View File

@ -1,20 +0,0 @@
//
// View - On Window Close.swift
// Cork
//
// Created by David Bureš on 20.03.2024.
//
import Foundation
import SwiftUI
extension View
{
func onWindowClose(action: @escaping () -> Void) -> some View
{
self
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification), perform: { _ in
action()
})
}
}

View File

@ -1,17 +0,0 @@
//
// View - Platform Conditional.swift
// Cork
//
// Created by David Bureš on 11.03.2024.
//
import Foundation
import SwiftUI
extension View
{
func modify<T: View>(@ViewBuilder modifier: (Self) -> T) -> T
{
modifier(self)
}
}

View File

@ -7,6 +7,7 @@
import Foundation import Foundation
import AppKit import AppKit
import DavidFoundation
/// A representation of a Homebrew package /// A representation of a Homebrew package
struct BrewPackage: Identifiable, Equatable, Hashable struct BrewPackage: Identifiable, Equatable, Hashable

View File

@ -6,7 +6,7 @@
"location" : "https://github.com/buresdv/DavidFoundation", "location" : "https://github.com/buresdv/DavidFoundation",
"state" : { "state" : {
"branch" : "main", "branch" : "main",
"revision" : "e75dfc53ff6797b6daf5a5b8a96c0fd47df90194" "revision" : "6b09749f713c1c769a664bb49c7fdc46ee9102e0"
} }
}, },
{ {