From f45c5b50732f175fd4569a69cdc1b41eb17a0989 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 17 Feb 2022 13:50:06 +0100 Subject: [PATCH] Rewrite code to better explain AminoTypes constructor --- packages/stargate/src/aminotypes.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/stargate/src/aminotypes.ts b/packages/stargate/src/aminotypes.ts index cf7a545147..adca645a8e 100644 --- a/packages/stargate/src/aminotypes.ts +++ b/packages/stargate/src/aminotypes.ts @@ -519,6 +519,10 @@ export interface AminoTypesOptions { readonly additions?: Record; } +function sameAminoType(a: AminoConverter, b: AminoConverter): boolean { + return a.aminoType === b.aminoType; +} + /** * A map from Stargate message types as used in the messages's `Any` type * to Amino types. @@ -527,12 +531,16 @@ export class AminoTypes { private readonly register: Record; public constructor({ prefix, additions = {} }: AminoTypesOptions) { + const defaultTypes = createDefaultTypes(prefix); const additionalAminoTypes = Object.values(additions); - const filteredDefaultTypes = Object.entries(createDefaultTypes(prefix)).reduce( + // `filteredDefaultTypes` contains all types of `defaultTypes` + // that are not included in the `additions`. Not included + // means not having the same Amino type identifier. + const filteredDefaultTypes = Object.entries(defaultTypes).reduce( (acc, [key, value]) => - additionalAminoTypes.find(({ aminoType }) => value.aminoType === aminoType) - ? acc - : { ...acc, [key]: value }, + additionalAminoTypes.find((addition) => sameAminoType(value, addition)) + ? acc /* Skip this defaultTypes entry */ + : { ...acc, [key]: value } /* Add this defaultTypes entry */, {}, ); this.register = { ...filteredDefaultTypes, ...additions };