@alwatr/local-storage
Version:
`localJsonStorage` is a utility object in our TypeScript package that provides methods for interacting with the local storage in a structured and versioned manner.
69 lines • 2.43 kB
TypeScript
/**
* A utility object for working with local storage and JSON data.
*/
export declare const localJsonStorage: {
/**
* Generate local storage key.
*
* @param name - Name of the item.
* @param version - Version of the item (default: 1).
* @returns The generated local storage key.
* @example
* ```typescript
* localJsonStorage.key_('myItem', 1); // myItem.v1
* ```
*/
readonly key_: (name: string, version?: number) => string;
/**
* Get the local storage item and parse it as JSON.
* If the item is not found, return the default value.
* If the version is greater than 1, remove the previous version.
* If the item is not a valid JSON object, return the default value.
*
* @param name - The name of the item.
* @param defaultValue - The default value of the item.
* @param version - The data structure version of the item (default: 1).
* @returns The parsed JSON value or the default value if the item is not found.
* @example
* ```typescript
* const value = localJsonStorage.getItem('myItem', {a: 1, b: 2});
* ```
*/
readonly getItem: <T extends Json>(name: string, defaultValue: T, version?: number) => T;
/**
* Check if an item exists in local storage.
*
* @param name - The name of the item.
* @param version - The version of the item (default: 1).
* @returns True if the item exists, false otherwise.
* @example
* ```typescript
* const exists = localJsonStorage.hasItem('myItem');
* ```
*/
readonly hasItem: (name: string, version?: number) => boolean;
/**
* Set local storage item as JSON.
*
* @param name - Name of the item.
* @param value - Value of the item.
* @param version - Version of the item.
* @example
* ```typescript
* localJsonStorage.setItem('myItem', {a: 1, b: 2});
* ```
*/
readonly setItem: <T extends Json>(name: string, value: T, version?: number) => void;
/**
* Removes an item from the local storage.
*
* @param name - The name of the item to remove.
* @param version - The version of the item to remove. Default is 1.
* @example
* ```typescript
* localJsonStorage.removeItem('myItem');
* ```
*/
readonly removeItem: (name: string, version?: number) => void;
};
//# sourceMappingURL=main.d.ts.map