Skip to content

Commit

Permalink
refactor: abstract stringify function
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed May 22, 2024
1 parent 0637f9a commit 1211624
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/utils/makeTsJsonSchema/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -28,3 +27,9 @@ export function makeCircularRefReplacer(): (
return value;
};
}

const circularReplacer = makeCircularRefReplacer();

export function stringify(input: unknown): string {
return commentJsonStringify(input, circularReplacer, 2);
}
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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: {} },
Expand Down

0 comments on commit 1211624

Please sign in to comment.