Skip to content

Commit

Permalink
feat: add function returning all keys from store
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyVasyk committed Jun 13, 2024
1 parent 182eb48 commit ca18dc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/UserDefaultsStore/UserDefaultsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public struct UserDefaultsStore: UserDefaultsStoreProtocol {
}

// MARK: - Data Management

/// Returns all keys which are present in the store.
///
/// - Returns: The array of `String` representing all keys from the store.
public func keys() -> [String] {
let keys = userDefaults.dictionaryRepresentation().keys
return [String](keys)
}

/// Removes the value associated with a given key.
///
Expand Down
20 changes: 20 additions & 0 deletions Tests/UserDefaultsStoreTests/UserDefaultsStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ final class UserDefaultsStoreTests: XCTestCase {
XCTAssertTrue(storageValue.isEmpty)
}

/// Tests retrieving array containing all keys of the store
func testKeys() throws {
let store = createStore()

let value1 = "foo"
let key1 = "KEY_1"
store.set(value1, forKey: key1)

let value2 = "bar"
let key2 = "KEY_2"
store.set(value2, forKey: key2)

let keys = store.keys()
XCTAssertTrue(keys.contains(key1))
XCTAssertTrue(keys.contains(key2))

let key3 = "KEY_3"
XCTAssertFalse(keys.contains(key3))
}

/// Tests removing a specific key-value pair.
func testRemove() throws {
let store = createStore()
Expand Down

0 comments on commit ca18dc4

Please sign in to comment.