Last active
June 11, 2025 08:45
-
-
Save dacioromero/2dccfb622791f1f966773722b25d8f19 to your computer and use it in GitHub Desktop.
TypeScript Storage Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InMemoryStorage implements Storage { | |
private data = new Map<string, string>() | |
clear (): void { | |
this.data.clear() | |
} | |
getItem (key: string): string | null { | |
return this.data.get(String(key)) ?? null | |
} | |
removeItem (key: string): void { | |
this.data.delete(String(key)) | |
} | |
key (index: number): string | null { | |
return [...this.data.keys()][Number(index)] ?? null | |
} | |
setItem (key: string, value: string): void { | |
this.data.set(String(key), String(value)) | |
} | |
get length (): number { | |
return this.data.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment