| 1 | import {Storage} from "./storage"; |
| 2 | |
| 3 | export class LocalStorage implements Storage { |
| 4 | public getValue(key: string): string { |
| 5 | let result: string; |
| 6 | if (window.localStorage) { |
| 7 | result = window.localStorage.getItem(key); |
| 8 | if (!result) { |
| 9 | // Somehow we stored an invalid value. Destroy it! |
| 10 | this.removeKey(key); |
| 11 | } |
| 12 | } |
| 13 | return result; |
| 14 | } |
| 15 | |
| 16 | public getValues(keys: string[]): {} { |
| 17 | let values = {}; |
| 18 | if (window.localStorage && keys) { |
| 19 | for (let i = 0; i < keys.length; i++) { |
| 20 | let result = window.localStorage.getItem(keys[i]); |
| 21 | if (!result) { |
| 22 | // Somehow we stored an invalid value. Destroy it! |
| 23 | this.removeKey(keys[i]); |
| 24 | } else { |
| 25 | values[keys[i]] = result; |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | return values; |
| 30 | } |
| 31 | |
| 32 | public setValue(key: string, value: string): void { |
| 33 | if (window.localStorage) { |
| 34 | if (!value) { |
| 35 | window.localStorage.removeItem(key); |
| 36 | } else { |
| 37 | window.localStorage.setItem(key, value); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public removeKey(key: string): boolean { |
| 43 | if (window.localStorage) { |
| 44 | window.localStorage.removeItem(key); |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | } |
nothing calls this directly
no outgoing calls
no test coverage detected