Refactored PublisherTarget (-> PlatformType)

This commit is contained in:
Kir_Antipov 2023-03-31 09:13:44 +00:00
parent 390914e813
commit 90564b0aa2
3 changed files with 90 additions and 17 deletions

View file

@ -0,0 +1,61 @@
import { Enum, EnumOptions } from "@/utils/enum";
/**
* Represents different platform types for mod distribution.
*
* @partial
*/
enum PlatformTypeValues {
/**
* Represents CurseForge.
*/
CURSEFORGE = "curseforge",
/**
* Represents Modrinth.
*/
MODRINTH = "modrinth",
/**
* Represents GitHub.
*/
GITHUB = "github",
}
/**
* Options for configuring the behavior of the `PlatformType` enum.
*
* @partial
*/
const PlatformTypeOptions: EnumOptions = {
/**
* The case should be ignored while parsing the platform type.
*/
ignoreCase: true,
/**
* Non-word characters should be ignored while parsing the platform type.
*/
ignoreNonWordCharacters: true,
/**
* Custom friendly names for keys that don't follow the general naming convention.
*/
names: [
["CURSEFORGE", "CurseForge"],
["GITHUB", "GitHub"],
],
};
/**
* Represents different platform types for mod distribution.
*/
export const PlatformType = Enum.create(
PlatformTypeValues,
PlatformTypeOptions,
);
/**
* Represents different platform types for mod distribution.
*/
export type PlatformType = Enum<typeof PlatformTypeValues>;

View file

@ -1,17 +0,0 @@
enum PublisherTarget {
CurseForge,
Modrinth,
GitHub,
}
namespace PublisherTarget {
export function getValues(): PublisherTarget[] {
return <PublisherTarget[]>Object.values(PublisherTarget).filter(x => !isNaN(+x));
}
export function toString(target: PublisherTarget): string {
return PublisherTarget[target] || target.toString();
}
}
export default PublisherTarget;

View file

@ -0,0 +1,29 @@
import { PlatformType } from "@/platforms/platform-type";
describe("PlatformType", () => {
describe("parse", () => {
test("parses all its own formatted values", () => {
for (const value of PlatformType.values()) {
expect(PlatformType.parse(PlatformType.format(value))).toBe(value);
}
});
test("parses all friendly names of its own values", () => {
for (const value of PlatformType.values()) {
expect(PlatformType.parse(PlatformType.friendlyNameOf(value))).toBe(value);
}
});
test("parses all its own formatted values in lowercase", () => {
for (const value of PlatformType.values()) {
expect(PlatformType.parse(PlatformType.format(value).toLowerCase())).toBe(value);
}
});
test("parses all its own formatted values in UPPERCASE", () => {
for (const value of PlatformType.values()) {
expect(PlatformType.parse(PlatformType.format(value).toUpperCase())).toBe(value);
}
});
});
});