Skip to content

Commit

Permalink
Merge pull request #176 from jderrough/master
Browse files Browse the repository at this point in the history
Boolean values at root
  • Loading branch information
BBlackwo authored Jan 17, 2021
2 parents c38b89f + 9cdc1c5 commit 8c5b1a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
41 changes: 28 additions & 13 deletions spec/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class TypeA {
if (value.afield) {
return new TypeB(value.afield);
} else {
return new TypeA(value.astring, value.anumber, value.adate, value.aclass);
return new TypeA(value.astring, value.anumber, value.aboolean, value.adate, value.aclass);
}
}
return dateReviver(key, value);
}

static replacer(key: string, value: any) {
if (key === 'anumber' || key === 'adate') {
if (key === 'anumber' || key === 'aboolean' || key === 'adate') {
return undefined;
}
return value;
Expand All @@ -36,12 +36,13 @@ class TypeA {
}

static deserialize(json: any): TypeA {
return new TypeA(json.astring, json.anumber, json.adate, new TypeB(json.aclass.afield));
return new TypeA(json.astring, json.anumber, json.aboolean, json.adate, new TypeB(json.aclass.afield));
}

constructor(
public astring: string = undefined,
public anumber: number = undefined,
public aboolean: boolean = undefined,
public adate: Date = undefined,
public aclass: TypeB = undefined
) {}
Expand Down Expand Up @@ -87,27 +88,20 @@ function mockStorageKeySerializer(key) {
}

describe('ngrxLocalStorage', () => {
let t1 = new TypeA('Testing', 3.14159, new Date('1968-11-16T12:30:00Z'), new TypeB('Nested Class'));
let t1 = new TypeA('Testing', 3.14159, true, new Date('1968-11-16T12:30:00Z'), new TypeB('Nested Class'));

let t1Json = JSON.stringify(t1);

let t1Filtered = new TypeA('Testing', undefined, undefined, new TypeB('Nested Class'));
let t1Filtered = new TypeA('Testing', undefined, undefined, undefined, new TypeB('Nested Class'));

let t1FilteredJson = JSON.stringify(t1Filtered);

let t1Simple = { astring: 'Testing', adate: '1968-11-16T12:30:00.000Z', anumber: 3.14159 };
let t1Simple = { astring: 'Testing', adate: '1968-11-16T12:30:00.000Z', anumber: 3.14159, aboolean: true };

let initialState = { state: t1 };

let initialStateJson = JSON.stringify(initialState);

let undefinedState = { state: undefined };

let undefinedStateJson = JSON.stringify(undefinedState);

const primitiveStr = 'string is not an object';
const initialStatePrimitiveStr = { state: primitiveStr };

beforeEach(() => {
localStorage.clear();
});
Expand All @@ -133,6 +127,9 @@ describe('ngrxLocalStorage', () => {
});

it('simple string', () => {
const primitiveStr = 'string is not an object';
const initialStatePrimitiveStr = { state: primitiveStr };

const s = new MockStorage();
const skr = mockStorageKeySerializer;

Expand All @@ -145,6 +142,24 @@ describe('ngrxLocalStorage', () => {
expect(finalState.state).toEqual(primitiveStr);
});

[true, false].forEach((bool) => {
it(`simple ${bool} boolean`, () => {
const primitiveBool = bool;
const initialStatePrimitiveBool = { state: primitiveBool };

const s = new MockStorage();
const skr = mockStorageKeySerializer;

syncStateUpdate(initialStatePrimitiveBool, ['state'], s, skr, false);

const raw = s.getItem('state');
expect(JSON.parse(raw)).toEqual(primitiveBool);

const finalState: any = rehydrateApplicationState(['state'], s, skr, true);
expect(finalState.state).toEqual(primitiveBool);
});
});

it('filtered', () => {
// Use the filter by field option to round-trip an object while
// filtering out the anumber and adate filed
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const rehydrateApplicationState = (
const isObjectRegex = new RegExp('{|\\[');
let raw = stateSlice;

if (stateSlice === 'null' || isObjectRegex.test(stateSlice.charAt(0))) {
if (stateSlice === 'null' || stateSlice === 'true' || stateSlice === 'false' || isObjectRegex.test(stateSlice.charAt(0))) {
raw = JSON.parse(stateSlice, reviver);
}

Expand Down

0 comments on commit 8c5b1a2

Please sign in to comment.