Skip to content

Commit

Permalink
Merge pull request #1 from codykerns/add-generate
Browse files Browse the repository at this point in the history
New `generateID` method and short ID generator
  • Loading branch information
codykerns authored Feb 22, 2024
2 parents 327c233 + e10ea82 commit 0a3b31a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
12 changes: 12 additions & 0 deletions Sources/StableID/Generators/IDGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}
}
14 changes: 13 additions & 1 deletion Sources/StableID/StableID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 22 additions & 1 deletion Tests/StableIDTests/StableIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}

0 comments on commit 0a3b31a

Please sign in to comment.