Skip to content

Commit

Permalink
Add entries method to KeyValueDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
vostrnad committed Feb 14, 2023
1 parent 5217f3b commit 84d6b74
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ A getter than returns an array of all existing keys.
A getter that returns an array of all stored values.
#### `.entries`
A getter that returns an array of all stored key-value pairs.
#### `.has(key)`
Returns true if the given key exists in the database, false otherwise.
Expand Down
7 changes: 7 additions & 0 deletions src/key-value-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export class KeyValueDatabase<T extends Serializable> extends Storage<
return Object.values(this.data).map(deepClone)
}

get entries(): Array<[string, T]> {
return Object.entries(this.data).map(([key, value]) => [
key,
deepClone(value),
])
}

/**
* Returns true if the given key exists in the database, false otherwise.
*/
Expand Down
7 changes: 6 additions & 1 deletion test/unit/key-value-database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ describe('KeyValueDatabase', () => {
expect(db.get('key')).toEqual({ a: 1, b: 'test' })
})

it('should return keys and values', async () => {
it('should return keys, values and entries', async () => {
await Promise.all([
db.set('key1', 'value1'),
db.set('key2', 'value2'),
db.set('key3', 'value3'),
])
expect(db.keys).toEqual(['key1', 'key2', 'key3'])
expect(db.values).toEqual(['value1', 'value2', 'value3'])
expect(db.entries).toEqual([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3'],
])
})

it('should update an object', async () => {
Expand Down

0 comments on commit 84d6b74

Please sign in to comment.