Skip to content

Commit

Permalink
Support of key method
Browse files Browse the repository at this point in the history
  • Loading branch information
vsaienko committed Sep 2, 2019
1 parent 98c0608 commit df5ff15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions __tests__/inMemoryStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe('inMemoryStorage', () => {
expect(inMemoryStorage.length).toBe(1)
})

test('key', () => {
inMemoryStorage.setItem('key-1', 'value-1')

expect(() => {
inMemoryStorage.key()
}).toThrow(
"Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present."
)

expect(inMemoryStorage.key(0)).toBe('key-1')
expect(inMemoryStorage.key(1)).toBe(null)
})

test('clear', () => {
inMemoryStorage.setItem('key-1', 'value-1')
inMemoryStorage.setItem('key-2', 'value-2')
Expand Down
16 changes: 16 additions & 0 deletions src/inMemoryStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ class InMemoryStorage {
return null
}

key(index) {
if (index === undefined) {
throw new TypeError(
"Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present."
)
}

const key = Object.keys(this._data)[index]

if (key !== undefined) {
return key
}

return null
}

clear() {
this._data = {}
this.length = 0
Expand Down

0 comments on commit df5ff15

Please sign in to comment.