dsql-admin/dsql-app/package-shared/utils/ejson.js

39 lines
890 B
JavaScript
Raw Normal View History

2024-11-05 14:18:40 +00:00
/**
*
* @param {string | null | number} string
* @param {(this: any, key: string, value: any) => any} [reviver]
2024-12-05 07:03:33 +00:00
* @returns {Object<string, any> | Object<string, any>[] | undefined}
2024-11-05 14:18:40 +00:00
*/
function parse(string, reviver) {
if (!string) return undefined;
if (typeof string == "object") return string;
if (typeof string !== "string") return undefined;
try {
return JSON.parse(string, reviver);
} catch (error) {
return undefined;
}
}
/**
*
* @param {any} value
2024-12-05 07:03:33 +00:00
* @param {((this: any, key: string, value: any) => any) | null} [replacer]
2024-11-05 14:18:40 +00:00
* @param { string | number } [space]
* @returns {string | undefined}
*/
function stringify(value, replacer, space) {
try {
return JSON.stringify(value, replacer, space);
} catch (error) {
return undefined;
}
}
const EJSON = {
parse,
stringify,
};
module.exports = EJSON;