Skip to content
/ strg.js Public

Simple localStorage, sessionStorage and cookie operating library with the single API

Notifications You must be signed in to change notification settings

fend25/strg.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

strg.js

Simple localStorage, sessionStorage and cookie operating library with the single API.

Getting started

Just download file and include it in your HTML document: <script src="strg.min.js"></script>
You can use any set of localStorage, sessionStorage or cookie wrappers, all files are in the /only folder. Also, if you use one of modern browsers which support localStorage, the easiest way is to use just localStorage wrapper, or simply copy it to one of your JS files. It takes only 24 lines and contains ready and useful code. You can remove some functions from localstore object if you don't need them, or add some. Use, code and enjoy!

API

So, the strg.js contains three objects with single API:
localstore - localStorage wrapper
sessionstore - sessionStorage wrapper
cookiestore - cookie wrapper
and the fourth object store, that use localStorage if it's supported or cookies otherwise.

Each of them has 5 functions:
set(key, value): sets key-value pair. JSON is supported in values
get(key): returns just value for the key. returns undefined if no value found
getAll(): returns object with all key-value pairs. JSON is parsed. returns {} on empty store
remove(key): removes key. returns undefined
removeAll(): remove all key-value pairs, returns undefined

In case of cookiestore, function set takes five params: key, value, expires, path, secure
expires: Date, number or string, that can be used in Date constructor
path: string, path for cookie
secure: bool, secure flag for cookie
Also, all objects contain two additional fields:
s: Storage object or document.cookie, for example: window.localStorage
type: string, storage type, for example 'localStorage'

Examples

store.set('a', 1); // 1
store.set('b', {c: [1, '2', {d: 3}]})); // {"c":[1,"2",{"d":3}]}
store.getAll(); // {"a":1,"b":{"c":[1,"2",{"d":3}]}}

store.set('c', 'some string'); // "some string"
store.remove('b'); // undefined
store.getAll(); // {"a":1,"c":"some string"}

store.removeAll(); // undefined
store.getAll(); // {}

Tests

/test/test.html Tests are made with simple and useful framework - Angular Light. Even if you don't need this right now, just take a look - it's worth it. Actually it is Angular in JS-way: no factories, services and so on. Just write less, do more, have fun and forget about jQuery with very simple and clear code.

License

MIT

About

Simple localStorage, sessionStorage and cookie operating library with the single API

Resources

Stars

Watchers

Forks

Packages

No packages published