JSPM

ts-enum-helpers

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q8612F
  • License MIT

A small collection of utility methods to help parse typescript enums into objects, keys and values

Package Exports

  • ts-enum-helpers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-enum-helpers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-enum-helpers

A small collection of utility methods to help parse typescript enums into objects, keys and values.

Installation

Install using yarn or npm

yarn add ts-enum-helpers
npm install ts-enum-helpers --save

Methods

Given the following enums:

enum StringEnum {
    ONE = "one",
    TWO = "two",
    THREE = "three"
}

enum NumberEnum {
    ONE,
    TWO,
    THREE
}

parseEnum - convert an enum to an object

import {parseEnum} from "ts-enum-helpers"

const stringEnum = parseEnum(StringEnum);
const numericEnum = parseEnum(NumberEnum);

console.log(stringEnum)
console.log(numericEnum)

> {"ONE": "one", "TWO": "two", "THREE": "three"}
> {"ONE": 0, "TWO": 1, "THREE": 2}

getEnumNames - convert an enum to an array of its keys

import {getEnumNames} from "ts-enum-helpers"

const stringEnum = getEnumNames(StringEnum);
const numericEnum = getEnumNames(NumberEnum);

console.log(stringEnum)
console.log(numericEnum)

> ["ONE", "TWO", "THREE"]
> ["ONE", "TWO", "THREE"]

getEnumValues - convert an enum to an array of its values

import {getEnumValues} from "ts-enum-helpers"

const stringEnum = getEnumValues(StringEnum);
const numericEnum = getEnumValues(NumberEnum);

console.log(stringEnum)
console.log(numericEnum)

> ["one", "two", "three"]
> [0, 1, 2]

getEnumNamesAndValues - convert an enum to an array of ({name: string, value: T extends (number | string)})

import {getEnumNamesAndValues} from "ts-enum-helpers"

const stringEnum = getEnumNamesAndValues(StringEnum);
const numericEnum = getEnumNamesAndValues(NumberEnum);

console.log(stringEnum)
console.log(numericEnum)

> [
    {"name": "ONE", "value": "one"}, 
    {"name": "TWO", "value": "two"}, 
    {"name": "THREE", "value": "three"}
  ]

> [
    {"name": "ONE", "value": 0}, 
    {"name": "TWO", "value": 1}, 
    {"name": "THREE", "value": 2}
  ]