Code Monkey home page Code Monkey logo

typescriptenumgenerator's Introduction

Typescript Enum Generator

TypescriptEnumGenerator is a package that utilizes the dotnet ISourceGenerator to provide enum types and helper methods for enums. It helps to reduce the overhead of modifying enums throughout your front and back end by generating Typescript types and classes. This assists in maintaining uniformity between C# and TypeScript.

Features

  • Helps maintain uniformity between C# and TypeScript enum definitions.
  • Provides a clean mechanism for utilizing enums for front end dropdowns, while providing compatibility for most component libraries.
  • Integrates into the [Display] attribute and respects usage of [Display(Name="")

Install

dotnet add package TypescriptEnumGenerator --version 0.2.1
Install-Package TypescriptEnumGenerator -Version 0.2.1

Example

Given the following input code

[TypescriptEnum] //Decorate your enum with this little badger 
public enum TestEnum
{
    Active,
    Closed,
    [Display(Name = "Something else")]
    SomethingElse = 13,
    [Display(Name = "Hello it's magic")]
    HelloItsMagic
}

And the system will generate the following typescript file

import DropDownOption from './DropDownOption'
export enum TestEnum {
    Active = 0,
    Closed = 1,
    SomethingElse = 13,
    HelloItsMagic = 14,
}

export function getTestEnumFromInt(value: number): TestEnum {
    switch (value) {

        case 0: return TestEnum.Active;
        case 1: return TestEnum.Closed;
        case 13: return TestEnum.SomethingElse;
        case 14: return TestEnum.HelloItsMagic;

        default: throw new Error(`Unknown TestEnum value: ${value}`);
    }
}


export function getTestEnumFromString(value: string): TestEnum {
    switch (value.toLowerCase()) {

        case "active": return TestEnum.Active;
        case "closed": return TestEnum.Closed;
        case "somethingelse": return TestEnum.SomethingElse;
        case "something else": return TestEnum.SomethingElse;
        case "helloitsmagic": return TestEnum.HelloItsMagic;
        case "hello it's magic": return TestEnum.HelloItsMagic;

        default: throw new Error(`Unknown TestEnum value: ${value}`);
    }
}

export const TestEnumDropDownOptions: DropDownOption[] = [
    new DropDownOption(0, "Active"),
    new DropDownOption(1, "Closed"),
    new DropDownOption(13, "Something else"),
    new DropDownOption(14, "Hello it's magic"),
];

The generator will also generate a single DropDownOption.ts file that is utilized for the DropDownOption class, this is designed to be used with Select and DropDown components provided by most component libraries and the vanilla HTML specification.

export default class DropdownOption {
    id: number;
    label: string;

    constructor(id: number, label: string) {
        this.id = id;
        this.label = label;
    }
}

typescriptenumgenerator's People

Contributors

sam-beynon avatar

Stargazers

Benji Peck avatar Benji avatar

Watchers

 avatar

typescriptenumgenerator's Issues

Allow utilizing a pre-existing enum (such as from nswag)

Add functionality to allow a plug and play implementation with nswag.

  1. Add an attribute that allows users to define an import for the top generated .ts file.
  2. Add an option to the main attribute that prevents the generation of the enum, but does not affect generation of all the methods.

Convert GetDropDownOptions method into a single static instance

Store the array within a variable, so that after first invocation it never needs processing after the initial variable load.

export const dropDownOptions: DropDownOption[] = [
    new DropDownOption(0, "Active"),
    new DropDownOption(1, "Closed"),
    new DropDownOption(13, "Something else"),
    new DropDownOption(14, "Hello it's magic"),
];

.NET6 support

Code does not generate when your project targets .net 6.0

Add [Display] compatibility

Add the ability to pull the value utilized within the [Display] attribute through instead of the standard string for the GetDropDownOptions methods.

[TypescriptEnum]
public enum TestEnum
{
    Active,
    Closed,
    [Display(Name = "Something else")]
    SomethingElse,
    HelloItsMagic
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.