Skip to content

Commit e9f648d

Browse files
committed
added tryJSONStringify / tryJSONParse utils
1 parent ff8fe80 commit e9f648d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/utils/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,33 @@ export function isFunction(item) {
3030
return (item && typeof item === 'function');
3131
}
3232

33+
/**
34+
*
35+
* @param string
36+
* @returns {*}
37+
*/
38+
export function tryJSONParse(string) {
39+
try {
40+
return JSON.parse(string);
41+
} catch (jsonError) {
42+
return string;
43+
}
44+
}
45+
46+
/**
47+
*
48+
* @param data
49+
* @returns {*}
50+
*/
51+
export function tryJSONStringify(data) {
52+
try {
53+
return JSON.stringify(data);
54+
} catch (jsonError) {
55+
return undefined;
56+
}
57+
}
58+
59+
3360
// noinspection Eslint
3461
export const windowOrGlobal = (typeof self === 'object' && self.self === self && self) || (typeof global === 'object' && global.global === global && global) || this;
3562

0 commit comments

Comments
 (0)