Package Exports
- local-data-storage
- local-data-storage/dist/LocalDataStorage.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (local-data-storage) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Local Data Storage
It's a simple wrapper over browsers native localStorage. It uses "smart" items and can work with existing localStorage entries. It always returns success of operation as boolean.
Installation
$ npm install local-data-storage
Usage
Name | Parameters | Description | Return value |
---|---|---|---|
isAvailable() |
It tests if localStorage is available | boolean | |
itemExists() |
key: string | It check existence of an item | boolean |
clearExpiredItems() |
Clears expired items | boolean | |
getItem() |
key: string | Return an item by key | *LocalDataStorageItem |
setItem() |
key: string, obj: *LocalDataStorageItem, forceOverwrite?: boolean | Saves an item with given key | boolean |
removeItem() |
key: string | Removes an item by key | boolean |
clear() |
Clears the whole storage | boolean |
Examples:
import {
isAvailable,
itemExists,
clearExpiredItems,
getItem,
setItem,
removeItem,
clear,
} from "local-data-storage";
if (isAvailable()) {
/* do something */
}
if (itemExists("cat")) {
/* do something */
}
clearExpiredItems();
const myItem = getItem("cat");
setItem("cat", { value: "meow" });
setItem("willExpireCat", { value: "meow", expiryDate: Date.now() + 86_400 });
setItem("cat", { value: "OVERWRITTEN" }, true);
removeItem("cat");
clear();
*LocalDataStorageItem
Property | Type | Description |
---|---|---|
value | string | This will always exist, will contain unparseable items from storage as well. |
createdDate | number | This is a timestamp of creation. It will be set automatically at saving. |
expiryDate | number | This is an optional property. It's a timestamp of expiry, will be deleted by clearExpiredItems() if expired. |
Example:
{
value: 'cat',
createdDate: 86400,
expiryDate: 86401
}