web-storage-proxy
v1.0.0
Published
Super simple webstorage proxy adding your own serialialization/deserialization pipeline
Downloads
3
Readme
web-storage-proxy
Proxies getItem
and setItem
storage apis (e.g. localStorage, sessionStorage) and add in your
own serializers and deserializers.
Installation
npm install --save web-storage-proxy
Usage
The following code will serialize JavaScript objects and store them compressed in localStorage.
const createStorageProxy = require('web-storage-proxy');
const lzString = require('lz-string');
const { localStorage } = global;
const compressedStorage = createStorageProxy({
storage: localStorage,
serializer: [
JSON.stringify,
lzString.compress,
],
deserializer: [
lzString.decompress,
JSON.parse,
],
});
const input = {
this: 'is', just: 'an', exmaple: { of: { a: { deeply: { nested: 'object' } } } }
};
console.log(`Input ${JSON.stringify(input)}`)
compressedStorage.setItem('foo', input);
console.log(`Compressed value: ${memStorage.store.foo}`)
const result = compressedStorage.getItem('foo');
console.log(`Deserialized ${JSON.stringify(result)}`)
Play with example.js
to learn more.
Requirements
This library has no dependencies, however:
ES6 Proxy is used. If you are using this in the browser you should include the Proxy polyfill.
Array.reduce
is used. Modern browsers support this.