-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make settings code resilient + refactoring (#8)
- Loading branch information
1 parent
dee1f2d
commit 4c68686
Showing
16 changed files
with
127 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Install, test and Deploy LD Explorer | ||
|
||
name: Test and Deploy | ||
name: LD-Explorer | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# How to contributing | ||
# How To Contribute | ||
|
||
## Raising issues | ||
|
||
|
Binary file modified
BIN
-25 Bytes
(100%)
e2e/tests/smoke/navigation.spec.ts-snapshots/navigation-2-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* (c) Crown Copyright GCHQ */ | ||
|
||
import { retrieveJSONObject, storeJSONObject } from '$lib/util/localStorage.utils'; | ||
import { namespace } from '$lib/constants'; | ||
import { writable } from 'svelte/store'; | ||
|
||
/** | ||
* | ||
* @param key storage key to use | ||
* @param defaultValue value to use if no value exists already | ||
* @returns | ||
*/ | ||
export function createLocalStorageJSONStore<T>(key: string, defaultValue: T) { | ||
const namespacedKey = `${namespace}${key}`; | ||
|
||
const { set, subscribe, update } = writable<T>(retrieveJSONObject(namespacedKey) || defaultValue); | ||
|
||
subscribe((store) => { | ||
storeJSONObject<T>(namespacedKey, store); | ||
}); | ||
|
||
return { | ||
restore() { | ||
set(defaultValue); | ||
}, | ||
subscribe, | ||
update | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* (c) Crown Copyright GCHQ */ | ||
|
||
import { createLocalStorageStore } from './localStorage.store'; | ||
import { createLocalStorageJSONStore } from './localStorageJson.store'; | ||
import defaultPrefixes from '$lib/data/prefixes.json'; | ||
|
||
export const prefixes = createLocalStorageStore('prefixes', defaultPrefixes); | ||
export const prefixes = createLocalStorageJSONStore('prefixes', defaultPrefixes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* (c) Crown Copyright GCHQ */ | ||
|
||
import { retrieveJSONObject, storeJSONObject } from './localStorage.utils'; | ||
|
||
const exampleObject = { | ||
foo: 'bar', | ||
fizz: 2, | ||
bang: true | ||
}; | ||
|
||
describe('localStorage functionality', () => { | ||
afterEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
describe(storeJSONObject, () => { | ||
it('Sets an object on localStorage with a given key', () => { | ||
storeJSONObject('test', exampleObject); | ||
expect(JSON.parse(localStorage.getItem('test') || '""')).toStrictEqual(exampleObject); | ||
}); | ||
}); | ||
|
||
describe(retrieveJSONObject, () => { | ||
it('Returns an object from a given key of localStorage', () => { | ||
localStorage.setItem('test', JSON.stringify(exampleObject)); | ||
expect(retrieveJSONObject('test')).toStrictEqual(exampleObject); | ||
}); | ||
|
||
it('Fails gracefully upon encoutering corrupt storage items', () => { | ||
// Arrange | ||
const consoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); | ||
localStorage.setItem('test', 'this is not valid JSON'); | ||
|
||
// Act | ||
const storedValue = retrieveJSONObject('test'); | ||
|
||
// Assert | ||
expect(consoleWarn).toHaveBeenCalledOnce(); | ||
expect(storedValue).toStrictEqual({}); | ||
}); | ||
|
||
it('Returns null if localStorage does not contain a given key', () => { | ||
expect(retrieveJSONObject('test')).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* (c) Crown Copyright GCHQ */ | ||
|
||
/** | ||
* Utilities related to any interactions with the browser's localStorage API. | ||
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API | ||
*/ | ||
|
||
/** | ||
* Set a key in local storage to contain an object. No-op if a non-JSON | ||
* value is passed in. | ||
* @param key | ||
* @param value | ||
*/ | ||
export function storeJSONObject<T>(key: string, value: T) { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
} | ||
|
||
/** | ||
* Get a key from local storage and parse it as JSON. If for some reason the | ||
* local storage is corrupt, an empty object is returned. | ||
* @param key | ||
* @returns value from store, parsed | ||
*/ | ||
export function retrieveJSONObject(key: string) { | ||
const value = localStorage.getItem(key); | ||
|
||
try { | ||
return value && JSON.parse(value); | ||
} catch (e) { | ||
console.warn('Unable to read value from local storage.', e); | ||
return {}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.