Skip to content

Instantly share code, notes, and snippets.

@nilscox
Created December 4, 2022 15:56
Show Gist options
  • Select an option

  • Save nilscox/2196639ee31115f26e9f7e606bc6b16d to your computer and use it in GitHub Desktop.

Select an option

Save nilscox/2196639ee31115f26e9f7e606bc6b16d to your computer and use it in GitHub Desktop.
Stub implementation of the Web Storage API
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