Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie committed Jul 26, 2022
1 parent 847724e commit 28a1ea8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class LRUCache<T> {
}

public get(key: string): T | undefined {
// Prevent runtime errors
if (typeof key !== 'string') {
throw new TypeError('The LRUCache key must be string.');
}

if (this.cache.has(key)) {
const tmp = this.cache.get(key) as T;
this.cache.delete(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('initial LRU', () => {
expect(() => lruCache(0)).toThrow(Error);
});

test('LRU operation', () => {
test('LRU operations', () => {
const cache = lruCache<string>(3);
cache.set('1', 'a');
cache.set('2', 'b');
Expand All @@ -39,8 +39,10 @@ test('LRU operation', () => {
cache.set('5', 'e');
expect(cache.has('2')).toBeTruthy();
expect(cache.has('3')).toBeFalsy();
// @ts-ignore
// @ts-expect-error
expect(() => cache.set(0)).toThrow(TypeError);
expect(() => cache.get(0)).toThrow(TypeError);
expect(cache.size).toBe(3);
cache.clear();
expect(cache.size).toBe(0);
expect(cache.capacity).toBe(3);
Expand Down

0 comments on commit 28a1ea8

Please sign in to comment.