Skip to content

Commit 6726b96

Browse files
authored
refactor: replace TypeScript enums with erasable const objects (#1669)
1 parent 03685f8 commit 6726b96

11 files changed

Lines changed: 53 additions & 47 deletions

File tree

lib/subject/date.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ test.each([
5454
SubjectType.Book,
5555
DATE.parse('2019-11-28'),
5656
],
57-
])('extractDate(%s) = %s', (w: Wiki, t: SubjectType, date: DATE) => {
57+
])('extractDate(%s) = %s', (w: Wiki, t: number, date: DATE) => {
5858
expect(extractDate(w, t, 0)).toEqual(date);
5959
});

lib/subject/date.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import type { Wiki } from '@bgm38/wiki';
33
import { DATE } from '@app/lib/utils/date.ts';
44
import { getSubjectPlatformSortKeys } from '@app/vendor';
55

6-
import type { SubjectType } from './type';
7-
8-
export function extractDate(w: Wiki, typeID: SubjectType, platform: number): DATE {
6+
export function extractDate(w: Wiki, typeID: number, platform: number): DATE {
97
const keys = getSubjectPlatformSortKeys(typeID, platform);
108

119
const values = keys

lib/subject/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export async function create({
212212
rate10: 0,
213213
} satisfies typeof schema.chiiSubjectFields.$inferInsert);
214214

215-
if ([SubjectType.Anime, SubjectType.Real].includes(typeID) && episodes) {
215+
if ((typeID === SubjectType.Anime || typeID === SubjectType.Real) && episodes) {
216216
// avoid create too many episodes, 50 is enough.
217217
episodes = Math.min(episodes, 50);
218218

lib/subject/infobox.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { UnreachableError } from '@app/lib/error.ts';
12
import { getInfoboxValue } from '@app/lib/infobox.ts';
23
import type * as res from '@app/lib/types/res.ts';
34

45
import { SubjectType } from './type';
56

6-
function getDisplayFields(type: SubjectType): string[][] {
7+
function getDisplayFields(type: number): string[][] {
78
const airdate = ['放送开始', '上映日', '上映年度', '发售日'];
89
switch (type) {
910
case SubjectType.Book: {
@@ -21,10 +22,13 @@ function getDisplayFields(type: SubjectType): string[][] {
2122
case SubjectType.Real: {
2223
return [airdate, ['开始'], ['导演'], ['编剧'], ['主演']];
2324
}
25+
default: {
26+
throw new UnreachableError(`unexpected subject type: ${type}`);
27+
}
2428
}
2529
}
2630

27-
export function getInfoboxSummary(infobox: res.IInfoboxItem[], type: SubjectType, eps = 0): string {
31+
export function getInfoboxSummary(infobox: res.IInfoboxItem[], type: number, eps = 0): string {
2832
const displayFields = getDisplayFields(type);
2933
const list: string[] = [];
3034
if (eps > 0) {

lib/subject/type.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// eslint-disable-next-line erasable-syntax-only/enums
2-
export enum SubjectType {
3-
Book = 1, // 书籍
4-
Anime = 2, // 动画
5-
Music = 3, // 音乐
6-
Game = 4, // 游戏
7-
Real = 6, // 三次元
8-
}
1+
import { UnreachableError } from '@app/lib/error.ts';
2+
3+
export const SubjectType = {
4+
Book: 1, // 书籍
5+
Anime: 2, // 动画
6+
Music: 3, // 音乐
7+
Game: 4, // 游戏
8+
Real: 6, // 三次元
9+
} as const;
910
export const SubjectTypeValues = new Set([1, 2, 3, 4, 6]);
1011

1112
export const EpisodeType = Object.freeze({
@@ -22,18 +23,17 @@ export const EpisodeType = Object.freeze({
2223
});
2324
export type EpisodeType = (typeof EpisodeType)[keyof typeof EpisodeType];
2425

25-
// eslint-disable-next-line erasable-syntax-only/enums
26-
export enum CollectionType {
27-
Wish = 1,
28-
Collect = 2,
29-
Doing = 3,
30-
OnHold = 4,
31-
Dropped = 5,
32-
}
26+
export const CollectionType = {
27+
Wish: 1,
28+
Collect: 2,
29+
Doing: 3,
30+
OnHold: 4,
31+
Dropped: 5,
32+
} as const;
3333
export const CollectionTypeValues = new Set([1, 2, 3, 4, 5]);
3434
export const CollectionTypeProfileValues = new Set([1, 2]);
3535

36-
export function getCollectionTypeField(type: CollectionType) {
36+
export function getCollectionTypeField(type: number) {
3737
switch (type) {
3838
case CollectionType.Wish: {
3939
return 'wish';
@@ -50,6 +50,9 @@ export function getCollectionTypeField(type: CollectionType) {
5050
case CollectionType.Dropped: {
5151
return 'dropped';
5252
}
53+
default: {
54+
throw new UnreachableError(`unexpected collection type: ${type}`);
55+
}
5356
}
5457
}
5558

@@ -85,7 +88,7 @@ export type SubjectSort = (typeof SubjectSort)[keyof typeof SubjectSort];
8588
export type SubjectTagsCategory = 'meta' | 'subject';
8689

8790
export interface SubjectFilter {
88-
type: SubjectType;
91+
type: number;
8992
nsfw: boolean;
9093
cat?: number;
9194
series?: boolean;

lib/subject/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { decr, incr, op, type orm, schema, type Txn } from '@app/drizzle';
22

33
import { markEpisodesAsWatched } from './ep';
4-
import { type CollectionType, SubjectType } from './type';
4+
import { SubjectType } from './type';
55
import { getCollectionTypeField } from './type';
66

77
/** 更新条目收藏计数,需要在事务中执行 */
88
export async function updateSubjectCollectionCounts(
99
t: Txn,
1010
subjectID: number,
11-
newType: CollectionType,
12-
oldType?: CollectionType,
11+
newType: number,
12+
oldType?: number,
1313
) {
1414
if (oldType && oldType === newType) {
1515
return;
@@ -89,7 +89,7 @@ export async function completeSubjectProgress(
8989
if (subject.volumes > 0) {
9090
interest.volStatus = subject.volumes;
9191
}
92-
if ([SubjectType.Anime, SubjectType.Real].includes(subject.typeID)) {
92+
if (subject.typeID === SubjectType.Anime || subject.typeID === SubjectType.Real) {
9393
const episodes = await t
9494
.select({ id: schema.chiiEpisodes.id })
9595
.from(schema.chiiEpisodes)

lib/timeline/writer.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as lo from 'lodash-es';
22

33
import { db, op, schema } from '@app/drizzle';
4-
import { BadRequestError } from '@app/lib/error.ts';
4+
import { BadRequestError, UnreachableError } from '@app/lib/error.ts';
55
import { producer } from '@app/lib/kafka';
66
import { CollectionType, EpisodeCollectionStatus, SubjectType } from '@app/lib/subject/type';
77
import { decode } from '@app/lib/utils';
@@ -30,7 +30,7 @@ export interface TimelineMessage {
3030
uid: number;
3131
subject: {
3232
id: number;
33-
type: SubjectType;
33+
type: number;
3434
};
3535
createdAt: number;
3636
source: number;
@@ -39,11 +39,11 @@ export interface TimelineMessage {
3939
uid: number;
4040
subject: {
4141
id: number;
42-
type: SubjectType;
42+
type: number;
4343
};
4444
collect: {
4545
id: number;
46-
type: CollectionType;
46+
type: number;
4747
rate: number;
4848
comment: string;
4949
};
@@ -54,7 +54,7 @@ export interface TimelineMessage {
5454
uid: number;
5555
subject: {
5656
id: number;
57-
type: SubjectType;
57+
type: number;
5858
};
5959
episode: {
6060
id: number;
@@ -67,7 +67,7 @@ export interface TimelineMessage {
6767
uid: number;
6868
subject: {
6969
id: number;
70-
type: SubjectType;
70+
type: number;
7171
eps: number;
7272
volumes: number;
7373
};
@@ -525,7 +525,7 @@ export const TimelineWriter: TimelineDatabaseWriter = {
525525
},
526526
};
527527

528-
function switchSubjectType(ctype: CollectionType, stype: SubjectType): number {
528+
function switchSubjectType(ctype: number, stype: number): number {
529529
switch (stype) {
530530
case SubjectType.Book: {
531531
const source = {
@@ -535,7 +535,7 @@ function switchSubjectType(ctype: CollectionType, stype: SubjectType): number {
535535
[CollectionType.OnHold]: 13,
536536
[CollectionType.Dropped]: 14,
537537
};
538-
return source[ctype];
538+
return source[ctype as keyof typeof source];
539539
}
540540
case SubjectType.Anime:
541541
case SubjectType.Real: {
@@ -546,7 +546,7 @@ function switchSubjectType(ctype: CollectionType, stype: SubjectType): number {
546546
[CollectionType.OnHold]: 13,
547547
[CollectionType.Dropped]: 14,
548548
};
549-
return source[ctype];
549+
return source[ctype as keyof typeof source];
550550
}
551551
case SubjectType.Music: {
552552
const source = {
@@ -556,7 +556,7 @@ function switchSubjectType(ctype: CollectionType, stype: SubjectType): number {
556556
[CollectionType.OnHold]: 13,
557557
[CollectionType.Dropped]: 14,
558558
};
559-
return source[ctype];
559+
return source[ctype as keyof typeof source];
560560
}
561561
case SubjectType.Game: {
562562
const source = {
@@ -566,7 +566,10 @@ function switchSubjectType(ctype: CollectionType, stype: SubjectType): number {
566566
[CollectionType.OnHold]: 13,
567567
[CollectionType.Dropped]: 14,
568568
};
569-
return source[ctype];
569+
return source[ctype as keyof typeof source];
570+
}
571+
default: {
572+
throw new UnreachableError(`unexpected subject type: ${stype}`);
570573
}
571574
}
572575
}

lib/trending/cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { SubjectType } from '@app/lib/subject/type.ts';
21
import type { TrendingPeriod } from '@app/lib/trending/type.ts';
32

4-
export function getTrendingSubjectKey(type: SubjectType, period: TrendingPeriod) {
3+
export function getTrendingSubjectKey(type: number, period: TrendingPeriod) {
54
return `trending:subjects:${type}:${period}`;
65
}
76

lib/trending/subject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getTrendingDateline, TrendingPeriod } from '@app/lib/trending/type.ts';
88
import { getTrendingSubjectKey } from './cache';
99

1010
export async function updateTrendingSubjects(
11-
subjectType: SubjectType,
11+
subjectType: number,
1212
period = TrendingPeriod.Month,
1313
flush = false,
1414
) {

lib/user/stats.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ export async function countUserSubjectCollection(
146146
[SubjectType.Real]: Object.assign({}, empty),
147147
};
148148
for (const d of data) {
149-
const type = d.ctype as CollectionType;
150-
const stype = d.stype as SubjectType;
151-
stats[stype][type] = d.count;
149+
const type = d.ctype;
150+
const stype = d.stype;
151+
stats[stype as keyof typeof stats][type as keyof typeof empty] = d.count;
152152
}
153153
await redis.setex(key, 3600, JSON.stringify(stats));
154154
return stats;

0 commit comments

Comments
 (0)