Argon Storage
Argon storage is a cross-browser wrapper for local storage.
Installation
npm i argon-storage
How does it work?
Argon storage test's if your current browser supports local/session storage API. If not, it stores data in cookies.
With Argon Storage
;const store = ; store;const value = store; // --> 'dataValue'
Without Argon Storage
let value = '';try localStorage && localStorage; if localStorage value = localStorage; // --> 'dataValue' catche // Assuming you have 'setCookie' and 'getCookie' implementation available ; value = ;
Argon Storage takes an extra step to verify your data and stores it correctly in local storage.
Without Argon Storage
localStorage;localStorage; // --> [object Object] // Local storage stores everything as strings
With Argon Storage
...store;store; // --> { m: 'helloworld', n: 100 }
Saves an extra step of transforming data before saving it.
You can also store data in session storage. Data validations and fallback still works.
...store; // Third parameter enables session storage mode
Argon Storage respects the "type" of data stored.
Without Argon Storage
localStorage; // Stored value is booleanlocalStorage; // --> 'true' // Retrieved value is a string
With Argon Storage
...store;store; // --> true // Returns the value as boolean
Argon storage supports data compression
We use Pieroxy's LZW algorithm (custom implementation) to compress input data. Useful to save some bytes when dealing with large dataset.
const store = compress: true ;...
Argon Storage provide methods to save data directly to cookies
; ;; // --> 'value'
By default setCookie
creates a session cookie (cookie without an expiry). You can set expiryDays
by passing a third parameter.
; // Cookie expires after 3 days
You can also set cookie path
and domain
by passing fourth and fifth parameters.
;
However, if you don't pass them, the path value defaults to /
and domain value defaults to current site domain. If for any reason you do not want to set domain (which isn't recommended), you can pass an empty string value.
You can also mark cookies as secure by passing a sixth boolean parameter.
; // Creates a secure cookie
For secure websites, you don't need to do this step. Argon storage by default creates secure cookies by checking if the site uses an https://
URL.
About
Argon Storage is a great utility for local storage. It is tested on majority of desktop and mobile browsers which makes it perfect for production use. It's built-in type resolution reduces a ton of code (and pain to write them). You can see it for yourself in one of the examples above.
Argon Storage supports IE9 browser and above.
Additional functions/methods
Get All Cookies
;; // --> Returns all stored cookies as list[]; // --> Returns all stored cookies that matches the regex.
Remove Cookie
;; // Deletes a cookie
Remove local/session storage data
; ; // Removes an item from local/session storage.
... ; // Removes item from session storage only.