CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-store

A localStorage wrapper for all browsers without using cookies or flash, providing persistent client-side storage with automatic fallback and plugin architecture

75

0.96x
Overview
Eval results
Files

core-api.mddocs/

Core Storage API

The core storage API provides basic, cross-browser persistent storage operations that work consistently across all supported browsers.

Capabilities

Basic Storage Operations

Set Value

Stores a value at the specified key. If the value is undefined, the key is removed.

/**
 * Store a value at the given key
 * @param {string} key - Storage key
 * @param {any} value - Value to store (undefined removes the key)
 * @returns {any} The stored value
 */
store.set(key, value)

Usage Examples:

// Store various data types
store.set('user', { name: 'Alice', age: 30 })
store.set('count', 42)
store.set('active', true)
store.set('tags', ['javascript', 'storage'])

// Remove a key by setting to undefined
store.set('temporary', undefined) // Equivalent to store.remove('temporary')

Get Value

Retrieves a value by key, with optional default value if the key doesn't exist.

/**
 * Get the value of the given key
 * @param {string} key - Storage key to retrieve
 * @param {any} [defaultValue] - Default value if key doesn't exist
 * @returns {any} Stored value or default value
 */
store.get(key, defaultValue?)

Usage Examples:

// Basic retrieval
var user = store.get('user')
console.log(user.name) // 'Alice'

// With default values
var theme = store.get('theme', 'light')     // Returns 'light' if 'theme' not set
var settings = store.get('settings', {})    // Returns {} if 'settings' not set
var count = store.get('count', 0)           // Returns 0 if 'count' not set

Remove Value

Deletes a key-value pair from storage.

/**
 * Remove a key-value pair from storage
 * @param {string} key - Storage key to remove
 */
store.remove(key)

Usage Examples:

// Remove specific keys
store.remove('user')
store.remove('temporary-data')

// Check if removal was successful
store.set('test', 'value')
store.remove('test')
console.log(store.get('test')) // undefined

Clear All Values

Removes all stored key-value pairs.

/**
 * Clear all stored key-value pairs
 */
store.clearAll()

Usage Examples:

// Clear everything
store.set('key1', 'value1')
store.set('key2', 'value2')
store.clearAll()

console.log(store.get('key1')) // undefined
console.log(store.get('key2')) // undefined

Iterate Over Values

Iterates over all stored key-value pairs.

/**
 * Iterate over all stored key-value pairs
 * @param {function} callback - Function called for each key-value pair
 */
store.each(callback)

// Callback signature
type EachCallback = (value: any, key: string) => void

Usage Examples:

// Basic iteration
store.set('user1', { name: 'Alice' })
store.set('user2', { name: 'Bob' })
store.set('count', 42)

store.each(function(value, key) {
    console.log(key + ' = ' + JSON.stringify(value))
})
// Output:
// user1 = {"name":"Alice"}
// user2 = {"name":"Bob"}  
// count = 42

// Conditional processing
var users = []
store.each(function(value, key) {
    if (key.startsWith('user')) {
        users.push(value)
    }
})

Store Information

Version

The current version of the store.js library.

/**
 * Library version string
 * @type {string}
 */
store.version

Enabled Status

Indicates whether storage is enabled and functional.

/**
 * Whether storage is enabled and functional
 * @type {boolean}
 */
store.enabled

Usage Examples:

console.log('Store.js version:', store.version)  // '2.0.12'

if (store.enabled) {
    // Storage is working
    store.set('data', { message: 'Storage is available' })
} else {
    // Storage is not available (rare - store.js has extensive fallbacks)
    console.warn('Storage is not available')
}

Store Management

Create Store Instance

Creates a new store instance with specified storage backends and plugins.

/**
 * Create a new store instance
 * @param {Array|Object} storages - Storage backend(s) to use
 * @param {Array|Function} plugins - Plugin(s) to apply  
 * @param {string} [namespace] - Optional namespace for isolation
 * @returns {Object} New store instance
 */
store.createStore(storages, plugins, namespace?)

Usage Examples:

var engine = require('store/src/store-engine')
var localStorage = require('store/storages/localStorage')
var eventsPlugin = require('store/plugins/events')

// Create custom store with specific storage and plugins
var customStore = store.createStore(localStorage, eventsPlugin)

// Create namespaced store
var userStore = store.createStore([localStorage], [], 'users')
userStore.set('current', { name: 'Alice' })

var settingsStore = store.createStore([localStorage], [], 'settings')  
settingsStore.set('theme', 'dark')

// Keys are isolated by namespace
console.log(userStore.get('current'))    // { name: 'Alice' }
console.log(settingsStore.get('current')) // undefined

Add Plugin

Adds a plugin to the current store instance.

/**
 * Add a plugin to the current store instance
 * @param {Function|Array} plugin - Plugin function or array of plugins
 */
store.addPlugin(plugin)

Usage Examples:

var expirePlugin = require('store/plugins/expire')
var eventsPlugin = require('store/plugins/events')

// Add single plugin
store.addPlugin(expirePlugin)

// Now expiration methods are available
store.set('temp', 'data', Date.now() + 60000) // Expires in 1 minute

// Add multiple plugins
store.addPlugin([eventsPlugin, expirePlugin])

Create Namespaced Store

Creates a new store instance with the same storage and plugins but in a different namespace.

/**
 * Create a namespaced store instance
 * @param {string} namespace - Namespace for the new store
 * @returns {Object} New namespaced store instance
 */
store.namespace(namespace)

Usage Examples:

// Create namespaced stores for different contexts
var userStore = store.namespace('user')
var appStore = store.namespace('app')

userStore.set('preferences', { theme: 'dark' })
appStore.set('preferences', { language: 'en' })

// Values are isolated by namespace
console.log(userStore.get('preferences')) // { theme: 'dark' }
console.log(appStore.get('preferences'))  // { language: 'en' }

Check Namespace

Checks if the current store instance has a specific namespace.

/**
 * Check if store has the given namespace
 * @param {string} namespace - Namespace to check
 * @returns {boolean} True if store has the namespace
 */
store.hasNamespace(namespace)

Usage Examples:

var userStore = store.namespace('user')
var mainStore = store

console.log(userStore.hasNamespace('user'))  // true
console.log(userStore.hasNamespace('app'))   // false
console.log(mainStore.hasNamespace('user'))  // false

Raw API Access

Direct access to store methods without plugin modifications. The raw property provides access to the original, unmodified store methods before any plugins have been applied.

/**
 * Raw API access without plugin modifications
 * @type {Object} Object containing unmodified store methods
 */
store.raw

// Raw methods available:
store.raw.get(key, defaultValue?)      // Unmodified get method
store.raw.set(key, value)              // Unmodified set method  
store.raw.remove(key)                  // Unmodified remove method
store.raw.clearAll()                   // Unmodified clearAll method
store.raw.each(callback)               // Unmodified each method

Usage Examples:

// If you have plugins that modify set/get behavior,
// raw gives you access to the unmodified methods
store.addPlugin(require('store/plugins/events'))
store.addPlugin(require('store/plugins/expire'))

// These trigger plugin behavior (events, expiration handling)
store.set('key', 'value')
store.set('temp', 'data', Date.now() + 60000) // expire plugin

// These bypass all plugin modifications
store.raw.set('key', 'value')          // No events triggered
store.raw.get('temp')                  // No expiration checking

// Useful for plugin development or when you need guaranteed
// access to the core storage functionality
var rawValue = store.raw.get('plugin-managed-key')

Install with Tessl CLI

npx tessl i tessl/npm-store

docs

core-api.md

index.md

plugins.md

storage-config.md

tile.json