JSONAttemptPrase fallback
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.27.8",
|
"version": "0.27.9",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -9,6 +9,7 @@ export * from './files';
|
|||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './http';
|
export * from './http';
|
||||||
|
export * from './json';
|
||||||
export * from './jwt';
|
export * from './jwt';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './math';
|
export * from './math';
|
||||||
|
40
src/json.ts
Normal file
40
src/json.ts
Normal 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);
|
||||||
|
}
|
@@ -1,3 +1,5 @@
|
|||||||
|
import {JSONSanitize} from './json.ts';
|
||||||
|
|
||||||
export type Delta = { [key: string]: any | Delta | null };
|
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)])
|
return <any>Object.entries(obj).map(([key, value]: [string, any]) => [key, fn(key, value)])
|
||||||
.reduce((acc, [key, value]) => ({ ...acc, [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);
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user