Skip to content

Commit

Permalink
Change getEnumValues and GetEnumValueFromName return type (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
manni497 authored Oct 10, 2024
1 parent f4217d5 commit 6d168d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PKG PR New
on:
workflow_dispatch:
pull_request:
types: [opened, synchronize, ready_for_review]
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20

- run: yarn --frozen-lockfile

# append the git commit to the package.json version.
# We do this because some cache mechanisms (like nextjs) don't work well with the same version and ignore the changes
# until you manually delete the cache
- run: jq '.version = .version + "-" + env.GITHUB_SHA' package.json > package.json.tmp && mv package.json.tmp package.json

- run: npx pkg-pr-new publish
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `pkg.pr.new` workflow

### Changed

- :boom: `getEnumValues` and `getEnumValueFromName` return type from `string | number` to enum type

## [0.7.0] - 2024-10-02

### dependabot: \#42 Bump rollup from 3.21.4 to 3.29.5
Expand Down
10 changes: 6 additions & 4 deletions src/lib/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export function getEnumNameFromValue<T>(enumVariable: StandardEnum<T>, enumValue
* @param enumName The name of the enum for which you want to get the value
* @returns A string containing the value of the enum
*/
export function getEnumValueFromName<T>(enumVariable: StandardEnum<T>, enumName: string): number | string {
export function getEnumValueFromName<T>(enumVariable: StandardEnum<T>, enumName: string): T {
const value = Object.values(enumVariable)[Object.keys(enumVariable).findIndex((x) => x === enumName)] as string;
return isEnumString(enumVariable) ? value : Number.parseInt(value);
return (isEnumString(enumVariable) ? value : Number.parseInt(value)) as T;
}

/**
Expand All @@ -44,11 +44,13 @@ export function getEnumNames<T>(enumVariable: StandardEnum<T>) {
* @param enumVariable The enum for which you want to get the values
* @returns A string or number array containing the values of the enum
*/
export function getEnumValues<T>(enumVariable: StandardEnum<T>) {
export function getEnumValues<T>(enumVariable: StandardEnum<T>): T[] {
const keys = Object.keys(enumVariable);

// If enum is with values integer, object.keys returns a list of [values, names].
return isEnumString(enumVariable) ? Object.values(enumVariable) : keys.slice(0, keys.length / 2).map((value) => Number.parseInt(value));
return (
isEnumString(enumVariable) ? Object.values(enumVariable) : keys.slice(0, keys.length / 2).map((value) => Number.parseInt(value))
) as T[];
}

/**
Expand Down

0 comments on commit 6d168d5

Please sign in to comment.