Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #20 from RanzQ/master
Browse files Browse the repository at this point in the history
Support for replacer and reviver
  • Loading branch information
michaelcontento committed Apr 22, 2016
2 parents e855e2d + f66091c commit f310420
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ import createEngine from 'redux-storage-engine-localstorage';
const engine = createEngine('my-save-key');
```

You can customize the saving and loading process by providing a [`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter) and/or a [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter).

```js
import createEngine from 'redux-storage-engine-localstorage';

function replacer (key, value) {
if (typeof value === 'string') {
return 'foo';
}
return value;
}

function reviver (key, value) {
if (key === 'foo') {
return 'bar';
}
return value;
});

const engine = createEngine('my-save-key', replacer, reviver);
```

**Warning**: `localStorage` does not expose a async API and every save/load
operation will block the JS thread!

Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ describe('engine', () => {
return engine.load()
.should.eventually.be.rejectedWith('Unexpected token a');
});

it('should use a provided reviver', async () => {
const reviver = sinon.stub();
const engine = createEngine('key', null, reviver);

await engine.load();

reviver.should.have.been.called;
});
});

describe('save', () => {
Expand Down Expand Up @@ -97,5 +106,14 @@ describe('engine', () => {
return engine.save(a)
.should.eventually.be.rejectedWith('Converting circular structure to JSON');
});

it('should use a provided replacer', async () => {
const replacer = sinon.stub();
const engine = createEngine('key', replacer);

await engine.save({});

replacer.should.have.been.called;
});
});
});
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ function rejectWithMessage(error) {
return Promise.reject(error.message);
}

export default (key) => ({
export default (key, replacer, reviver) => ({
load() {
return new Promise((resolve) => {
const jsonState = localStorage.getItem(key);
resolve(JSON.parse(jsonState) || {});
resolve(JSON.parse(jsonState, reviver) || {});
}).catch(rejectWithMessage);
},

save(state) {
return new Promise((resolve) => {
const jsonState = JSON.stringify(state);
const jsonState = JSON.stringify(state, replacer);
localStorage.setItem(key, jsonState);
resolve();
}).catch(rejectWithMessage);
Expand Down

0 comments on commit f310420

Please sign in to comment.