From dec78265a3cfae89fa02d9cbbb728bef5eed90cb Mon Sep 17 00:00:00 2001 From: rowan Date: Sat, 17 May 2025 21:54:01 -0500 Subject: [PATCH] skip serializing option --- src/options.ts | 56 ++++++++++++++++++++++++++++++++++++++----------- src/ser/impl.ts | 4 ++++ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/options.ts b/src/options.ts index 370c09c..5857752 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,7 +1,7 @@ import { CaseConvention, convertCase } from './case' import { Deserializer } from './de' import { Serializer } from './ser' -import { isFunction, isNumber, isString, Morphism, Nullable } from './utils' +import { isFunction, isNumber, isString } from './utils' export interface RenameOptions { @@ -47,7 +47,7 @@ export interface PropertyOptions { rename?: RenameOptions | string //serializeWith?: CustomSerializer //deserializeWith: CustomDeserializer - skip?: SkipOptions | boolean + skip?: SkipOptions | ConditionalSkipOptions | boolean } export const Stage = Object.freeze({ @@ -133,23 +133,55 @@ export class SerdeOptions { } } - getCustomImpl(property: string, stage: Stage) { + //getCustomImpl(property: string, stage: Stage) { + // const options = this.properties.get(property) + // if (options != null) { + // if (stage === Stage.Serialize && isFunction(options.serializeWith)) { + // return options.serializeWith + // } else if (stage === Stage.Deserialize && isFunction(options.deserializeWith)) { + // return options.deserializeWith + // } + // } + //} + + //getSerializer(property: string): Nullable { + // return this.getCustomImpl(property, Stage.Serialize) as CustomSerializer + //} + + //getDeserializer(property: string): Nullable { + // return this.getCustomImpl(property, Stage.Deserialize) as CustomDeserializer + //} + + private isConditionalSkip(skip: any): skip is ConditionalSkipOptions { + return 'if' in skip && isFunction(skip.if) + } + + shouldSkip(property: string, value: any, stage: Stage): boolean { const options = this.properties.get(property) - if (options != null) { - if (stage === Stage.Serialize && isFunction(options.serializeWith)) { - return options.serializeWith - } else if (stage === Stage.Deserialize && isFunction(options.deserializeWith)) { - return options.deserializeWith + if (options != null && options.skip != null) { + if (typeof options.skip === 'boolean') { + return options.skip + } else if (this.isConditionalSkip(options.skip)) { + return options.skip.if(value) + } else if (stage === Stage.Serialize && typeof options.skip.serializing === 'boolean') { + return options.skip.serializing + } else if (stage === Stage.Serialize && this.isConditionalSkip(options.skip.serializing)) { + return options.skip.serializing.if(value) + } else if (stage === Stage.Deserialize && typeof options.skip.deserializing === 'boolean') { + return options.skip.deserializing + } else if (stage === Stage.Deserialize && this.isConditionalSkip(options.skip.deserializing)) { + return options.skip.deserializing.if(value) } } + return false } - getSerializer(property: string): Nullable { - return this.getCustomImpl(property, Stage.Serialize) as CustomSerializer + shouldSkipSerializing(property: string, value: any): boolean { + return this.shouldSkip(property, value, Stage.Serialize) } - getDeserializer(property: string): Nullable { - return this.getCustomImpl(property, Stage.Deserialize) as CustomDeserializer + shouldSkipDeserializing(property: string, value: any): boolean { + return this.shouldSkip(property, value, Stage.Deserialize) } } diff --git a/src/ser/impl.ts b/src/ser/impl.ts index e3d4b94..cbac447 100644 --- a/src/ser/impl.ts +++ b/src/ser/impl.ts @@ -11,6 +11,10 @@ function serializeEntries