Skip to content

Instantly share code, notes, and snippets.

@dacioromero
Last active February 13, 2024 02:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacioromero/2dccfb622791f1f966773722b25d8f19 to your computer and use it in GitHub Desktop.
Save dacioromero/2dccfb622791f1f966773722b25d8f19 to your computer and use it in GitHub Desktop.
TypeScript Storage Implementation
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