Giter Site home page Giter Site logo

enum-values's Introduction

enum-values

Library for getting the names and values of typescript enum

Installation

npm install enum-values --save

Example in TypeScript

  import EnumValues from 'enum-values';

  // Suppose we have an numeric and a string valued enum
  enum NumericEnum {
    VALUE1,
    VALUE2,
    VALUE3
  }

  enum StringEnum {
    VALUE4 = 'V4',
    VALUE5 = 'V5'
  }

  // names will be equal to: ['VALUE1', 'VALUE2', 'VALUE3']
  var names1 = EnumValues.getNames(NumericEnum);

  // names will be equal to: ['VALUE4', 'VALUE5']
  var names2 = EnumValues.getNames(StringEnum);

  // values will be equal to: [0, 1, 2]
  var values1 = EnumValues.getValues(NumericEnum);

  // values will be equal to: ['V4', 'V5']
  var values2 = EnumValues.getValues(StringEnum);

  // name will be 'VALUE4'
  var nameFromValue1 = EnumValues.getNameFromValue(StringEnum, 'V4')

  // name will be null
  var nameFromValue2 = EnumValues.getNameFromValue(StringEnum, 'AB')

  // name will be 'VALUE1'
  var nameFromValue3 = EnumValues.getNameFromValue(NumericEnum, 0);
  
  // namesAndValues will be equal to:
  // [
  //  { name: 'VALUE1', value: 0 },
  //  { name: 'VALUE2', value: 1 },
  //  { name: 'VALUE3', value: 2 }
  // ]
  var namesAndValues1 = EnumValues.getNamesAndValues(NumericEnum);

  // namesAndValues will be equal to:
  // [
  //  { name: 'VALUE4', value: 'V4' },
  //  { name: 'VALUE5', value: 'V5' }
  // ]
  var namesAndValues2 = EnumValues.getNamesAndValues(StringEnum);

enum-values's People

Contributors

slavik57 avatar sbosell avatar zorgatone avatar

Watchers

Kidoo Han avatar

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.