Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/client/src/utils/localstorage.ts

32 lines
725 B
TypeScript
Raw Normal View History

2020-01-24 04:23:26 +13:00
export function set<T extends string | number | boolean>(key: string, val: T) {
switch (typeof val) {
case 'number':
localStorage.setItem(key, val.toString())
break
case 'string':
localStorage.setItem(key, val)
break
case 'boolean':
localStorage.setItem(key, val ? '1' : '0')
break
}
}
export function get<T extends string | number | boolean>(key: string, def: T): T {
let store = localStorage.getItem(key)
if (store) {
switch (typeof def) {
case 'number':
return parseInt(store) as T
case 'string':
return store as T
case 'boolean':
return (store === '1') as T
default:
return def
}
}
return def
}