Add ObjectStorage object implementing Storage API

This commit is contained in:
Talia 2021-09-20 11:35:31 +02:00
parent 22ef3ae3c5
commit 95683894bc
2 changed files with 27 additions and 0 deletions

View file

@ -37,3 +37,9 @@ recommendable.
Turns template literals directly into HTML templates. Just read the code, it's
like 5 lines or so.
## Storage
Currently a sngle class `ObjectStorage` implementing the API of the Storage
class using a plain JS Map as backend. This is mostly meant as a page-local
fallback to LocalStorage and SessionStorage

21
storage.js Normal file
View file

@ -0,0 +1,21 @@
export class ObjectStorage {
#map = new Map()
key(index) {
return [...this.#map.keys()][index]
}
getItem(keyName) {
if (this.#map.has(keyName))
return this.#map.get(keyName)
else
return null
}
setItem(keyName, keyValue) {
this.#map.set(keyName, keyValue)
}
removeItem(keyName) {
this.#map.delete(keyName)
}
clear() {
this.#map.clear()
}
}