Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change getEnumValues and GetEnumValueFromName return type #45

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- `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
Loading