Skip to content

Commit

Permalink
chore: rewrite enums to string literal type unions
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Nov 17, 2021
1 parent cea79d0 commit ab13845
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 145 deletions.
27 changes: 15 additions & 12 deletions src/badge/badge-kind.ts
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>;
11 changes: 3 additions & 8 deletions src/badge/ensure-badge-kind/ensure-badge-kind.ts
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")}`);
}
}
44 changes: 20 additions & 24 deletions src/badge/get-badges/get-badges.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {GetBadgesOptions} from "./get-badges-options";
import {GetBadgesResult} from "./get-badges-result";
import {BadgeKind} from "../badge-kind";
import {findCodeStyles} from "../../code-style/find-code-style/find-code-styles";
import {CONSTANT} from "../../constant/constant";
import {formatUrl} from "../../markdown/format-url/format-url";
Expand All @@ -11,9 +10,6 @@ import {getLicenseForLicenseName} from "../../license/get-license-for-license-na

/**
* Gets relevant badges based on the given options
*
* @param options
* @returns
*/
export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesResult> {
const result: GetBadgesResult = {};
Expand All @@ -25,8 +21,8 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
const encodedRepoUrl = repoUrl == null ? undefined : encodeURIComponent(repoUrl);

// Unless explicitly excluded, and if possible, generate a badge for the amount of downloads for the project
if (!excluded.has(BadgeKind.DOWNLOADS) && encodedName != null) {
result[BadgeKind.DOWNLOADS] = [
if (!excluded.has("downloads") && encodedName != null) {
result.downloads = [
formatUrl({
url: `https://npmcharts.com/compare/${encodedName}?minimal=true`,
inner: formatImage({
Expand All @@ -38,8 +34,8 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for the NPM version
if (!excluded.has(BadgeKind.NPM) && encodedName != null) {
result[BadgeKind.NPM] = [
if (!excluded.has("npm") && encodedName != null) {
result.npm = [
formatUrl({
url: `https://www.npmjs.com/package/${encodedName}`,
inner: formatImage({
Expand All @@ -51,8 +47,8 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for the package dependencies
if (!excluded.has(BadgeKind.DEPENDENCIES) && repoUrl != null && encodedRepoUrl != null) {
result[BadgeKind.DEPENDENCIES] = [
if (!excluded.has("dependencies") && repoUrl != null && encodedRepoUrl != null) {
result.dependencies = [
formatUrl({
url: `https://david-dm.org/${repoUrl}`,
inner: formatImage({
Expand All @@ -64,8 +60,8 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for the package dependencies
if (!excluded.has(BadgeKind.CONTRIBUTORS) && encodedName != null && repoUrl != null && encodedRepoUrl != null) {
result[BadgeKind.CONTRIBUTORS] = [
if (!excluded.has("contributors") && encodedName != null && repoUrl != null && encodedRepoUrl != null) {
result.contributors = [
formatUrl({
url: `https://github.com/${repoUrl}/graphs/contributors`,
inner: formatImage({
Expand All @@ -77,10 +73,10 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for the project code style(s).
if (!excluded.has(BadgeKind.CODE_STYLE)) {
if (!excluded.has("code_style")) {
const codeStyles = await findCodeStyles(options);
if (codeStyles.length > 0) {
result[BadgeKind.CODE_STYLE] = codeStyles.map(({kind, url, badgeUrl}) =>
result.code_style = codeStyles.map(({kind, url, badgeUrl}) =>
formatUrl({
url,
inner: formatImage({
Expand All @@ -93,11 +89,11 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for the project license
if (!excluded.has(BadgeKind.LICENSE)) {
if (!excluded.has("license")) {
const licenseName = await findLicense(options);
if (licenseName != null) {
const {badgeUrl, url} = getLicenseForLicenseName(licenseName);
result[BadgeKind.LICENSE] = [
result.license = [
formatUrl({
url,
inner: formatImage({
Expand All @@ -110,8 +106,8 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes
}

// Unless explicitly excluded, and if possible, generate a badge for supporting on Patreon
if (!excluded.has(BadgeKind.PATREON) && options.config.donate != null && options.config.donate.patreon != null && options.config.donate.patreon.userId != null) {
result[BadgeKind.PATREON] = [
if (!excluded.has("patreon") && options.config.donate != null && options.config.donate.patreon != null && options.config.donate.patreon.userId != null) {
result.patreon = [
formatUrl({
url: CONSTANT.patreonDonateUrl(options.config.donate.patreon.userId),
inner: formatImage({
Expand All @@ -124,12 +120,12 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes

// Unless explicitly excluded, and if possible, generate a badge for supporting on Open Collective
if (
!excluded.has(BadgeKind.OPEN_COLLECTIVE_DONATE) &&
!excluded.has("open_collective_donate") &&
options.config.donate != null &&
options.config.donate.openCollective != null &&
options.config.donate.openCollective.project != null
) {
result[BadgeKind.OPEN_COLLECTIVE_DONATE] = [
result.open_collective_donate = [
formatUrl({
url: CONSTANT.openCollectiveDonateUrl(options.config.donate.openCollective.project),
inner: formatImage({
Expand All @@ -142,12 +138,12 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes

// Unless explicitly excluded, and if possible, generate a badge for listing the amount of backers on Open Collective
if (
!excluded.has(BadgeKind.OPEN_COLLECTIVE_BACKERS) &&
!excluded.has("open_collective_backers") &&
options.config.donate != null &&
options.config.donate.openCollective != null &&
options.config.donate.openCollective.project != null
) {
result[BadgeKind.OPEN_COLLECTIVE_BACKERS] = [
result.open_collective_backers = [
formatUrl({
url: CONSTANT.openCollectiveContributorsUrl(options.config.donate.openCollective.project),
inner: formatImage({
Expand All @@ -160,12 +156,12 @@ export async function getBadges(options: GetBadgesOptions): Promise<GetBadgesRes

// Unless explicitly excluded, and if possible, generate a badge for listing the amount of sponsors on Open Collective
if (
!excluded.has(BadgeKind.OPEN_COLLECTIVE_SPONSORS) &&
!excluded.has("open_collective_sponsors") &&
options.config.donate != null &&
options.config.donate.openCollective != null &&
options.config.donate.openCollective.project != null
) {
result[BadgeKind.OPEN_COLLECTIVE_SPONSORS] = [
result.open_collective_sponsors = [
formatUrl({
url: CONSTANT.openCollectiveContributorsUrl(options.config.donate.openCollective.project),
inner: formatImage({
Expand Down
Loading

0 comments on commit ab13845

Please sign in to comment.