JSONAttemptPrase fallback
Some checks failed
Build / Build NPM Project (push) Failing after 48s
Build / Publish Documentation (push) Has been skipped
Build / Tag Version (push) Has been skipped

This commit is contained in:
2025-10-20 15:51:10 -04:00
parent a7b19900da
commit a9a9b04a5a
4 changed files with 44 additions and 41 deletions

View File

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

View File

@@ -9,6 +9,7 @@ export * from './files';
export * from './emitter';
export * from './errors';
export * from './http';
export * from './json';
export * from './jwt';
export * from './logger';
export * from './math';

40
src/json.ts Normal file
View File

@@ -0,0 +1,40 @@
/**
* Parse JSON but return the original string if it fails
*
* @param {any} json JSON string to parse
* @param fallback Fallback value if unable to parse, defaults to original string
* @return {string | T} Object if successful, original string otherwise
*/
export function JSONAttemptParse<T1, T2>(json: T2, fallback?: T1): T1 | T2 {
try { return JSON.parse(<any>json); }
catch { return fallback ?? 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.
*
* @param obj Object to convert to JSON
* @param {number} space Format the JSON with spaces
* @return {string} JSON string
*/
export function JSONSanitize(obj: any, space?: number): string {
const cache: any[] = [];
return JSON.stringify(obj, (key, value) => {
if(typeof value === 'object' && value !== null) {
if(cache.includes(value)) return '[Circular]';
cache.push(value);
}
return value;
}, space);
}

View File

@@ -1,3 +1,5 @@
import {JSONSanitize} from './json.ts';
export type Delta = { [key: string]: any | Delta | null };
@@ -303,43 +305,3 @@ export function objectMap<T>(obj: any, fn: (key: string, value: any) => any): T
return <any>Object.entries(obj).map(([key, value]: [string, any]) => [key, fn(key, value)])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
}
/**
* Parse JSON but return the original string if it fails
*
* @param {any} json JSON string to parse
* @return {string | T} Object if successful, original string otherwise
*/
export function JSONAttemptParse<T1, T2>(json: T2): T1 | T2 {
try { return JSON.parse(<any>json); }
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.
*
* @param obj Object to convert to JSON
* @param {number} space Format the JSON with spaces
* @return {string} JSON string
*/
export function JSONSanitize(obj: any, space?: number): string {
const cache: any[] = [];
return JSON.stringify(obj, (key, value) => {
if(typeof value === 'object' && value !== null) {
if(cache.includes(value)) return '[Circular]';
cache.push(value);
}
return value;
}, space);
}