diff --git a/Package.swift b/Package.swift index e399e24..157fdf1 100644 --- a/Package.swift +++ b/Package.swift @@ -23,6 +23,7 @@ let package = Package( .library(name: "RAKCombine", targets: ["RAKCombine"]), .library(name: "RAKEpoxy", targets: ["RAKEpoxy"]), .library(name: "RAKGRDB", targets: ["RAKGRDB"]), + .library(name: "RAKNanoID", targets: ["RAKNanoID"]), ], dependencies: [ .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.8.2"), @@ -44,6 +45,7 @@ let package = Package( "RAKGradient", "RAKLocalCache", "RAKNotification", + "RAKNanoID", ] ), @@ -135,6 +137,12 @@ let package = Package( path: "Sources/GRDB" ), + .target( + name: "RAKNanoID", + dependencies: ["RAKCore"], + path: "Sources/NanoID" + ), + .testTarget( name: "RakuyoKitTests", dependencies: ["RakuyoKit"] diff --git a/Sources/Config/Mediator/ConfigMediator.swift b/Sources/Config/Mediator/ConfigMediator.swift index db2119e..dbedf8f 100755 --- a/Sources/Config/Mediator/ConfigMediator.swift +++ b/Sources/Config/Mediator/ConfigMediator.swift @@ -1,5 +1,5 @@ // -// Collection+RAK.swift +// ConfigMediator.swift // RakuyoKit // // Created by Rakuyo on 2024/4/9. diff --git a/Sources/Config/Mediator/ConfigMediatorProtocol.swift b/Sources/Config/Mediator/ConfigMediatorProtocol.swift index b0fef14..d630949 100755 --- a/Sources/Config/Mediator/ConfigMediatorProtocol.swift +++ b/Sources/Config/Mediator/ConfigMediatorProtocol.swift @@ -1,5 +1,5 @@ // -// Collection+RAK.swift +// ConfigMediatorProtocol.swift // RakuyoKit // // Created by Rakuyo on 2024/4/9. diff --git a/Sources/Config/Mediator/ConfigSourceProtocol.swift b/Sources/Config/Mediator/ConfigSourceProtocol.swift index 1b8a4dd..46ebe65 100755 --- a/Sources/Config/Mediator/ConfigSourceProtocol.swift +++ b/Sources/Config/Mediator/ConfigSourceProtocol.swift @@ -1,5 +1,5 @@ // -// Collection+RAK.swift +// ConfigSourceProtocol.swift // RakuyoKit // // Created by Rakuyo on 2024/4/9. diff --git a/Sources/Config/Model/ColorConfig.swift b/Sources/Config/Model/ColorConfig.swift index 91b4c82..b488ee5 100644 --- a/Sources/Config/Model/ColorConfig.swift +++ b/Sources/Config/Model/ColorConfig.swift @@ -1,5 +1,5 @@ // -// Collection+RAK.swift +// ColorConfig.swift // RakuyoKit // // Created by Rakuyo on 2024/4/9. diff --git a/Sources/Config/Model/EmptyConfig.swift b/Sources/Config/Model/EmptyConfig.swift index bd5f369..77fb145 100644 --- a/Sources/Config/Model/EmptyConfig.swift +++ b/Sources/Config/Model/EmptyConfig.swift @@ -1,5 +1,5 @@ // -// Collection+RAK.swift +// EmptyConfig.swift // RakuyoKit // // Created by Rakuyo on 2024/4/9. diff --git a/Sources/LocalCache/LocalCache+Migrated.swift b/Sources/LocalCache/LocalCache+Migrated.swift index 5698a12..d331678 100644 --- a/Sources/LocalCache/LocalCache+Migrated.swift +++ b/Sources/LocalCache/LocalCache+Migrated.swift @@ -1,3 +1,11 @@ +// +// LocalCache+Migrated.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/4/10. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + import Foundation import RAKCodable diff --git a/Sources/LocalCache/LocalCache+Storage.swift b/Sources/LocalCache/LocalCache+Storage.swift index dc0a5e4..fd8b109 100644 --- a/Sources/LocalCache/LocalCache+Storage.swift +++ b/Sources/LocalCache/LocalCache+Storage.swift @@ -1,3 +1,11 @@ +// +// LocalCache+Storage.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/4/10. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + import Foundation import RAKCore diff --git a/Sources/LocalCache/LocalCache.swift b/Sources/LocalCache/LocalCache.swift index bd8ed09..edb4033 100644 --- a/Sources/LocalCache/LocalCache.swift +++ b/Sources/LocalCache/LocalCache.swift @@ -1,3 +1,11 @@ +// +// LocalCache.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/4/10. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + import Foundation /// Namespaces diff --git a/Sources/NanoID/NanoID+Alphabet.swift b/Sources/NanoID/NanoID+Alphabet.swift new file mode 100644 index 0000000..d5f4317 --- /dev/null +++ b/Sources/NanoID/NanoID+Alphabet.swift @@ -0,0 +1,55 @@ +// +// NanoID+Alphabet.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/8/12. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + +import Foundation + +// MARK: - NanoID.Alphabet + +extension NanoID { + public struct Alphabet: OptionSet { + public let rawValue: Int + + public init(rawValue: Int) { + self.rawValue = rawValue + } + } +} + +extension NanoID.Alphabet { + public static let uppercasedLatinLetters = Self(rawValue: 1 << 0) + public static let lowercasedLatinLetters = Self(rawValue: 1 << 1) + public static let numbers = Self(rawValue: 1 << 2) + public static let symbol = Self(rawValue: 1 << 3) + + public static let urlSafe: Self = [uppercasedLatinLetters, lowercasedLatinLetters, numbers, symbol] +} + +extension NanoID.Alphabet { + public var supportedChars: String { + var chars = "" + lazy var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + if contains(.uppercasedLatinLetters) { + chars.append(letters.uppercased()) + } + + if contains(.lowercasedLatinLetters) { + chars.append(letters.lowercased()) + } + + if contains(.numbers) { + chars.append("1234567890") + } + + if contains(.symbol) { + chars.append("-_") + } + + return chars + } +} diff --git a/Sources/NanoID/NanoID.swift b/Sources/NanoID/NanoID.swift new file mode 100644 index 0000000..e184bd8 --- /dev/null +++ b/Sources/NanoID/NanoID.swift @@ -0,0 +1,79 @@ +// +// NanoID.swift +// RakuyoKit +// +// Created by Rakuyo on 2024/8/12. +// Copyright © 2024 RakuyoKit. All rights reserved. +// + +import Foundation + +// MARK: - NanoID + +/// Used to create NanoID +/// +/// ```swift +/// // Nano ID with default alphabet (0-9a-zA-Z_-) and length (21 chars) +/// let id = NanoID.new() +/// +/// // Nano ID with default alphabet and given length +/// let id = NanoID.new(size: 12) +/// +/// // Nano ID with given alphabet and length +/// let id = NanoID.new(alphabet: .uppercasedLatinLetters, size: 15) +/// +/// // Nano ID with preset custom parameters +/// let nanoID = NanoID(alphabet: [.lowercasedLatinLetters, .numbers], size: 10) +/// let idFirst = nanoID.new() +/// let idSecond = nanoID.new() +/// ``` +public final class NanoID { + public enum Default { + public static let size = 21 + public static let aphabet = NanoID.Alphabet.urlSafe + } + + private var size: Int + private var alphabet: Alphabet + + public init(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) { + self.size = size + self.alphabet = alphabet + } +} + +// MARK: - Public + +extension NanoID { + public static func new(alphabet: Alphabet = Default.aphabet, size: Int = Default.size) -> String { + generate(from: alphabet, of: size) + } + + public func new() -> String { + Self.generate(from: alphabet, of: size) + } +} + +// MARK: - Private + +extension NanoID { + /// Generates a Nano ID using given parameters + private static func generate(from alphabet: Alphabet, of length: Int) -> String { + let alphabetSupportedChars = alphabet.supportedChars + var nanoID = "" + + for _ in 0 ..< length { + let randomCharacter = randomCharacter(from: alphabetSupportedChars) + nanoID.append(randomCharacter) + } + + return nanoID + } + + /// Returns a random character from a given string + private static func randomCharacter(from string: String) -> Character { + let randomNum = Int.random(in: 0 ..< string.count) + let randomIndex = string.index(string.startIndex, offsetBy: randomNum) + return string[randomIndex] + } +} diff --git a/Sources/RakuyoKit/Exports.swift b/Sources/RakuyoKit/Exports.swift index ad6385a..6c5abb5 100644 --- a/Sources/RakuyoKit/Exports.swift +++ b/Sources/RakuyoKit/Exports.swift @@ -6,4 +6,5 @@ @_exported import RAKEncrypte @_exported import RAKGradient @_exported import RAKLocalCache +@_exported import RAKNanoID @_exported import RAKNotification