T-storage
README English doc | 中文 doc
What is T-storage?
T-storage is a storage plugin based on modern browsers that encapsulates LocalStorage and SessionStorage.
It borrows the idea of a database and stores the data you need to store according to the three levels of DB_NAME => TABLE_NAME => FIELD_NAME. The time option is added to the data () storage process to limit the length of data storage.
Features
- Store data of the same type together to avoid naming conflicts and variable pollution.
- There is a time limit on the data stored.
- demo
Install
Introduced through script tags
https://cdn.jsdelivr.net/npm/t-storage/dist/tstorage.min.js
var storage = windowTstorage;
Introduced through npm
npm install t-storage -S
Usage
// localStoragestoragedata; // sessionStoragestoragesessiondata
// Most used LocalStoragelet dog1 = storagedata'dog' key: 'dog1' value: 'Tom'console;console;console; let dog2 = storagedata'dog' key: 'dog2' value: name: 'Charlie' age: 'tow' console;console;console; // The usage of sessionStorage is exactly the same as localStorage// But sessionStorage does not support the time optionlet Fish = storagesessiondata'Fish' key: 'Fish1' value: 'Gldli'console;console;console; // Time-Limited Storage: Time in Seconds// Don't millisecondslet cat1 = storagedata'cat' key: 'cat1' value: name: 'Cleo' age: 'one' time: 5 * 60 * 60 // 5 hours // time: '2020/3/7 8' // Future time YES // time: '2020/3/7 8:59' // Future time YES // time: '22020/3/7 08:23:23:14:58' // Malformed // ...console;console;console;
- localStorage uses storage.data (), sessionStorages uses storage.session.data ()
- time parameter you can pass the number of seconds, representing the length of time you want to store, for example you want to store 1 hour, time should pass 1 * 60 * 60
- You can also pass a date format to the time parameter, which means that this storage will be deleted on a certain date in the future. For the date format, see demo
- More usage demo
API List
api | explain |
---|---|
.data() | Set and get the specified value |
.has() | Determine if there is a specified value |
.clear() | Clear all values in storage space |
.getAll() | Get all values in space |
.forEach() | forEach each data in the storage space and call the callback function |
License
T-storage is MIT licensed.