Skip to content

Commit

Permalink
Remove dependency on json-stringify-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro5id committed May 26, 2023
1 parent 4511bc5 commit 035e79b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 19 deletions.
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@
"dependencies": {
"app-root-path": "^3.1.0",
"dotenv": "^16.0.3",
"json-stringify-safe": "^5.0.1",
"source-map-support": "^0.5.21",
"winston": "^3.9.0"
},
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/chai": "^4.2.19",
"@types/cheerio": "^0.22.29",
"@types/json-stringify-safe": "^5.0.0",
"@types/mocha": "^8.2.2",
"@types/node": "^16.7.10",
"@types/request-promise": "^4.1.47",
Expand Down
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';
import { stringify } 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, null, spacing);
else json = stringify(JSON.parse(jsonInput), null, spacing);
if (typeof jsonInput !== 'string') json = stringify(jsonInput, undefined, spacing);
else json = stringify(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, null, spacing);
else json = stringify(JSON.parse(jsonInput), null, spacing);
if (typeof jsonInput !== 'string') json = stringify(jsonInput, undefined, spacing);
else json = stringify(JSON.parse(jsonInput), undefined, spacing);
return json;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export * from './callsites';
export * from './colors';
export * from './env';
export * from './json-stringify-safe';
export * from './capture-nested-stack-trace';
export * from './error-with-context';
export * from './format-stack-trace';
Expand Down
3 changes: 3 additions & 0 deletions src/json-stringify-safe/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// created from 'create-ts-index'

export * from './stringify-safe';
38 changes: 38 additions & 0 deletions src/json-stringify-safe/stringify-safe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type EntryProcessor = (key: string, value: any) => any;

export function stringify(obj: any, serializer?: EntryProcessor, indent?: string | number, decycler?: EntryProcessor): string {
const foo = getSerialize(serializer, decycler);
return JSON.stringify(obj, foo as any, indent);
}

export function getSerialize(serializer?: EntryProcessor, decycler?: EntryProcessor): EntryProcessor {
const stack: any[] = [];
const keys: any[] = [];

if (decycler == null)
decycler = (_key: any, value: any) => {
if (stack[0] === value) return '[Circular ~]';
return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
};

return function (key, value) {
if (stack.length > 0) {
const thisPos = stack.indexOf(this);
if (thisPos === -1) {
stack.push(this);
} else {
stack.splice(thisPos + 1);
}
if (thisPos === -1) {
keys.push(key);
} else {
keys.splice(thisPos, Infinity, key);
}
if (stack.includes(value)) value = decycler!.call(this, key, value);
} else {
stack.push(value);
}

return serializer == null ? value : serializer.call(this, key, value);
};
}
3 changes: 1 addition & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable:object-literal-sort-keys */
import appRootPath from 'app-root-path';
// import stringify from 'json-stringify-safe';
import * as path from 'path';
import * as w from 'winston';
import { ErrorWithContext } from './error-with-context';
Expand All @@ -13,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';
import { stringify } from './json-stringify-safe/stringify-safe';

// tslint:disable-next-line:no-var-requires
require('source-map-support').install({
Expand Down
2 changes: 1 addition & 1 deletion 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';
import { stringify } 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

0 comments on commit 035e79b

Please sign in to comment.