From 05450a54bdb3cfa0108ee94aaaea2099bf629466 Mon Sep 17 00:00:00 2001 From: Cody Kerns Date: Wed, 21 Feb 2024 22:14:56 -0500 Subject: [PATCH 1/2] add new short ID generator, fix tests --- Sources/StableID/Generators/IDGenerator.swift | 12 ++++++++++ Sources/StableID/StableID.swift | 14 ++++++++++- Tests/StableIDTests/StableIDTests.swift | 23 ++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Sources/StableID/Generators/IDGenerator.swift b/Sources/StableID/Generators/IDGenerator.swift index 583de70..cfd76d6 100644 --- a/Sources/StableID/Generators/IDGenerator.swift +++ b/Sources/StableID/Generators/IDGenerator.swift @@ -19,4 +19,16 @@ extension StableID { return UUID().uuidString } } + + public class ShortIDGenerator: IDGenerator { + public init() { } + + public func generateID() -> String { + let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + + return String((0..<8).compactMap { _ in + letters.randomElement() + }) + } + } } diff --git a/Sources/StableID/StableID.swift b/Sources/StableID/StableID.swift index 3663b02..02ba9c9 100644 --- a/Sources/StableID/StableID.swift +++ b/Sources/StableID/StableID.swift @@ -87,6 +87,8 @@ public class StableID { adjustedId = delegateId } + Self.logger.log(type: .info, message: "Setting StableID to \(adjustedId)") + Self.shared._id = adjustedId self.setLocal(key: Constants.StableID_Key_Identifier, value: adjustedId) self.setRemote(key: Constants.StableID_Key_Identifier, value: adjustedId) @@ -95,6 +97,13 @@ public class StableID { } } + private func generateID() { + Self.logger.log(type: .info, message: "Generating new StableID.") + + let newID = self._idGenerator.generateID() + self.setIdentity(value: newID) + } + private func setLocal(key: String, value: String) { Self._localStore?.set(value, forKey: key) } @@ -134,10 +143,13 @@ extension StableID { public static var id: String { return Self.shared._id } public static func identify(id: String) { - logger.log(type: .info, message: "Setting StableID to \(id)") Self.shared.setIdentity(value: id) } + public static func generateNewID() { + Self.shared.generateID() + } + public static func set(delegate: any StableIDDelegate) { Self.shared.delegate = delegate } diff --git a/Tests/StableIDTests/StableIDTests.swift b/Tests/StableIDTests/StableIDTests.swift index d2095f1..5d059c1 100644 --- a/Tests/StableIDTests/StableIDTests.swift +++ b/Tests/StableIDTests/StableIDTests.swift @@ -11,9 +11,11 @@ final class StableIDTests: XCTestCase { } func clearDefaults() { - let defaults = UserDefaults.standard + guard let defaults = UserDefaults(suiteName: Constants.StableID_Key_DefaultsSuiteName) else { return } + let dictionary = defaults.dictionaryRepresentation() dictionary.keys.forEach { key in + print(key) defaults.removeObject(forKey: key) } } @@ -36,4 +38,23 @@ final class StableIDTests: XCTestCase { XCTAssert(StableID.id == uuid) } + func testGenerateNewID() { + clearDefaults() + + StableID.configure() + let originalID = StableID.id + + StableID.generateNewID() + let newID = StableID.id + + XCTAssert(originalID != newID) + } + + func testShortIDLength() { + clearDefaults() + + StableID.configure(idGenerator: StableID.ShortIDGenerator()) + + XCTAssert(StableID.id.count == 8) + } } From e10ea823299456b2597ba047abc597c33a466475 Mon Sep 17 00:00:00 2001 From: Cody Kerns Date: Wed, 21 Feb 2024 22:28:52 -0500 Subject: [PATCH 2/2] add generators --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f8fac96..37f1c85 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,10 @@ Then pass the generator as part of the `configure` method: StableID.configure(idGenerator: MyCustomIDGenerator()) ``` +**Built-in generators** +- `StableID.StandardGenerator`: Standard UUIDs +- `StableID.ShortIDGenerator`: 8-character alphanumeric IDs + ## 📚 Examples _Coming soon_