Added JSONSerialize
All checks were successful
Build / Build NPM Project (push) Successful in 1m12s
Build / Tag Version (push) Successful in 12s
Build / Publish Documentation (push) Successful in 48s

This commit is contained in:
Zakary Timson 2024-11-24 11:08:17 -05:00
parent e8fdca8236
commit 2522635815
2 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.23.1",
"version": "0.23.2",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -224,6 +224,17 @@ export function JSONAttemptParse<T1, T2>(json: T2): T1 | T2 {
catch { return json; }
}
/**
* Stringifies objects & skips primitives
*
* @param {any} obj Object to convert to serializable value
* @return {string | T} Serialized value
*/
export function JSONSerialize<T1>(obj: T1): T1 | string {
if(typeof obj == 'object' && obj != null) return JSONSanitize(obj);
return obj;
}
/**
* Convert an object to a JSON string avoiding any circular references.
*