Created
December 4, 2022 15:56
-
-
Save nilscox/2196639ee31115f26e9f7e606bc6b16d to your computer and use it in GitHub Desktop.
Stub implementation of the Web Storage API
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 StubStorage implements Storage { | |
| readonly items = new Map<string, string>(); | |
| get itemsArray() { | |
| return Object.values(this.items); | |
| } | |
| get length(): number { | |
| return this.itemsArray.length; | |
| } | |
| clear(): void { | |
| this.items.clear(); | |
| } | |
| getItem(key: string): string | null { | |
| return this.items.get(key) ?? null; | |
| } | |
| key(index: number): string | null { | |
| return this.itemsArray[index] ?? null; | |
| } | |
| removeItem(key: string): void { | |
| this.items.delete(key); | |
| } | |
| setItem(key: string, value: string): void { | |
| this.items.set(key, value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment