store2 2.10.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package store2 --version 2.10.0
NuGet\Install-Package store2 -Version 2.10.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="store2" Version="2.10.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add store2 --version 2.10.0
#r "nuget: store2, 2.10.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install store2 as a Cake Addin
#addin nuget:?package=store2&version=2.10.0

// Install store2 as a Cake Tool
#tool nuget:?package=store2&version=2.10.0

Documentation

The main store function can handle set, get, transact, setAll, getAll and clear actions directly. Respectively, these are called like so:

store(key, data);                 // sets stringified data under key
store(key);                       // gets and parses data stored under key
store(key, fn[, alt]);            // run transaction function on/with data stored under key
store({key: data, key2: data2});  // sets all key/data pairs in the object
store();                          // gets all stored key/data pairs as an object
store((key, data)=>{ });          // calls function for each key/data in storage, return false to exit
store(false);                     // clears all items from storage

Parameters in [brackets] are optional. There are also more explicit and versatile functions available:

store.set(key, data[, overwrite]); // === store(key, data);
store.setAll(data[, overwrite]);   // === store({key: data, key2: data});
store.get(key[, alt]);             // === store(key);
store.getAll([fillObj]);           // === store();
store.transact(key, fn[, alt]);    // === store(key, fn[, alt]);
store.clear();                     // === store(false);
store.has(key);                    // returns true or false
store.remove(key[, alt]);          // removes key and its data, then returns the data or alt, if none
store.each(fn[, fill]);            // === store(fn); optional call arg will be 3rd fn arg (e.g. for gathering values)
store.add(key, data);              // concats, merges, or adds new value into existing one
store.keys([fillList]);            // returns array of keys
store.size();                      // number of keys, not length of data
store.clearAll();                  // clears *ALL* areas (but still namespace sensitive)

Passing in false for the optional overwrite parameters will cause set actions to be skipped if the storage already has a value for that key. All set action methods return the previous value for that key, by default. If overwrite is false and there is a previous value, the unused new value will be returned.

Functions passed to transact will receive the current value for that key as an argument or a passed alternate if there is none. When the passed function is completed, transact will save the returned value under the specified key. If the function returns undefined, the original value will be saved. This makes it easy for transact functions to change internal properties in a persistent way:

store.transact(key, function(obj) {
    obj.changed = 'newValue';// this change will be persisted
});

Functions passed to each will receive the key as first argument and current value as the second; if a fill parameter is specified, it's value will be the third argument for every call (few should ever need a fill parameter). If the function returns false at any point during the iteration, the loop will exit early and not continue on to the next key/value pair.

store.each(function(key, value) {
    console.log(key, '->', value);
    if (key === 'stopLoop') {
        return false;// this will cause each to stop calling this function
    }
});

For getAll and keys, there is the option to pass in the object or list, respectively, that you want the results to be added to. This is instead of an empty list. There are only a few special cases where you are likely to need or want this, in general, most users should ignore these optional parameters. These both use the second, optional argument each function, which is also a niche feature. The value argument is passed as the second arg to the callback function (in place of the data associated with the current key) and is returned at the end. Again, most users should not need this feature. All of these use the browser's localStorage (aka "local"). Using sessionStorage merely requires calling the same functions on store.session:

store.session("addMeTo", "sessionStorage");
store.local({lots: 'of', data: 'altogether'});// store.local === store :)

There is also a store API automatically available for keeping non-persistent information, meant only to last until page reload.

store.page("until","reload");

All the specific get, set, etc. functions are available on store.session, store.local, and store.page, as well as any other storage facility registered via store.area(name, customStorageObject) by an extension, where customStorageObject must implement the Storage interface. This is how [store.old.js][old] extends store.js to support older versions of IE and Firefox.

If you want to put stored data from different pages or areas of your site into separate namespaces, the store.namespace(ns) function is your friend:

var cart = store.namespace('cart');
cart('total', 23.25);// stores in localStorage as 'cart.total'
console.log(store('cart.total') == cart('total'));// logs true
console.log(store.cart.getAll());// logs {total: 23.25}
cart.session('group', 'toys');// stores in sessionStorage as 'cart.group'

The namespace provides the same exact API as store but silently adds/removes the namespace prefix as needed. It also makes the namespaced API accessible directly via store[namespace] (e.g. store.cart) as long as it does not conflict with an existing part of the store API.

The 'namespace' function is one of two "extra" functions that are also part of the "store API":

store.namespace(prefix);// returns a new store API that prefixes all key-based functions
store.isFake();// is this actually localStorage/sessionStorage or an in-memory fallback?

If localStorage or sessionStorage are unavailable, they will be faked to prevent errors, but data stored will NOT persist beyond the life of the current document/page.

Write Your Own Extension

To write your own extension, you can use or carefully override internal functions exposed as store._. In particular, the store._.fn(fnName, fn) method is available to automatically add your new function to every instance of the store interface (e.g. store, store.session and all existing and future namespaces). Take care using this, as it will override existing methods. Here is a simple example:

(function(_) {
    _.fn('falsy', function(key) {
        return !this.get(key);
    });
    _.fn('truthy', function(key) {
        return !this.falsy(key);
    });
})(store._);

This extension would be used like so:

store('foo', 1);
store.falsy('foo'); // returns false

store.session('bar', 'one');
store.session.truthy('bar'); // return true;

const widgetStore = store.namespace('widget');
widgetStore.falsy('state'); // returns true
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.14.2 1,485 7/18/2022
2.14.1 1,502 7/15/2022
2.14.0 2,269 5/11/2022
2.13.2 1,582 3/14/2022
2.13.1 1,149 12/20/2021
2.13.0 1,337 12/19/2021
2.12.0 1,618 8/12/2020
2.11.2 1,598 5/11/2020
2.11.1 1,574 4/14/2020
2.11.0 1,626 3/24/2020
2.10.0 1,639 9/28/2019
2.9.0 1,714 8/22/2019
2.8.0 1,750 7/23/2019
2.7.1 2,032 11/15/2018
2.7.0 1,952 3/5/2018
2.5.2 2,390 8/9/2017
2.5.0 2,616 1/31/2017
2.3.2 56,708 10/28/2015
2.3.0 2,695 8/25/2015
2.1.1 6,676 5/28/2013