1.0.8 • Published 3 years ago

@gaoming13/browser-storage v1.0.8

Weekly downloads
18
License
MIT
Repository
github
Last release
3 years ago

browser-storage

English | 中文简体

Version codecov Build Status Downloads

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-storage

CDN

jsdelivr

<script src="https://cdn.jsdelivr.net/npm/@gaoming13/browser-storage@1.0.1"></script>

unpkg

<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 content
  • setItem(key, value) Store a value
  • removeItem(key) Delete a value
  • setPriority(priority) Set storage priority
    • example obj.setPriority('c')
    • example obj.setPriority('cl')
    • example obj.setPriority('cs')
    • example obj.setPriority('cls')
  • setPreKey(key) Set key prefix
    • @example obj.setPreKey('h5-')
  • cookieStoreSetPreKey(key) Set cookie key name prefix
    • @example obj.cookieStoreSetPreKey('h5-')
  • localStoreSetPreKey(key) Set LocalStorage key name prefix
  • seesionStoreSetPreKey(key) Set SessionStorage key name prefix
  • cookieStoreIsSupport() Does the browser support Cookie
  • localStoreIsSupport() Does the browser support LocalStorage
  • seesionStoreIsSupport() Does the browser support SessionStorage
  • cookieSetOptions Set cookie storage options
    • example setCookieOptions({ path: '/abc', maxAge: 86400, domain: 'local.com' })

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.

License

MIT

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.2-beta.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago