Skip to content

Commit

Permalink
update name of jsonStringifySafe
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro5id committed May 26, 2023
1 parent 035e79b commit 06da04d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/colors/colorize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringify } from '../json-stringify-safe/stringify-safe';
import { jsonStringifySafe } from '../json-stringify-safe/stringify-safe';

export interface IDefaultColorMap {
black: string;
Expand Down Expand Up @@ -127,8 +127,8 @@ export function colorJson(jsonInput: any, colorsInput: Partial<IColorConfigurati
let isWarnLevel = false;
let json: string;
if (supportsColor()) {
if (typeof jsonInput !== 'string') json = stringify(jsonInput, undefined, spacing);
else json = stringify(JSON.parse(jsonInput), undefined, spacing);
if (typeof jsonInput !== 'string') json = jsonStringifySafe(jsonInput, undefined, spacing);
else json = jsonStringifySafe(JSON.parse(jsonInput), undefined, spacing);
return (
(colorMap as any)[colors.separator] +
json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, (match: string) => {
Expand Down Expand Up @@ -205,8 +205,8 @@ export function colorJson(jsonInput: any, colorsInput: Partial<IColorConfigurati
'\x1b[0m'
);
} else {
if (typeof jsonInput !== 'string') json = stringify(jsonInput, undefined, spacing);
else json = stringify(JSON.parse(jsonInput), undefined, spacing);
if (typeof jsonInput !== 'string') json = jsonStringifySafe(jsonInput, undefined, spacing);
else json = jsonStringifySafe(JSON.parse(jsonInput), undefined, spacing);
return json;
}
}
11 changes: 10 additions & 1 deletion src/json-stringify-safe/stringify-safe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
export type EntryProcessor = (key: string, value: any) => any;

export function stringify(obj: any, serializer?: EntryProcessor, indent?: string | number, decycler?: EntryProcessor): string {
/**
* Stringifies JSON objects into strings in a safe way so that it can handle recursion and cyclical references
*
* @param obj the object to serialize
* @param serializer custom serializer
* @param indent indent for JSON formatting
* @param decycler a function to use when a cyclical refernce is found
* @returns the JSON string represantation
*/
export function jsonStringifySafe(obj: any, serializer?: EntryProcessor, indent?: string | number, decycler?: EntryProcessor): string {
const foo = getSerialize(serializer, decycler);
return JSON.stringify(obj, foo as any, indent);
}
Expand Down
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ToOneLine } from './to-one-line';
import { Env } from './env';
import { NewLineCharacter } from './new-line-character';
import { colorJson } from './colors/colorize';
import { stringify } from './json-stringify-safe/stringify-safe';
import { jsonStringifySafe } from './json-stringify-safe/stringify-safe';

// tslint:disable-next-line:no-var-requires
require('source-map-support').install({
Expand Down Expand Up @@ -219,7 +219,7 @@ export function FormatErrorObject(object: any) {
if (CONSOLE_LOG_COLORIZE && CONSOLE_LOG_COLORIZE.toLowerCase() === 'true') {
return `${colorJson(returnData)}${endOfLogCharacter}`;
} else {
const jsonString = stringify(returnData);
const jsonString = jsonStringifySafe(returnData);
return `${jsonString}${endOfLogCharacter}`;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/safe-object-assign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stringify } from './json-stringify-safe/stringify-safe';
import { jsonStringifySafe } from './json-stringify-safe/stringify-safe';
import { sortObject } from './sort-object';
// tslint:disable-next-line:no-var-requires
/* tslint:disable:only-arrow-functions */
Expand Down Expand Up @@ -62,8 +62,8 @@ export function safeObjectAssign(target: any, mergeStringProperties: string[], .
return mergeDeep(theTarget, ...theSources);
}

const targetCopy = JSON.parse(stringify(target));
const sourcesCopy = JSON.parse(stringify(sources));
const targetCopy = JSON.parse(jsonStringifySafe(target));
const sourcesCopy = JSON.parse(jsonStringifySafe(sources));

return mergeDeep(targetCopy, ...sourcesCopy);
}
Expand Down

0 comments on commit 06da04d

Please sign in to comment.