Skip to content

Commit b5d0d29

Browse files
refactor(key-storage): renamed to use-store (#23)
1 parent 965d355 commit b5d0d29

File tree

13 files changed

+59
-76
lines changed

13 files changed

+59
-76
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './key-storage';
1+
export * from './use-store';

apps/www/src/app/library-components/utilities/key-storage/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

apps/www/src/app/library-components/utilities/key-storage/key-storage.spec.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './stores';
2+
export * from './key-store';
3+
export * from './use-store';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {KeyStore} from "./key-store";
2+
import {InMemoryStore} from "./stores";
3+
4+
describe('Key Store', () => {
5+
6+
const key = 'key';
7+
const defaultValue = 'default';
8+
9+
let store: KeyStore;
10+
11+
beforeEach(() => {
12+
store = new KeyStore(
13+
key,
14+
defaultValue,
15+
new InMemoryStore(),
16+
);
17+
});
18+
19+
it('should return a stored record', () => {
20+
const record = 'value';
21+
store.setValue(record);
22+
expect(store.value).toEqual(record);
23+
});
24+
25+
it('should overwrite the value', () => {
26+
const record1 = 'value';
27+
const record2 = 'value-2';
28+
29+
store.setValue(record1);
30+
store.setValue(record2);
31+
32+
expect(store.value).toEqual(record2);
33+
});
34+
35+
it('should return the default value if the record doesnt exist', () => {
36+
expect(store.value).toBe(defaultValue);
37+
});
38+
39+
})
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class KeyStorage<T = unknown> {
1+
export class KeyStore<T = unknown> {
22

33
constructor(
44
private _key: string,
@@ -7,20 +7,16 @@ export class KeyStorage<T = unknown> {
77
) {
88
}
99

10-
getItem(): T {
10+
get value(): T {
1111
const data = this._store.getItem(this._key);
1212
return data
1313
? JSON.parse(data)
1414
: this._defaultValue;
1515
}
1616

17-
setItem(value: T): void {
17+
setValue(value: T): void {
1818
const data = JSON.stringify(value);
1919
this._store.setItem(this._key, data);
2020
}
2121

22-
removeItem(): void {
23-
this._store.removeItem(this._key);
24-
}
25-
2622
}

apps/www/src/app/library-components/utilities/key-storage/create-key-storage.spec.ts renamed to apps/www/src/app/library-components/utilities/use-store/use-storage.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
import {TestBed} from "@angular/core/testing";
2-
import {createKeyStorage} from "./create-key-storage";
3-
import {KeyStorage} from "./key-storage";
2+
import {useStore} from "./use-store";
3+
import {KeyStore} from "./key-store";
44
import {InMemoryStore, LocalStorageStore} from "./stores";
55

6-
describe('Create Key Storage', () => {
6+
describe('useStore', () => {
77

88
const key = 'key';
99
const defaultValue = 'default-value';
1010

1111
it('should return an Angular injection token', () => {
12-
const token = createKeyStorage(key, defaultValue);
12+
const token = useStore(key, defaultValue);
1313
const store = TestBed.inject(token);
14-
expect(store).toBeInstanceOf(KeyStorage);
14+
expect(store).toBeInstanceOf(KeyStore);
1515
});
1616

1717
it('should configure the key storage correctly', () => {
18-
const token = createKeyStorage(key, defaultValue);
18+
const token = useStore(key, defaultValue);
1919
const store = TestBed.inject(token);
2020
expect(store['_key']).toBe(key)
2121
expect(store['_defaultValue']).toBe(defaultValue)
2222
});
2323

2424
it('should configure the key storage to use the localStorageStore by default', () => {
25-
const token = createKeyStorage(key, defaultValue);
25+
const token = useStore(key, defaultValue);
2626
const store = TestBed.inject(token);
2727
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
2828
});
2929

3030
it('should configure the key storage to use the localStorage Store', () => {
31-
const token = createKeyStorage(key, defaultValue, );
31+
const token = useStore(key, defaultValue, new LocalStorageStore());
3232
const store = TestBed.inject(token);
3333
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
3434
});
3535

3636
it('should configure the key storage to use the InMemory Store', () => {
37-
const token = createKeyStorage(key, defaultValue, new InMemoryStore());
37+
const token = useStore(key, defaultValue, new InMemoryStore());
3838
const store = TestBed.inject(token);
3939
expect(store['_store']).toBeInstanceOf(InMemoryStore)
4040
});
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {InjectionToken} from "@angular/core";
2-
import {KeyStorage} from "./key-storage";
2+
import {KeyStore} from "./key-store";
33
import {localStorageStore} from "./stores";
44

5-
export const createKeyStorage = <T = unknown>(
5+
export const useStore = <T = unknown>(
66
key: string,
77
defaultValue: T,
88
store: Storage = localStorageStore,
99
) =>
1010
new InjectionToken(
11-
`storage-key-${key}`, {
11+
`store-${key}`, {
1212
providedIn: 'root',
13-
factory: () => new KeyStorage<T>(key, defaultValue, store)
13+
factory: () => new KeyStore<T>(key, defaultValue, store)
1414
});
1515

0 commit comments

Comments
 (0)