From 25226358150cd2552f184dd37baf1ed472dd35fd Mon Sep 17 00:00:00 2001 From: ztimson Date: Sun, 24 Nov 2024 11:08:17 -0500 Subject: [PATCH] Added JSONSerialize --- package.json | 2 +- src/objects.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 02fbfd8..9b3dc4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.23.1", + "version": "0.23.2", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/objects.ts b/src/objects.ts index 0560e0e..bab46bf 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -224,6 +224,17 @@ export function JSONAttemptParse(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(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. *