-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rewrite enums to string literal type unions
- Loading branch information
Showing
9 changed files
with
92 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
export enum BadgeKind { | ||
DOWNLOADS = "downloads", | ||
DEPENDENCIES = "dependencies", | ||
NPM = "npm", | ||
CONTRIBUTORS = "contributors", | ||
LICENSE = "license", | ||
PATREON = "patreon", | ||
OPEN_COLLECTIVE_DONATE = "open_collective_donate", | ||
OPEN_COLLECTIVE_BACKERS = "open_collective_backers", | ||
OPEN_COLLECTIVE_SPONSORS = "open_collective_sponsors", | ||
CODE_STYLE = "code_style" | ||
} | ||
import {ElementOf} from "helpertypes"; | ||
|
||
export const BADGE_KINDS = [ | ||
"downloads", | ||
"dependencies", | ||
"npm", | ||
"contributors", | ||
"license", | ||
"patreon", | ||
"open_collective_donate", | ||
"open_collective_backers", | ||
"open_collective_sponsors", | ||
"code_style" | ||
] as const; | ||
export type BadgeKind = ElementOf<typeof BADGE_KINDS>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import {listFormat} from "../../util/list-format/list-format"; | ||
import {getValuesForEnum} from "../../util/enum/get-values-for-enum"; | ||
import {BadgeKind} from "../badge-kind"; | ||
|
||
const BADGE_ENUM_VALUES = getValuesForEnum(BadgeKind); | ||
import {BadgeKind, BADGE_KINDS} from "../badge-kind"; | ||
|
||
/** | ||
* Ensures that the given input is a proper BadgeKind | ||
* | ||
* @param badgeKind | ||
*/ | ||
export function ensureBadgeKind(badgeKind: BadgeKind | string): BadgeKind { | ||
if (typeof badgeKind !== "string") return badgeKind; | ||
if (BADGE_ENUM_VALUES.some(key => key === badgeKind)) { | ||
if (BADGE_KINDS.some(key => key === badgeKind)) { | ||
return badgeKind as BadgeKind; | ||
} else { | ||
throw new TypeError(`Could not parse string: '${badgeKind}' as a BadgeKind. Possible values: ${listFormat(BADGE_ENUM_VALUES, "and")}`); | ||
throw new TypeError(`Could not parse string: '${badgeKind}' as a BadgeKind. Possible values: ${listFormat(BADGE_KINDS, "and")}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.