Code Monkey home page Code Monkey logo

enum-for's Introduction

enum-for Build Status

NPM

Tiny library to get all values/keys of a Typescript enum.

Install

  • With yarn: yarn add enum-for
  • With npm: npm install --save enum-for

API reference

import {getAllEnumKeys, getAllEnumValues, getAllEnumEntries} from 'enum-for'
  • getAllEnumKeys returns all keys of the enum object
  • getAllEnumValues returns all values of the enum object
  • getAllEnumEntries returns all entries ([key, value]) of the enum object

Source code

The source code of the project is very simple (3 LOCs). You can copy and use it directly in your project.

However, I recommend installing this package instead, because it is well tested.

exports.getAllEnumKeys = enumType => Object.keys(enumType).filter(key => isNaN(Number(key)))
exports.getAllEnumValues = enumType => exports.getAllEnumKeys(enumType).map(key => enumType[key])
exports.getAllEnumEntries = enumType => exports.getAllEnumKeys(enumType).map(key => [key, enumType[key]])

Example

import {getAllEnumKeys, getAllEnumValues, getAllEnumEntries} from 'enum-for'

enum MyEnum {
  foo = 0,
  bar = '1',
  '1foo' = 2,
  '2foo' = '3'
}

console.log(getAllEnumKeys(MyEnum))
console.log(getAllEnumValues(MyEnum))
console.log(getAllEnumEntries(MyEnum))
console.log(Object.keys(MyEnum))
console.log(Object.values(MyEnum))
console.log(Object.entries(MyEnum))

This script will print. Note that the results returned from Object.keys, Object.values are incorrect.

[ 'foo', 'bar', '1foo', '2foo' ]
[ 0, '1', 2, '3' ]
[ [ 'foo', 0 ], [ 'bar', '1' ], [ '1foo', 2 ], [ '2foo', '3' ] ]
[ '0', '2', 'foo', 'bar', '1foo', '2foo' ]
[ 'foo', '1foo', 0, '1', 2, '3' ]
[ [ '0', 'foo' ], [ '2', '1foo' ], [ 'foo', 0 ], [ 'bar', '1' ], [ '1foo', 2 ], [ '2foo', '3' ] ]

enum-for's People

Contributors

tranvansang avatar dependabot[bot] avatar adivated avatar

Watchers

James Cloos 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.