English | 简体中文
The encapsulation of local storage localStorage, provides expiration time setting and subscription functions, provides simple API use, no dependencies, and the compression is only 3.71KB (gzipped: 1.37KB).
- Better localStorage, Next localStorage
- Learn and use easily
- Expiration time of support data
- Support for monitoring data changes
- Built with TypeScript, providing full type definition files
# npm install
npm install storetify
# yarn install
yarn add storetify
#pnpm install
pnpm add storetifynpm run buildnpm testimport store from 'storetify';
store("test","storetify");or manually download and include in your HTML storetify.min.js,you can also pass UNPKG to download:
<script src="https://unpkg.com/storetify/lib/storetify.min.js"></script>
<script type="text/javascript">
Storetify("test","storetify");
</script>Storing data
store.set(key, data[, expires]);
or store(key, data[, expires]);
store.set("test","1"); //⇒1
store.set("test","1",3); //⇒1 test expires after 3 secondsGet string data of key
store.get(key)
or store(key)
store.get("test"); // Get the string data of test
store("test"); // Same function as abovestore.get<CustomType>("test"); // Get the string data of test
// or
const t:CustomType = store.get("test");
store("test"); // Same function as above
// or
const t:CustomType = store.("test");delete data under key store.remove(key)
store.remove("test");clear all key/data store.clear()
store.clear(); // Will publish all key value changesCheck if it exists and return true/false store.has(key)
store.has("test"); //⇒trueSubscribe to test data changes
store.subscribe("test",(e)=>{})For the event variable e, it is an abbreviated object from the StorageEvent object, which provides some practical properties, which can be used to observe the changes of key-value pairs well, as shown in the following table:
// JSON SAFE
export type JSONPrimitive = string | number | boolean | null
// eslint-disable-next-line no-use-before-define
export type StoretifyValue = JSONPrimitive | JSONObject | JSONArray
export interface JSONObject {
[key: string]: StoretifyValue
}
// eslint-disable-next-line no-use-before-define
export type JSONArray = Array<StoretifyValue>
export type StoretifyEventValue = StoretifyValue| Property | Type | Description |
|---|---|---|
| key | string |
The key to store the value, modify, delete according to it |
| oldValue | StoretifyEventValue |
last value |
| newValue | StoretifyEventValue |
current new value |
| type | string |
event type |
| native | StorageEvent |
native event |
Unsubscribe from test data changes
const someName = (e)=>{}
store.subscribe("test",someName)
store.unsubscribe("test",someName) // ⚠️Note that unsubscribe cannot be an anonymous method
store.unsubscribe("test") // ⚠️Note that all subscriptions to test will be cancelled including anonymous functionsGet the storage usage of the store
store.getUsed() // return `0.111 KB`source:localStorage
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) | iPhone(IOS) | Android | Opera Mobile | Window Phone |
|---|---|---|---|---|---|---|---|---|---|
| localStorage | 4+ | 3.5+ | 8+ | 10.50+ | 4+ | 3.2+ | 2.1+ | 11+ | 8+ |
JSON.stringify(localStorage).length How much capacity is currently occupied