From 035e79b3b73e00e291665cda33f88f57972b773c Mon Sep 17 00:00:00 2001 From: Roberto Sebestyen Date: Fri, 26 May 2023 21:42:16 +0000 Subject: [PATCH] Remove dependency on json-stringify-safe --- package-lock.json | 11 ++----- package.json | 2 -- src/colors/colorize.ts | 10 +++--- src/index.ts | 1 + src/json-stringify-safe/index.ts | 3 ++ src/json-stringify-safe/stringify-safe.ts | 38 +++++++++++++++++++++++ src/logger.ts | 3 +- src/safe-object-assign.ts | 2 +- 8 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 src/json-stringify-safe/index.ts create mode 100644 src/json-stringify-safe/stringify-safe.ts diff --git a/package-lock.json b/package-lock.json index eed43a8..2e09298 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "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" }, @@ -19,7 +18,6 @@ "@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", @@ -303,12 +301,6 @@ "integrity": "sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==", "dev": true }, - "node_modules/@types/json-stringify-safe": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@types/json-stringify-safe/-/json-stringify-safe-5.0.0.tgz", - "integrity": "sha512-UUA1sH0RSRROdInuDOA1yoRzbi5xVFD1RHCoOvNRPTNwR8zBkJ/84PZ6NhKVDtKp0FTeIccJCdQz1X2aJPr4uw==", - "dev": true - }, "node_modules/@types/keyv": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/@types/keyv/-/keyv-3.1.1.tgz", @@ -3386,7 +3378,8 @@ "node_modules/json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", - "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" + "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=", + "dev": true }, "node_modules/json5": { "version": "2.2.2", diff --git a/package.json b/package.json index 9751542..09aa370 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "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" }, @@ -60,7 +59,6 @@ "@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", diff --git a/src/colors/colorize.ts b/src/colors/colorize.ts index 2bab8a7..787dac5 100644 --- a/src/colors/colorize.ts +++ b/src/colors/colorize.ts @@ -1,4 +1,4 @@ -import stringify from 'json-stringify-safe'; +import { stringify } from '../json-stringify-safe/stringify-safe'; export interface IDefaultColorMap { black: string; @@ -127,8 +127,8 @@ export function colorJson(jsonInput: any, colorsInput: Partial { @@ -205,8 +205,8 @@ export function colorJson(jsonInput: any, colorsInput: Partial 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); + }; +} diff --git a/src/logger.ts b/src/logger.ts index acbd7af..5ce4c98 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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'; @@ -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({ diff --git a/src/safe-object-assign.ts b/src/safe-object-assign.ts index f6e5971..b34e067 100644 --- a/src/safe-object-assign.ts +++ b/src/safe-object-assign.ts @@ -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 */