#21. Добавление json парсеров (#27)

This commit is contained in:
Nikolay
2020-12-28 00:27:08 +03:00
committed by GitHub
parent 94f3b5452a
commit fbe6f9d286
3 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,10 @@
export const jsonParse = <T>(str?: string, defaultValue?: T): Undefinable<T> => {
const trimStr = str?.trim();
try {
const parsedValue = JSON.parse(trimStr ?? '');
return parsedValue === undefined ? defaultValue : parsedValue;
} catch (e) {
return defaultValue;
}
};