From 1211624d211e58b3ece1f9ae7a3280d00b181479 Mon Sep 17 00:00:00 2001 From: Andrea Carraro Date: Wed, 22 May 2024 13:12:34 +0200 Subject: [PATCH] refactor: abstract stringify function --- src/utils/makeTsJsonSchema/index.ts | 9 ++------- .../{makeCircularRefReplacer.ts => stringify.ts} | 13 +++++++++---- .../stringify.test.ts} | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) rename src/utils/makeTsJsonSchema/{makeCircularRefReplacer.ts => stringify.ts} (71%) rename test/{makeCircularRefReplacer.test.ts => unit-tests/stringify.test.ts} (62%) diff --git a/src/utils/makeTsJsonSchema/index.ts b/src/utils/makeTsJsonSchema/index.ts index 9959931f..01b17790 100644 --- a/src/utils/makeTsJsonSchema/index.ts +++ b/src/utils/makeTsJsonSchema/index.ts @@ -1,8 +1,7 @@ -import { stringify } from 'comment-json'; +import { stringify } from './stringify'; import { replaceInlinedRefsWithStringPlaceholder } from './replaceInlinedRefsWithStringPlaceholder'; import { replacePlaceholdersWithImportedSchemas } from './replacePlaceholdersWithImportedSchemas'; import { replacePlaceholdersWithRefs } from './replacePlaceholdersWithRefs'; -import { makeCircularRefReplacer } from './makeCircularRefReplacer'; import { patchJsonSchema } from './patchJsonSchema'; import { formatTypeScript } from '../'; import type { @@ -48,11 +47,7 @@ export async function makeTsJsonSchema({ * Stringifying schema with "comment-json" instead of JSON.stringify * to generate inline comments for "inline" refHandling */ - const stringifiedSchema = stringify( - patchedSchema, - makeCircularRefReplacer(), - 2, - ); + const stringifiedSchema = stringify(patchedSchema); let tsSchema = ` const schema = ${stringifiedSchema} as const; diff --git a/src/utils/makeTsJsonSchema/makeCircularRefReplacer.ts b/src/utils/makeTsJsonSchema/stringify.ts similarity index 71% rename from src/utils/makeTsJsonSchema/makeCircularRefReplacer.ts rename to src/utils/makeTsJsonSchema/stringify.ts index 9bb893f3..16f1225a 100644 --- a/src/utils/makeTsJsonSchema/makeCircularRefReplacer.ts +++ b/src/utils/makeTsJsonSchema/stringify.ts @@ -1,12 +1,11 @@ +import { stringify as commentJsonStringify } from 'comment-json'; + /** * JSON.stringify replacer * Replace circular references with {} * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references */ -export function makeCircularRefReplacer(): ( - key: string, - value: unknown, -) => unknown { +function makeCircularRefReplacer(): (key: string, value: unknown) => unknown { const ancestors: unknown[] = []; return function (this: unknown, key: string, value: unknown) { if (typeof value !== 'object' || value === null) { @@ -28,3 +27,9 @@ export function makeCircularRefReplacer(): ( return value; }; } + +const circularReplacer = makeCircularRefReplacer(); + +export function stringify(input: unknown): string { + return commentJsonStringify(input, circularReplacer, 2); +} diff --git a/test/makeCircularRefReplacer.test.ts b/test/unit-tests/stringify.test.ts similarity index 62% rename from test/makeCircularRefReplacer.test.ts rename to test/unit-tests/stringify.test.ts index dc6ae062..9b85e9fc 100644 --- a/test/makeCircularRefReplacer.test.ts +++ b/test/unit-tests/stringify.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect } from 'vitest'; -import { makeCircularRefReplacer } from '../src/utils/makeTsJsonSchema/makeCircularRefReplacer'; +import { stringify } from '../../src/utils/makeTsJsonSchema/stringify'; -describe('makeCircularRefReplacer', () => { +describe('stringify', () => { it('replaces first nested circular occurrency with "{}"', () => { const input: { a: unknown; b: { c: unknown } } = { a: 'foo', @@ -11,7 +11,7 @@ describe('makeCircularRefReplacer', () => { }; input['b']['c'] = input; - const actual = JSON.parse(JSON.stringify(input, makeCircularRefReplacer())); + const actual = JSON.parse(stringify(input)); const expected = { a: 'foo', b: { c: {} },