https://github.com/fixate/web-storage-proxy
Proxy localstorage and sessionstorage to add serializers and deserializers
https://github.com/fixate/web-storage-proxy
javascript nodejs utility webstorage
Last synced: 2 months ago
JSON representation
Proxy localstorage and sessionstorage to add serializers and deserializers
- Host: GitHub
- URL: https://github.com/fixate/web-storage-proxy
- Owner: fixate
- Created: 2017-03-04T12:19:21.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-06T09:43:13.000Z (about 8 years ago)
- Last Synced: 2025-01-04T08:37:35.424Z (4 months ago)
- Topics: javascript, nodejs, utility, webstorage
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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.
```javascript
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
is used. If you are using this in the browser you should include the
[Proxy polyfill](https://github.com/GoogleChrome/proxy-polyfill).- `Array.reduce` is used. Modern browsers support this.