Stash

A JavaScript offline storage library

stash is a library for JavaScript that makes using offline storage easy. stash supports storing strings, numbers, booleans, arrays, objects, regular expressions, dates, functions, DOM elements and more (and all of this in ~1KB).

Download

Download stash.min.js, stash.js or get the latest release at GitHub.

Examples

To use stash, include this script anywhere in your page.

<script src="//rezitech.github.com/stash/stash.min.js"></script>

Hello World

stash is easy to use, with commands like get, set, and cut.

Run
// set foo
stash.set('foo', 'Hello World');

// log foo
console.log( stash.get('foo') ); // 'Hello World'

// cut foo
stash.cut('foo');

// log foo
console.log( stash.get('foo') ); // null

Different Data Types

stash can store many types of data.

Run
// set foo
stash.set('foo', 'Hello World');

// set bar
stash.set('bar', 7);

// set baz
stash.set('baz', ['THX', 1138]);

// set qux
stash.set('qux', { a: 'lorem', b: true, c: [1, 2, 3], d: function (v) { return 'success '+v; } });

// log
console.log( stash.get('foo') ); // 'Hello World'
console.log( stash.get('bar') ); // 1138
console.log( stash.get('baz') ); // ['THX', 1138]
console.log( stash.get('qux') ); // { a: 'lorem', b: true, c: [1, 2, 3], d: function (v) { return 'success '+v; } });
console.log( stash.get('qux').d('doubled') ); // 'success doubled'

Data Manipulation

stash can intelligently add to existing data.

Run
// set foo
stash.set('foo', 1);

// set bar
stash.set('bar', { a: 'lorem' });

// add to foo
stash.add('foo', 5);

// add to bar
stash.add('bar', { b: 'ipsum' });

// log
console.log( stash.get('foo') ); // 6
console.log( stash.get('bar') ); // { a: 'lorem', b: 'ipsum' }

DOM Elements

stash supports the storage of DOM elements.

Run
// create a DIV element with innerHTML and a "foo" attribute
var div = document.createElement('div');
div.innerHTML = 'Hello World';
div.setAttribute('foo', 'bar');

// set foo as the DIV element
stash.set('foo', div);

// log
console.log( stash.get('foo').nodeName ); // "DIV"
console.log( stash.get('foo').innerHTML ); // "Hello World"
console.log( stash.get('foo').getAttribute('foo') ); // "bar"

Features

stash is packed with features for your pleasure, tested over a hundred different ways with qUnit.

Run the tests on your own machine.

set

Sets (an) item(s) in offline storage and returns 1 (if any value changed) or 2 (if any value was not changed).

stash.set('a', 'foo'); // 1

stash.set({ b: 'bar', c: 'baz'}); // 1

stash.set('a', 'foo'); // 2

stash.set('a', 'fee'); // 1

stash.set({ b: 'foo', c: 'baz' }); // 2

get

Returns an item in offline storage.

stash.set('foo', 'Success');
stash.get('foo'); // "Success"

getAsString

Returns an item as a string in offline storage.

stash.set('foo', { a: 1, b: 'two' });
stash.getAsString('foo'); // "{'a':1,'b':'two'}"

getAll

Returns all items in offline storage.

stash.set('foo', 'Success');
stash.getAll(); // { foo: "Success" }

add

Adds (an) item(s) to (an) existing one(s) in offline storage and returns 1 (if any value changed) or 2 (if any value was not changed).

stash.set('a', 'foo'); // 1
stash.get('a'); // "foo"

stash.add('a', '!'); // 1
stash.get('a'); // "foo!"

stash.add('a', ''); // 2
stash.get('a'); // "foo!"

cut

Removes (an) item(s) in offline storage.

stash.set('a', 'foo'); // 1
stash.get('a'); // "foo"

stash.cut('a');
stash.get('a'); // undefined

License

stash uses a dual MIT/GPL-2.0 License. The MIT License is recommended for most projects, because it is simple, easy to understand, and it places almost no restrictions on what you can do with stash. If the GPL-2.0 License suits your project better, then you are also free to use stash under that license.

You don't have to do anything special to choose one license or the other, and you don't have to notify anyone which license you are using. You are free to use stash in commercial projects as long as the copyright header is left intact.

Contribute

  1. Fork it.
  2. Create a branch. (git checkout -b my_stash)
  3. Commit your changes. (git commit -am "Added Awesomeness")
  4. Push to the branch. (git push origin my_stash)
  5. Create an Issue with a link to your branca.
  6. Enjoy a refreshing Coca Cola Classic (you earned it!) and wait.