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 stash.min.js, stash.js or get the latest release at GitHub.
To use stash, include this script anywhere in your page.
<script src="//rezitech.github.com/stash/stash.min.js"></script>
stash is easy to use, with commands like get, set, and cut.
// 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
stash can store many types of data.
// 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'
stash can intelligently add to existing data.
// 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' }
stash supports the storage of DOM elements.
// 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"
stash is packed with features for your pleasure, tested over a hundred different ways with qUnit.
Run the tests on your own machine.
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
Returns an item in offline storage.
stash.set('foo', 'Success'); stash.get('foo'); // "Success"
Returns an item as a string in offline storage.
stash.set('foo', { a: 1, b: 'two' }); stash.getAsString('foo'); // "{'a':1,'b':'two'}"
Returns all items in offline storage.
stash.set('foo', 'Success'); stash.getAll(); // { foo: "Success" }
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!"
Removes (an) item(s) in offline storage.
stash.set('a', 'foo'); // 1 stash.get('a'); // "foo" stash.cut('a'); stash.get('a'); // undefined
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.
git checkout -b my_stash
)
git commit -am "Added Awesomeness"
)
git push origin my_stash
)