JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 283975
  • Score
    100M100P100Q148534F
  • License MIT

A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood

Package Exports

  • store/dist/store.everything.min
  • store/dist/store.modern
  • store/dist/store.modern.js
  • store/dist/store.modern.min
  • store/plugins/defaults
  • store/plugins/events
  • store/plugins/expire
  • store/plugins/observe
  • store/plugins/operations
  • store/plugins/update
  • store/src/store-engine
  • store/storages/cookieStorage
  • store/storages/localStorage
  • store/storages/memoryStorage
  • store/storages/sessionStorage

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (store) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Store.js

  1. Basic Usage
  2. Supported Browsers
  3. Builds
  4. Plugins
  5. Storages

Basic Usage

All you need to know to get started:

API

store.js exposes a simple API for cross browser local storage:

// Store current user
store.set('user', { name:'Marcus' })

// Get current user
store.get('user')

// Remove current user
store.remove('user')

// Clear all keys
store.clearAll()

// Loop over all stored values
store.each(function(value, key) {
    console.log(key, '==', value)
})

Using npm

var store = require('store')
store.set('user', { name:'Marcus' })
store.get('user').name == 'Marcus'

Using a script tag

First, download one of the #builds (e.g https://raw.githubusercontent.com/marcuswestin/store.js/master/dist/store.legacy.min.js). Then:

<script src="path/to/my/store.legacy.min.js"></script>
<script>
var store = require('store')
store.set('user', { name:'Marcus' })
store.get('user').name == 'Marcus'
</script>

Supported Browsers

All of them, pretty much :)

To support all browsers (including IE6, IE7, Firefox 4, etc), use require('store/dist/legacy') or store.legacy.min.js.

To save some KBs but still support all modern browsers, use require('store/dist/modern') or store.modern.min.js instead.

List of supported browsers

Builds

Choose which build is right for you!

List of default builds

Make your own Build

If you're using NPM you can create your own build:

var engine = require('../store-engine')
var storages = [require('store/storages/localStorage'), require('../../storages/cookieStorage')]
var plugins = [require('store/plugins/defaults'), require('store/plugins/expire')]
var store = engine.createStore(storages, plugins)
store.set('foo', 'bar', new Date().getTime() + 3000) // Using expire plugin to expire in 3 seconds

Plugins

Plugins provide additional common functionality that some people need, but not everyone:

List of all Plugins

Using Plugins

With npm:

var expirePlugin = require('store/plugins/expire')
store.addPlugin(expirePlugin)

If you're using script tags, you can either use store.everything.min.js (which has all plugins built-in), or clone this repo to add or modify a build and run make build.

Write your own plugin

A store.js plugin is a function that returns an object that gets added on to the store. If any of the plugin functions overrides existing functions, the plugin function can still call the original function using the first argument (super_fn).

I'll elaborate on this shortly (let me know if you need more info sooner rather than later!), but for the moment I recommend you take a look at the current plugins. Good example plugins are plugins/defaults, plugins/expire and plugins/events.

Storages

Store.js will pick the best available storage, and automatically falls back to the first added storage that works:

List of all Storages

Write your own Storage

Chances are you won't ever need another storage. But if you do...

See storages/ for examples. Two good examples are memoryStorage and localStorage.

Basically, you just need an object that looks like this:

var storage = {
    name: 'myStorage',
    read: function(key) { ... },
    write: function(key, value) { ... },
    each: function(fn) { ... },
    remove: function(key) { ... },
    clearAll: function() { ... }
}
store.addStorage(storage)