README
localstorage-enhance
Enhance of localStorage, allow to set expires and namespace.
Install
npm install localstorage-enhance
Usage
basic
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.set('key', 'value', 1000); // The third param is expires from now(ms)
LocalStoarge.get('key'); // value
setTimeout(() => LocalStoarge.get('key'), 2000); // null;
set expires
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.set('1', 1, 1000); // The third param is expires from now(ms), default to 0 means expires;
LocalStoarge.get('1'); // 1
setTimeout(() => LocalStoarge.get('1'), 500); // 1;
setTimeout(() => LocalStoarge.get('1'), 1500); // null;
use namespace
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.getNamespace(); // 'default'
LocalStoarge.setNamespace('abc').getNamespace() // 'abc'
LocalStoarge.getAllNamespace(); // [default', 'abc]
set and get
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.set('1', 123);
LocalStoarge.set('2', '234');
LocalStoarge.set('3', true);
LocalStoarge.set('4', { key: 'value', inner: { key2: 'value2' } });
LocalStoarge.set('5', null);
LocalStoarge.set('6', undefined);
LocalStoarge.get('1'); // 123
LocalStoarge.get('2'); // '234'
LocalStoarge.get('3'); // true
LocalStoarge.get('4'); // { key: 'value', inner: { key2: 'value2' } }
LocalStoarge.get('5'); // null
LocalStoarge.get('6'); // undefined
LocalStoarge.get('7'); // null
set and get in different namespace, then clear and clearAll.
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.set('1', 123);
LocalStoarge.set('1', 1234, 0, 'another-namespace');
LocalStoarge.getNamespace(); // default;
LocalStoarge.get('1'); // 123
LocalStoarge.get('1', 'another-namespace') // 1234
LocalStoarge.setNamespace('another-namespace').get('1') // 1234;
LocalStoarge.getNamespace(); //'another-namespace';
LocalStoarge.setNamespace('default');
LocalStoarge.clear('default');
LocalStoarge.get('1') // null;
LocalStoarge.getAll(); // {}
LocalStoarge.get('1', 'another-namespace'); // 1234
LocalStoarge.clearAll();
LocalStoarge.get('1', 'another-namespace'); // null
setMuilty, getMuilty and getAll
import LocalStoarge from 'localstorage-enhance';
LocalStoarge.setMuilty([{ key: '1', data: 1, exp: 1000 }, { key: '2', data: '2' }, { key: '3', data: false }]); // exp key is expires from now(ms), default to 0 means never expires;
LocalStoarge.set('4', null).set('5', undefined);
LocalStoarge.get('1'); // 1
LocalStoarge.get('2'); // '2'
LocalStoarge.get('3'); // false
LocalStoarge.get('4'); // null
LocalStoarge.get('5'); // undefined
LocalStoarge.getMuilty([]); // {}
LocalStoarge.getMuilty(['1', '2']); // { '1': 1, '2': '2' }
LocalStoarge.getMuilty(['3', '7']); // { '3': false, '7': null }
LocalStoarge.getAll('default'); // { '1': 1, '2': '2', '3': false, '4': null, '5': undefined }
API
declare type Data = string | number | boolean | undefined | null | Object;
interface DataObj {
key: string;
data: Data;
exp?: number; // ms. 0 means never expire.
}
interface LocalStorage { // namespace default to curNamespace, 'default' as default;
set: (key: string, data: Data, exp?: number, namespace?: string) => LocalStorage;
setMuilty: (datas: DataObj[], namespace?: string) => LocalStorage;
get: (key: string, namespace?: string) => Data;
getMuilty: <T extends string>(keys: T[], namespace?: string) => {
[K in T]: Data;
};
getAll: (namespace?: string) => {
[key: string]: Data;
};
setNamespace: (namespace: string) => LocalStorage; // // set curNamespace
getNamespace: () => string; // return curNamespace
getAllNamespace: () => string[];
clear: (namespace?: string) => LocalStorage; // clear data in namespace
clearAll: () => LocalStorage; // clear data in all namespaces
}