From 810827d7c25ea7b644d134f3f1ce1e44db6f6c82 Mon Sep 17 00:00:00 2001 From: Norbert Csaba Herczeg Date: Tue, 8 Oct 2024 18:51:43 +0200 Subject: [PATCH] JNG-5966 enum ordinal helpers (#81) --- .../src/main/resources/model/enum.ts.hbs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/judo-ui-typescript-rest-api/src/main/resources/model/enum.ts.hbs b/judo-ui-typescript-rest-api/src/main/resources/model/enum.ts.hbs index dff6642..4470719 100644 --- a/judo-ui-typescript-rest-api/src/main/resources/model/enum.ts.hbs +++ b/judo-ui-typescript-rest-api/src/main/resources/model/enum.ts.hbs @@ -5,3 +5,31 @@ export enum {{ restParamName enumerationType }} { {{ member.name }} = '{{ member.name }}', {{/ each}} } + +type OrdinalOf{{ restParamName enumerationType }} = {{# each enumerationType.members as | member | }}{{ member.ordinal }}{{# unless @last }} | {{/ unless }}{{/ each }} + +export function get{{ restParamName enumerationType }}LiteralByOrdinal(ordinal: OrdinalOf{{ restParamName enumerationType }}): {{ restParamName enumerationType }} { + switch(ordinal) { + {{# each enumerationType.members as | member | }} + {{# unless @last }} + case {{ member.ordinal }}: + {{ else }} + default: + {{/ unless }} + return {{ restParamName enumerationType }}.{{ member.name }}; + {{/ each}} + } +} + +export function get{{ restParamName enumerationType }}OrdinalByLiteral(literal: {{ restParamName enumerationType }}): OrdinalOf{{ restParamName enumerationType }} { + switch(literal) { + {{# each enumerationType.members as | member | }} + {{# unless @last }} + case {{ restParamName enumerationType }}.{{ member.name }}: + {{ else }} + default: + {{/ unless }} + return {{ member.ordinal }}; + {{/ each}} + } +}