1.0.8 • Published 4 years ago
@gaoming13/browser-storage v1.0.8
browser-storage
Use Cookie, LocalStorage, SessionStorage according to priority.
Automatically detect whether the browser supports it, if it does not support using the next priority item for storage.
Features
- Cookie storage options can be modified
- Compact size 8.1KB (2.3KB gzipped)
Installation
You can install it via yarn or NPM.
$ yarn add @gaoming13/browser-storage
$ npm i @gaoming13/browser-storageCDN
<script src="https://cdn.jsdelivr.net/npm/@gaoming13/browser-storage@1.0.1"></script><script src="https://unpkg.com/@gaoming13/browser-storage@1.0.1"></script>Example
import browserStorage from '@gaoming13/browser-storage';
// Define the storage priority, and find the storage supported by the browser according to the priority
// `c` means Cookie, `s` is SessionStorage, `l` is LocalStorage
browserStorage.setPriority('csl');
// Store a value
browserStorage.setItem('item1', 'hello world');
// Get stored content
browserStorage.getItem('item1'); // It will return `hello world`
// Delete a value
browserStorage.removeItem('item1');// Set priority to restrict use of cookie storage
browserStorage.setPriority('c');
// Set the prefix of the stored key name (for example, `item2` is stored, then the actual stored key name is` h5-item2`)
browserStorage.setPreKey('h5-');
// Set cookie storage options
// Set Expires / Max-Age = Session
browserStorage.setCookieOptions({ path: '/abc', domain: 'local.com' });
// For more options, please refer to [https://github.com/jshttp/cookie#options-1](https://github.com/jshttp/cookie#options-1)
browserStorage.setCookieOptions({ path: '/abc', maxAge: 86400, domain: 'local.com' });
// Store a value
browserStorage.setItem('item2', 'xxxx');API
getItem(key)Get stored contentsetItem(key, value)Store a valueremoveItem(key)Delete a valuesetPriority(priority)Set storage priority- example
obj.setPriority('c') - example
obj.setPriority('cl') - example
obj.setPriority('cs') - example
obj.setPriority('cls')
- example
setPreKey(key)Set key prefix- @example
obj.setPreKey('h5-')
- @example
cookieStoreSetPreKey(key)Set cookie key name prefix- @example
obj.cookieStoreSetPreKey('h5-')
- @example
localStoreSetPreKey(key)Set LocalStorage key name prefixseesionStoreSetPreKey(key)Set SessionStorage key name prefixcookieStoreIsSupport()Does the browser support CookielocalStoreIsSupport()Does the browser support LocalStorageseesionStoreIsSupport()Does the browser support SessionStoragecookieSetOptionsSet cookie storage options- example
setCookieOptions({ path: '/abc', maxAge: 86400, domain: 'local.com' })
- example
Changelog
Details changes for each release are documented in the release notes.
Contribution
If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.