npm

Need private packages and team management tools?Check out npm Orgs. »

web-storages

0.1.3 • Public • Published

web-storages

Version Build status

Emulating localStorage and sessionStorage if not exist.

Install

Install with npm:

npm install web-storages

API - Set functions by args

    // On node.js, storages will attached to global automatically.
    // Take care for that exporting storages are not equals global storages.
    require('web-storages');
    
    // localStorage
    localStorage.setItem('hoge', 'Hello,hoge!');
    localStorage.setItem('fuga', 'Hello,fuga!');
    console.log(localStorage.getItem('hoge')); // => 'Hello,hoge!'
    console.log(localStorage.key(0)); // => 'hoge'
    console.log(localStorage.length()); // => 2
    
    localStorage.removeItem('hoge');
    console.log(localStorage.getItem('hoge'))); // => null
    console.log(localStorage.length()); // => 1
    
    localStorage.clear();
    console.log(localStorage.getItem('fuga'))); // => null
    console.log(localStorage.length()); // => 0
    
    // sessionStorage
    sessionStorage.setItem('hoge', 'Hello,hoge!');
    sessionStorage.setItem('fuga', 'Hello,fuga!');
    console.log(sessionStorage.getItem('hoge')); // => 'Hello,hoge!'
    console.log(sessionStorage.key(0)); // => 'hoge'
    console.log(sessionStorage.length()); // => 2
    
    sessionStorage.removeItem('hoge');
    console.log(sessionStorage.getItem('hoge'))); // => null
    console.log(sessionStorage.length()); // => 1
    
    sessionStorage.clear();
    console.log(sessionStorage.getItem('fuga'))); // => null
    console.log(sessionStorage.length()); // => 0

also use on browser

On browser, storages will attached to window automatically.
Now, almost all browsers have implemented web-storage, so that
native module is used in natural.

If you want to use this storages on a modern browser,
you have to prepare "module.exports" object or "global" object
on window namespace.

Ways to import module with using "require-js"
or "foonyah" is awesome way for importing module.

And more, 4 functions to access "document.cookie" are attached to exports.
docCookie(document) ... change cookie document to arguments[0] provided.
keyCookie() ... get cookies key array.
setCookie(key, value, options) ... set a cookie value. options accepts or to given days of period.
getCookie(key) .. get a cookie value.

<script type="text/javascript">
 
    var src = 'https://raw.github.com/ystskm/web-storages-js/master/web-storages.js';
    foonyah.require(src).done(function(){
      localStorage.setItem( ... )
    });
 
</script> 

install

npm i web-storages

Downloadsweekly downloads

1

version

0.1.3

license

none

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability