From 2763f1bbc63cc9fef97de6399b148e926febd536 Mon Sep 17 00:00:00 2001 From: tomastrajan Date: Sun, 16 Sep 2018 16:45:52 +0200 Subject: [PATCH] feat(core): support persistence of state with camelCase props in the local storage --- .../local-storage.service.spec.ts | 42 +++++++++++++++---- .../local-storage/local-storage.service.ts | 22 +++++++--- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/app/core/local-storage/local-storage.service.spec.ts b/src/app/core/local-storage/local-storage.service.spec.ts index e9dfb48c3..b0f3f2814 100644 --- a/src/app/core/local-storage/local-storage.service.spec.ts +++ b/src/app/core/local-storage/local-storage.service.spec.ts @@ -1,18 +1,46 @@ -import { TestBed, inject } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { LocalStorageService } from './local-storage.service'; describe('LocalStorageService', () => { + let service: LocalStorageService; + beforeEach(() => { TestBed.configureTestingModule({ providers: [LocalStorageService] }); + service = TestBed.get(LocalStorageService); + }); + + afterEach(() => localStorage.clear()); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should get and set the item', () => { + service.setItem('TEST', 'item'); + expect(service.getItem('TEST')).toBe('item'); + }); + + it('should load initial state', () => { + service.setItem('TEST.PROP', 'value'); + expect(LocalStorageService.loadInitialState()).toEqual({ + test: { prop: 'value' } + }); }); - it( - 'should be created', - inject([LocalStorageService], (service: LocalStorageService) => { - expect(service).toBeTruthy(); - }) - ); + it('should load nested initial state', () => { + service.setItem('TEST.PROP1.PROP2', 'value'); + expect(LocalStorageService.loadInitialState()).toEqual({ + test: { prop1: { prop2: 'value' } } + }); + }); + + it('should load nested initial state with camel case properties and object value', () => { + service.setItem('TEST.PROP.SUB-PROP', 'value'); + expect(LocalStorageService.loadInitialState()).toEqual({ + test: { prop: { subProp: 'value' } } + }); + }); }); diff --git a/src/app/core/local-storage/local-storage.service.ts b/src/app/core/local-storage/local-storage.service.ts index 4f14c70eb..db1b9682b 100644 --- a/src/app/core/local-storage/local-storage.service.ts +++ b/src/app/core/local-storage/local-storage.service.ts @@ -9,14 +9,24 @@ export class LocalStorageService { static loadInitialState() { return Object.keys(localStorage).reduce((state: any, storageKey) => { if (storageKey.includes(APP_PREFIX)) { - state = state || {}; - const stateKey = storageKey + const stateKeys = storageKey .replace(APP_PREFIX, '') .toLowerCase() - .split('.'); + .split('.') + .map(key => + key + .split('-') + .map( + (token, index) => + index === 0 + ? token + : token.charAt(0).toUpperCase() + token.slice(1) + ) + .join('') + ); let currentStateRef = state; - stateKey.forEach((key, index) => { - if (index === stateKey.length - 1) { + stateKeys.forEach((key, index) => { + if (index === stateKeys.length - 1) { currentStateRef[key] = JSON.parse(localStorage.getItem(storageKey)); return; } @@ -25,7 +35,7 @@ export class LocalStorageService { }); } return state; - }, undefined); + }, {}); } setItem(key: string, value: any) {