@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
35 lines (27 loc) • 607 B
text/typescript
import { Clearable } from '@johngw/stream/types/Clearable'
/**
* An in-memory storage class.
*
* @group Storages
*/
export class MemoryStorage implements Storage, Clearable {
#data: Record<string, string> = {}
get length() {
return Object.keys(this.#data).length
}
clear() {
this.#data = {}
}
getItem(key: string) {
return key in this.#data ? this.#data[key] : null
}
key(index: number) {
return Object.keys(this.#data)[index]
}
removeItem(key: string) {
delete this.#data[key]
}
setItem(key: string, value: string) {
this.#data[key] = value
}
}