Added docs to miLocalStorage.
This commit is contained in:
parent
aeb568664d
commit
8c1508fae4
|
@ -44,16 +44,45 @@ export type Keys =
|
|||
// セッション毎に廃棄されるLocalStorage代替(セーフモードなどで使用できそう)
|
||||
//const safeSessionStorage = new Map<Keys, string>();
|
||||
|
||||
|
||||
/**
|
||||
* A utility object for interacting with the browser's localStorage.
|
||||
*
|
||||
* It's mostly a small wrapper around window.localStorage, but it validates
|
||||
* keys with a typesafe enum, and provides a few convenience methods for JSON.
|
||||
*/
|
||||
export const miLocalStorage = {
|
||||
/**
|
||||
* Retrieves an item from localStorage.
|
||||
* @param {Keys} key - The key of the item to retrieve.
|
||||
* @returns {string | null} The value of the item, or null if the item does not exist.
|
||||
*/
|
||||
getItem: (key: Keys): string | null => {
|
||||
return window.localStorage.getItem(key);
|
||||
},
|
||||
|
||||
/**
|
||||
* Stores an item in localStorage.
|
||||
* @param {Keys} key - The key of the item to store.
|
||||
* @param {string} value - The value of the item to store.
|
||||
*/
|
||||
setItem: (key: Keys, value: string): void => {
|
||||
window.localStorage.setItem(key, value);
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes an item from localStorage.
|
||||
* @param {Keys} key - The key of the item to remove.
|
||||
*/
|
||||
removeItem: (key: Keys): void => {
|
||||
window.localStorage.removeItem(key);
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves an item from localStorage and parses it as JSON.
|
||||
* @param {Keys} key - The key of the item to retrieve.
|
||||
* @returns {any | undefined} The parsed value of the item, or undefined if the item does not exist.
|
||||
*/
|
||||
getItemAsJson: (key: Keys): any | undefined => {
|
||||
const item = miLocalStorage.getItem(key);
|
||||
if (item === null) {
|
||||
|
@ -61,6 +90,12 @@ export const miLocalStorage = {
|
|||
}
|
||||
return JSON.parse(item);
|
||||
},
|
||||
|
||||
/**
|
||||
* Stores an item in localStorage as a JSON string.
|
||||
* @param {Keys} key - The key of the item to store.
|
||||
* @param {any} value - The value of the item to store.
|
||||
*/
|
||||
setItemAsJson: (key: Keys, value: any): void => {
|
||||
miLocalStorage.setItem(key, JSON.stringify(value));
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue