Socket
Book a DemoInstallSign in
Socket

ts-enum-helpers

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-enum-helpers

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

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

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}
  ]

Keywords

typescript

FAQs

Package last updated on 08 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.