Skip to content

Commit 1e62747

Browse files
Merge pull request #64 from oslabs-beta/fix_lint
refactor & fix: lint issues and simplify parseExpTime
2 parents 3c6ab75 + 93c3490 commit 1e62747

File tree

9 files changed

+104
-73
lines changed

9 files changed

+104
-73
lines changed

.github/workflows/deno.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
with:
1717
deno-version: canary
1818

19+
- name: Run lint
20+
run: deno lint **/*.ts
21+
1922
- name: Run tests
2023
run: deno test --coverage=./src/tests/coverage_report ./src/tests/ --allow-net --allow-import
2124

src/doublyLinkedLists.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { cacheValue } from './types.ts'
1+
import type { CacheValue } from './types.ts'
22

33
/**
44
* Class definition for linked list containing cached values for both LRU and LFU.
55
*/
66
export class Node {
77
next: Node | null;
88
prev: Node | null;
9-
value: cacheValue;
9+
value: CacheValue;
1010
key: string;
1111
count: number;
1212
byteSize: number;
@@ -15,7 +15,7 @@ export class Node {
1515

1616
constructor(
1717
key: string,
18-
value: cacheValue,
18+
value: CacheValue,
1919
byteSize: number,
2020
timeStamp: Date,
2121
parent?: FreqNode
@@ -42,11 +42,11 @@ export class ValueDoublyLinkedList {
4242

4343
public addHead(
4444
key: string,
45-
value: cacheValue,
45+
value: CacheValue,
4646
byteSize: number,
4747
timeStamp: Date,
4848
parent?: FreqNode
49-
) {
49+
): Node {
5050
const node = new Node(key, value, byteSize, timeStamp, parent);
5151
if (!this.head) {
5252
this.head = node;
@@ -59,7 +59,7 @@ export class ValueDoublyLinkedList {
5959
return this.head;
6060
}
6161

62-
public delete(node: Node | null) {
62+
public delete(node: Node | null): Node | undefined {
6363
if (!node) {
6464
return;
6565
}
@@ -132,7 +132,7 @@ export class FreqDoublyLinkedList {
132132
this.tail = null;
133133
}
134134

135-
public addNewFreq(key: string, value: cacheValue, byteSize: number, timeStamp: Date) {
135+
public addNewFreq(key: string, value: CacheValue, byteSize: number, timeStamp: Date): Node {
136136
if (!this.head) {
137137
this.head = new FreqNode(1);
138138
this.tail = this.head;
@@ -146,7 +146,7 @@ export class FreqDoublyLinkedList {
146146
return this.head.valList.addHead(key, value, byteSize, timeStamp, this.head);
147147
}
148148

149-
public increaseFreq(node: Node) {
149+
public increaseFreq(node: Node): Node | undefined {
150150
if (!node.parent) {
151151
return;
152152
}
@@ -175,11 +175,11 @@ export class FreqDoublyLinkedList {
175175
return parent.next.valList.addHead(key, value, byteSize, timeStamp, parent.next);
176176
}
177177

178-
public deleteLeastFreq = () => this.head ?
178+
public deleteLeastFreq = (): Node | undefined => this.head ?
179179
this.deleteValNode(this.head.valList.tail)
180180
: undefined;
181181

182-
public deleteValNode(node: Node | null) {
182+
public deleteValNode(node: Node | null): Node | undefined {
183183
if (!node || !node.parent) {
184184
return;
185185
}
@@ -193,7 +193,7 @@ export class FreqDoublyLinkedList {
193193
return node;
194194
}
195195

196-
public delete(freqNode: FreqNode | null) {
196+
public delete(freqNode: FreqNode | null): FreqNode | undefined {
197197
if (!freqNode) {
198198
return;
199199
}

src/lfu.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { FreqDoublyLinkedList, Node } from './doublyLinkedLists.ts'
2-
import { cacheValue } from './types.ts'
3-
import PerfMetrics from './performanceMetrics.ts'
1+
import { FreqDoublyLinkedList, type Node } from './doublyLinkedLists.ts'
2+
import type { CacheValue } from './types.ts'
3+
import type PerfMetrics from './performanceMetrics.ts'
44

55

66
/**
@@ -31,7 +31,7 @@ class LFU {
3131
* @param value
3232
* @returns
3333
*/
34-
public put(key: string, value: cacheValue, byteSize: number) {
34+
public put(key: string, value: CacheValue, byteSize: number): CacheValue | undefined {
3535
if (this.cache[key]){
3636
this.metrics.decreaseBytes(this.cache[key].byteSize);
3737
this.metrics.increaseBytes(byteSize);
@@ -57,8 +57,11 @@ class LFU {
5757
this.metrics.decreaseBytes(deletedNode.byteSize);
5858
}
5959

60-
public get(key: string) {
61-
if (!this.cache[key]) return;
60+
public get(key: string): CacheValue | undefined {
61+
if (!this.cache[key]) {
62+
return;
63+
}
64+
6265
//if entry is stale, deletes and exits
6366
const currentTime = new Date();
6467
const timeElapsed = Math.abs(currentTime.getTime() - this.cache[key].timeStamp.getTime()) / 1000;
@@ -76,9 +79,11 @@ class LFU {
7679
}
7780
}
7881

79-
public delete(key: string) {
82+
public delete(key: string): Node | undefined {
8083
const node = this.cache[key];
81-
if (!node) return;
84+
if (!node) {
85+
return;
86+
}
8287

8388
this.freqList.deleteValNode(node);
8489

@@ -88,7 +93,7 @@ class LFU {
8893
return node;
8994
}
9095

91-
public clear() {
96+
public clear(): void {
9297
this.freqList = new FreqDoublyLinkedList();
9398
this.cache = {};
9499
this.length = 0;

src/lru.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ValueDoublyLinkedList, Node } from './doublyLinkedLists.ts'
2-
import { cacheValue } from './types.ts'
3-
import PerfMetrics from './performanceMetrics.ts'
1+
import { ValueDoublyLinkedList, type Node } from './doublyLinkedLists.ts'
2+
import type { CacheValue } from './types.ts'
3+
import type PerfMetrics from './performanceMetrics.ts'
44

55
/**
66
* Cache implementing a least recently used eviction policy.
@@ -33,7 +33,7 @@ class LRU {
3333
* @param value
3434
* @returns
3535
*/
36-
public put(key: string, value: cacheValue, byteSize: number) {
36+
public put(key: string, value: CacheValue, byteSize: number): CacheValue | undefined {
3737
//if key alreadys exits in cache, replace key value with new value, and move to list head.
3838
if (this.cache[key]){
3939
this.metrics.decreaseBytes(this.cache[key].byteSize);
@@ -67,9 +67,11 @@ class LRU {
6767
* @param key
6868
* @returns
6969
*/
70-
public get(key: string) {
70+
public get(key: string): CacheValue | undefined {
7171
//If no matching cache value (cache miss), return next();
72-
if (!this.cache[key]) return undefined;
72+
if (!this.cache[key]) {
73+
return;
74+
}
7375

7476
//if entry is stale, deletes and exits
7577
const currentTime = new Date();
@@ -100,9 +102,11 @@ class LRU {
100102
* @param key
101103
* @returns
102104
*/
103-
public delete(key: string) {
105+
public delete(key: string): Node | undefined {
104106
const node = this.cache[key];
105-
if (!node) return;
107+
if (!node) {
108+
return;
109+
}
106110

107111
this.list.delete(node);
108112
delete this.cache[key];
@@ -114,7 +118,7 @@ class LRU {
114118
/**
115119
* Clears entire cache contents.
116120
*/
117-
public clear() {
121+
public clear(): void {
118122
this.list = new ValueDoublyLinkedList();
119123
this.cache = {};
120124
this.length = 0;

src/performanceMetrics.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class PerfMetrics {
2424
this.hitLatencyTotal = 0;
2525
}
2626

27-
public addEntry = () => this.numberOfEntries++;
28-
public deleteEntry = () => this.numberOfEntries--;
29-
public readProcessed = () => this.readsProcessed++;
30-
public writeProcessed = () => this.writesProcessed++;
31-
public clearEntires = () => this.numberOfEntries = 0;
32-
public increaseBytes = (bytes: number) => this.memoryUsed += bytes;
33-
public decreaseBytes = (bytes: number) => this.memoryUsed -= bytes;
34-
public updateLatency = (latency: number, hitOrMiss: 'hit' | 'miss') => {
27+
public addEntry = (): number => this.numberOfEntries++;
28+
public deleteEntry = (): number => this.numberOfEntries--;
29+
public readProcessed = (): number => this.readsProcessed++;
30+
public writeProcessed = (): number => this.writesProcessed++;
31+
public clearEntires = (): number => this.numberOfEntries = 0;
32+
public increaseBytes = (bytes: number): number => this.memoryUsed += bytes;
33+
public decreaseBytes = (bytes: number): number => this.memoryUsed -= bytes;
34+
public updateLatency = (latency: number, hitOrMiss: 'hit' | 'miss'): void => {
3535
if (hitOrMiss === 'hit'){
3636
this.hitLatencyTotal += latency;
3737
this.currentHitLatency = latency;

src/tests/performanceMetrics_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
assertEquals,
33
assertInstanceOf,
4-
Context
4+
type Context
55
} from "../../deps.ts";
66
import Zoic from '../../zoic.ts';
77
import PerfMetrics from '../performanceMetrics.ts';

src/tests/zoic_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
assertEquals,
55
assertInstanceOf,
66
assertRejects,
7-
Context
7+
type Context
88
} from "../../deps.ts";
99
import Zoic from "../../zoic.ts";
1010
import LRU from "../lru.ts";
@@ -78,7 +78,7 @@ Deno.test(
7878
cache: "LRU",
7979
}),
8080
TypeError,
81-
"Cache expiration time must be string formatted as a numerical value followed by 'd', 'h', 'm', or 's', or a number representing time in seconds.",
81+
'Invalid format. Use number followed by d, h, m, or s (e.g., "1d,12h").',
8282
);
8383
},
8484
);
@@ -112,12 +112,12 @@ Deno.test(
112112
assertThrows(
113113
() => new Zoic({ expire: 31536001 }),
114114
TypeError,
115-
"Cache expiration time out of range.",
115+
"Cache expiration must be between 1 second and 1 year.",
116116
);
117117
assertThrows(
118118
() => new Zoic({ expire: 0 }),
119119
TypeError,
120-
"Cache expiration time out of range.",
120+
"Cache expiration must be between 1 second and 1 year.",
121121
);
122122
},
123123
);

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface options {
1+
export interface Options {
22
cache?: string;
33
port?: number;
44
hostname?: string;
@@ -7,8 +7,8 @@ export interface options {
77
capacity?: number;
88
}
99

10-
export interface cacheValue {
10+
export interface CacheValue {
1111
headers: { [k:string]:string };
1212
body: Uint8Array;
1313
status: number;
14-
}
14+
}

0 commit comments

Comments
 (0)