3.0.3 • Published 9 months ago

local-store-pro v3.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Build Status codecov license release

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).

✨ Features

  • 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

🪄 Install

# npm install
npm install local-store-pro

# yarn install
yarn add local-store-pro

#pnpm install
pnpm add local-store-pro

🏗️ Build

npm run build

🧪 Test

npm test

🔨 Usage

or manually download and include in your HTML local-store-pro.min.js,you can also pass UNPKG to download:

<script src="https://unpkg.com/local-store-pro/lib/local-store-pro.min.js"></script>
<script type="text/javascript">
LocalStorePro("test","local-store-pro");
</script>

or

import store from 'local-store-pro';
store("test","local-store-pro");

⚙️ API

set

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 seconds

get

Get string data of key store.get(key) or store(key)

store.get("test"); // Get the string data of test
store("test"); // Same function as above

remove

delete data under key store.remove(key)

store.remove("test");

clear

clear all key/data store.clear()

store.clear(); // Will publish all key value changes

has

Check if it exists and return true/false store.has(key)

store.has("test"); //⇒true

subscribe

Subscribe 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:

type NextStorageEventValue = Partial<any> | any[] | null | string | number
PropertyTypeDescription
keystringThe key to store the value, modify, delete according to it
oldValueNextStorageEventValuelast value
newValueNextStorageEventValuecurrent new value
typestringevent type
nativeStorageEventnative event

unsubscribe

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 functions

getUsed

Get the storage usage of the store

store.getUsed() // return `0.111 KB`

Compatibility

source:localStorage

FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)iPhone(IOS)AndroidOpera MobileWindow Phone
localStorage4+3.5+8+10.50+4+3.2+2.1+11+8+

local storage size

JSON.stringify(localStorage).length How much capacity is currently occupied

Detect the upper limit of localstore capacity

🌈Version changes

  • v1 mainly has a localStorage that can set the expiration time and monitor key value changes
  • v2 mainly adjusts the enhanced definition of typescript types
  • v3 mainly enhances the return value parameter of the standard monitoring callback function, optimizes and adjusts the type definition
  • v?...introducing namespaces, and sessions, etc.
3.0.3

9 months ago

3.0.2

11 months ago

3.0.1

11 months ago

3.0.0

12 months ago

2.0.0

1 year ago

1.2.1

1 year ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago