1.1.4 • Published 10 years ago

storage-bus v1.1.4

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

storage-bus

A Cookie and localStorage bus to packaging same API and auto select engine

  • Packaging cookie and localStorage to same API
  • Auto select localStorage as engine if current browser support it, or use cookie
  • Expire managment for localStorage

Install

npm i --save storage-bus

Usage

import storage from 'storage-bus'

// Options for set
//
// if storage engine is localStorage, only `expires`
// takes effect. And if the value is not set for localStorage,
// the value will not expire forever.
const opts = {
  maxAge: 0,
  // if is number, will think as second from current time
  expires: 0,
  path: '/',
  domain: 'example.com',
  secure: false,
  httpOnly: false,
}

// Set
//
// this will auto select storage engine, this well use localStorage
// if browser support it, else will use cookie
//
// if value is not string, will use `JSON.stringify` to serialize it
//
// if engine is localStorage, will wrap value as `LocalData`
storage.set('key', 'value', opts)

// Get
//
// the second param name is `parse`, means try to parse the value as
// JSON, and if storage engine is localStorage, will parse and return
// data field and check expire, else will return as string
storage.get('key', false)

// Remove
//
// if engine is cookie, set as empty string
// if engine is localStorage, set as null
storage.remove('key')


//-----------------------------//
// force use cookie
import {cookie} from 'storage-bus'
cookie.set('key', 'value')
cookie.get('key', false)
cookie.remove('key')

// force use localStorage
//
// if browser doesn't support, all operate will do nothing
import {local} from 'storage-bus'
local.set('key', {value: 'hahah'})
local.get('key', true) // must be true, else will return original string
local.remove('key')

Standalone Usage

i. download dist file:

ii. import file:

<script src="path/to/storage-bus.min.js"></script>

iii. usage

// if import as upon, there is a global variable `storage`
storage.get('key')
storage.set('key')

API

See storage-bus.d.ts

Test

npm run test

License

MIT

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago