tz-storage
v1.0.8
Published
A light-weight localStorage tool.
Downloads
30
Maintainers
Readme
tz-storage
A light-weight localStorage tool.
- No dependency
- Supports any type of data
- Supports UMD
- JSON support
- expiration support
- ~3kb before gzipped
Install
npm
$ npm install tz-storage --save
browser
<script src="tz-storage.min.js"></script>
Usage
Check compatibility:
storage.isSupported // return true or false
Set a storage namespace, it turns key to 'namespace.key':
storage.namespace = 'mystorage'
Set a default value in advance:
storage.defaultValue = 'nothing'
Set options by config:
storage.config({
namespace: 'space',
defaultValue: 'empty'
})
Set
Set a storage with any types:
storage.set('key', {active: true, label: 'helloword'})
storage.set('key', [1, 2, 3, 4])
storage.set('key', 'hello')
Set a expire time, it returns default value when expired:
storage.set('key', 'value', new Date().getTime() + 8640000)
Get
Replace with a custom value when the value is empty.
storage.get('key', { defaultValue: 'empty' })
Get with specified type(only worked in string, number and boolean)
storage.get('key', { type: 'number' })
Remove
storage.remove('key')