localstorageex
v1.3.5
Published
it use for extending the function ability of localStorage it can store json,array,number,boolean,string excepte function and convert them automatically
Downloads
5
Readme
localstorageex
A simple, fast and small lib that extend the ability of localStorage for browser. we can save or get obj directly without JSON.stringify or JSON.parse.
Install
via npm
$ npm install localstorageex --save
via yarn
$ yarn add localstorageex
Use
const localStorageEx = require("localstorageex");
// or
import localStorageEx from "localstorageex";
// Note. undefined or null values can't be saved.
// eg:
localStorageEx.set("foo", null); // will throw an error.
let a = {
a: 1,
b: "",
c: [1, 2],
d: {
e: {
m: [{ k: 2, d: null }],
},
f: null,
},
};
localStorageEx.set("some_obj_key", a);
console.log(localStorageEx.get("some_obj_key"));
/*
print {
a:1,
b:'',
c:[1,2],
d:{
e:{
m:[{k:2,d: null}]
},
f:null
}
}
*/
localStorageEx.set("key0", 1);
console.log(localStorageEx.get("key0")); // print 1
localStorageEx.set("key1", 2.2);
console.log(localStorageEx.get("key1")); // print 2.2
localStorageEx.set("key2", "some value");
console.log(localStorageEx.get("key2")); // print 'some value'
localStorageEx.set("key3", false);
console.log(localStorageEx.get("key3")); // print false
localStorageEx.set("key4", true);
console.log(localStorageEx.get("key4")); // print true
localStorageEx.set("key5", []);
console.log(localStorageEx.get("key5")); // print []
localStorageEx.set("key5_1", "[]");
console.log(localStorageEx.get("key5_1")); // print []
localStorageEx.set("key6", [1, 2]);
console.log(localStorageEx.get("key6")); // print [1,2]
localStorageEx.set("key6_1", "[1,2]");
console.log(localStorageEx.get("key6_1")); // print [1,2]
localStorageEx.set("key7", {});
console.log(localStorageEx.get("key7")); // print {}
localStorageEx.set("key8", "");
console.log(localStorageEx.get("key8")); // print ''
Remove Key
// remove key.
localStorageEx.remove("some_key"); // the value for the key will be removed.
console.log(localStorageEx.get("some_key")); // print null
Use Prefix
// by default there is no prefix for key. but you can customize it by yourself.
// each key store in different prefix won't impact each other. we can take it as namespace.
// eg:
// use the default prefix. default prefix is a empty string.
localStorageEx.set('key0, 'this is first value')
localStorageEx.setPrefix('_my_prefix_')
console.log(localStorageEx.get('key0')) // print null
localStorageEx.set('key0, 1)
localStorageEx.set('my_key0', [1,2,{c:false}])
console.log(localStorageEx.get('key0')) // print 1
console.log(localStorageEx.get('my_key0')) // print [1,2,{c:false}]
localStorageEx.setPrefix('_next_prefix_')
console.log(localStorageEx.get('key0')) // print null
console.log(localStorageEx.get('my_key0')) // print null
localStorageEx.set('key0', false)
console.log(localStorageEx.get('key0')) // print false
localStorageEx.setPrefix('_my_prefix_')
console.log(localStorageEx.get('key0')) // print 1
console.log(localStorageEx.get('my_key0')) // print [1,2,{c:false}]
localStorageEx.setPrefix('')
console.log(localStorageEx.get('key0')) // print 'this is first value'
Support
If you have any problem to use it or suggestion. please send a email to [email protected]