From bdd003b16274eb313540aa4d9ad0a90ca81790d0 Mon Sep 17 00:00:00 2001 From: Davide Caroselli Date: Sat, 20 Apr 2024 13:57:45 +0200 Subject: [PATCH] Code refactoring --- src/jsonthis.ts | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/jsonthis.ts b/src/jsonthis.ts index 6b96a17..981758a 100644 --- a/src/jsonthis.ts +++ b/src/jsonthis.ts @@ -1,11 +1,11 @@ import {camelCase, pascalCase, snakeCase} from "case-anything"; import {Model, Sequelize} from "@sequelize/core"; import { - JsonTraversalState, - JsonTraversalFn, + evaluateJsonTraversalFn, JsonFieldOptions, JsonSchema, - evaluateJsonTraversalFn, + JsonTraversalFn, + JsonTraversalState, VisitMap } from "./schema"; @@ -141,37 +141,25 @@ export class Jsonthis { if (isNull(value)) return this.options.keepNulls ? null : undefined; if (Array.isArray(value)) return value.map(e => this.serialize(e, state, serializer, options) || null); - if (serializer) { - // Check for circular references + if (!serializer) { + const [result, trivial] = this.serializeTrivialValue(value); + if (trivial) return result; + } + + try { state.visited.dive(); + // Check for circular references if (state.visited.visit(value)) { if (this.options.circularReferenceSerializer) return this.options.circularReferenceSerializer(value, state); throw new CircularReferenceError(value, state); } - try { + if (serializer) return evaluateJsonTraversalFn(serializer, value, state, options); - } finally { - state.visited.arise(); - } - } - - const [result, trivial] = this.serializeTrivialValue(value); - if (trivial) return result; - - // Check for circular references - state.visited.dive(); - - if (state.visited.visit(value)) { - if (this.options.circularReferenceSerializer) - return this.options.circularReferenceSerializer(value, state); - throw new CircularReferenceError(value, state); - } - - try { - return this.toJson(value, options, state!); + else + return this.toJson(value, options, state!); } finally { state.visited.arise(); }