-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
31 lines (28 loc) · 939 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { useState } from "react";
import cookie from "cookie";
// Hook for Local Storage (credit: https://usehooks.com/useLocalStorage/)
export function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
return initialValue;
}
});
const setValue = (value) => {
try {
const valueToStore =
value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {}
};
return [storedValue, setValue];
}
export function parseCookies(req) {
return cookie.parse(req ? req.headers.cookie || "" : document.cookie);
}
export function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}