Skip to content

Commit 08875c5

Browse files
committed
chore(es2023): fix lib to es2023 and handle low-risk syntax migration
1 parent f10b709 commit 08875c5

11 files changed

Lines changed: 32 additions & 33 deletions

File tree

src/api/kun.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ function extractKunTags(tags?: GalgameDetailTag[]): string[] {
137137
const filterLevel = useStore.getState().spoilerLevel;
138138

139139
return tags
140-
.slice()
141-
.sort((a, b) => (b.galgameCount || 0) - (a.galgameCount || 0))
140+
.toSorted((a, b) => (b.galgameCount || 0) - (a.galgameCount || 0))
142141
.filter((tag) => (tag.spoilerLevel ?? 0) <= filterLevel)
143142
.map((tag) => tag.name?.trim())
144143
.filter((name): name is string => Boolean(name));

src/api/vndb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function transformVndbData(
156156
const filtered_tags = (
157157
VNDBdata.tags as { rating: number; name: string; spoiler: number }[]
158158
)
159-
.sort((a, b) => b.rating - a.rating)
159+
.toSorted((a, b) => b.rating - a.rating)
160160
.filter(({ spoiler }) => spoiler <= filterLevel)
161161
.map(({ name }) => name);
162162

src/hooks/common/useVirtualCollections.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ function generateVirtualCategories<T>(
5656
const keyArray = Array.isArray(keys) ? keys : [keys];
5757

5858
for (const key of keyArray) {
59-
categoryMap.set(key, (categoryMap.get(key) || 0) + 1);
59+
categoryMap.set(key, (categoryMap.get(key) ?? 0) + 1);
6060
}
6161
}
6262

6363
// 转换为数组并排序
6464
let entries = Array.from(categoryMap.entries());
6565
if (config.sortResults) {
66-
entries = entries.sort(config.sortResults);
66+
entries = entries.toSorted(config.sortResults);
6767
}
6868

6969
// 映射为Category对象

src/pages/Detail/DetailPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ const TabPanel = (props: TabPanelProps) => {
7474
* @returns {JSX.Element} 游戏详情页面
7575
*/
7676
export const Detail: React.FC = () => {
77-
const id = Number(useLocation().pathname.split("/").pop());
77+
const location = useLocation();
78+
const id = Number(location.pathname.split("/").at(-1));
7879
const { t } = useTranslation();
7980
const navigate = useNavigate();
8081
const {
@@ -157,7 +158,6 @@ export const Detail: React.FC = () => {
157158
}, [selectedGame, t, handleDeveloperClick]);
158159

159160
const activePage = useActivePage();
160-
const location = useLocation();
161161
const title = selectedGame
162162
? getGameDisplayName(selectedGame)
163163
: t("pages.Detail.loading");

src/pages/Detail/GameInfoEdit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export const GameInfoEdit: React.FC<GameInfoEditProps> = ({
271271
) {
272272
// 退格键删除最后一个标签
273273
e.preventDefault();
274-
setAliases(aliases.slice(0, -1));
274+
setAliases(aliases.toSpliced(-1));
275275
}
276276
};
277277

@@ -297,7 +297,7 @@ export const GameInfoEdit: React.FC<GameInfoEditProps> = ({
297297
} else if (e.key === "Backspace" && tagInput === "" && tags.length > 0) {
298298
// 退格键删除最后一个标签
299299
e.preventDefault();
300-
setTags(tags.slice(0, -1));
300+
setTags(tags.toSpliced(-1));
301301
}
302302
};
303303

src/pages/Detail/InfoBox.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export const InfoBox: React.FC<InfoBoxProps> = ({ gameID }: InfoBoxProps) => {
191191
const dateStr = `${year}-${month}-${day}`;
192192
result.push({
193193
date: dateStr,
194-
playtime: datePlaytimeMap.get(dateStr) || 0,
194+
playtime: datePlaytimeMap.get(dateStr) ?? 0,
195195
});
196196
}
197197
} else if (timeRange === "MONTH") {
@@ -205,15 +205,15 @@ export const InfoBox: React.FC<InfoBoxProps> = ({ gameID }: InfoBoxProps) => {
205205
const dateStr = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
206206
result.push({
207207
date: dateStr,
208-
playtime: datePlaytimeMap.get(dateStr) || 0,
208+
playtime: datePlaytimeMap.get(dateStr) ?? 0,
209209
});
210210
}
211211
} else if (timeRange === "1Y") {
212212
// 1年:按月聚合
213213
const monthlyMap = new Map<string, number>();
214214
for (const [dateStr, playtime] of datePlaytimeMap) {
215215
const monthKey = dateStr.substring(0, 7); // YYYY-MM
216-
monthlyMap.set(monthKey, (monthlyMap.get(monthKey) || 0) + playtime);
216+
monthlyMap.set(monthKey, (monthlyMap.get(monthKey) ?? 0) + playtime);
217217
}
218218

219219
// 生成过去12个月(修复跨年问题)
@@ -224,35 +224,35 @@ export const InfoBox: React.FC<InfoBoxProps> = ({ gameID }: InfoBoxProps) => {
224224
const monthKey = `${year}-${month}`;
225225
result.push({
226226
date: monthKey,
227-
playtime: monthlyMap.get(monthKey) || 0,
227+
playtime: monthlyMap.get(monthKey) ?? 0,
228228
});
229229
}
230230
} else if (timeRange === "ALL") {
231231
// 全部:判断数据量决定是否按月聚合
232-
const allDates = Array.from(datePlaytimeMap.keys()).sort();
232+
const allDates = Array.from(datePlaytimeMap.keys()).toSorted();
233233

234234
if (allDates.length > 60) {
235235
// 数据点较多,按月聚合
236236
const monthlyMap = new Map<string, number>();
237237
for (const [dateStr, playtime] of datePlaytimeMap) {
238238
const monthKey = dateStr.substring(0, 7); // YYYY-MM
239-
monthlyMap.set(monthKey, (monthlyMap.get(monthKey) || 0) + playtime);
239+
monthlyMap.set(monthKey, (monthlyMap.get(monthKey) ?? 0) + playtime);
240240
}
241241

242242
// 按月排序输出
243-
const sortedMonths = Array.from(monthlyMap.keys()).sort();
243+
const sortedMonths = Array.from(monthlyMap.keys()).toSorted();
244244
for (const monthKey of sortedMonths) {
245245
result.push({
246246
date: monthKey,
247-
playtime: monthlyMap.get(monthKey) || 0,
247+
playtime: monthlyMap.get(monthKey) ?? 0,
248248
});
249249
}
250250
} else {
251251
// 数据点较少,按天显示
252252
for (const dateStr of allDates) {
253253
result.push({
254254
date: dateStr,
255-
playtime: datePlaytimeMap.get(dateStr) || 0,
255+
playtime: datePlaytimeMap.get(dateStr) ?? 0,
256256
});
257257
}
258258
}

src/pages/HomePage.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,19 @@ async function getGameActivities(games: GameData[]): Promise<{
181181
}
182182

183183
// 合并所有动态,按时间排序
184-
const allActivities = [...playItems, ...addItems].sort(
184+
const allActivities = [...playItems, ...addItems].toSorted(
185185
(a, b) => b.time - a.time,
186186
);
187187

188188
// 排序最近游玩和最近添加
189-
sessions.sort((a, b) => b.end_time - a.end_time);
190-
added.sort((a, b) => {
191-
if (a.time && b.time) return b.time.getTime() - a.time.getTime();
192-
return 0;
193-
});
189+
const sortedSessions = sessions.toSorted((a, b) => b.end_time - a.end_time);
190+
const sortedAdded = added.toSorted(
191+
(a, b) => b.time.getTime() - a.time.getTime(),
192+
);
194193

195194
return {
196-
sessions: sessions.slice(0, 6),
197-
added: added.slice(0, 6),
195+
sessions: sortedSessions.slice(0, 6),
196+
added: sortedAdded.slice(0, 6),
198197
activities: allActivities.slice(0, 10),
199198
};
200199
}

src/pages/Settings/SettingsPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ export const Settings: React.FC = () => {
225225
(entries) => {
226226
const visibleEntry = entries
227227
.filter((entry) => entry.isIntersecting)
228-
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
228+
.toSorted((a, b) => b.intersectionRatio - a.intersectionRatio)
229+
.at(0);
229230

230231
if (visibleEntry) {
231232
setActiveSectionId(visibleEntry.target.id);

src/utils/enhancedSearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export function getSearchSuggestions(
329329

330330
// 按优先级排序并去重
331331
const sortedSuggestions = prioritySuggestions
332-
.sort((a, b) => b.priority - a.priority)
332+
.toSorted((a, b) => b.priority - a.priority)
333333
.map((s) => s.name)
334334
.filter((name, index, arr) => arr.indexOf(name) === index) // 去重
335335
.slice(0, limit);

src/utils/gameStats.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export async function updateGameStatistics(gameId: number): Promise<void> {
9191

9292
if (isSameDay) {
9393
// 同一天,直接添加时间
94-
const currentValue = sessionsStatsMap.get(startDateStr) || 0;
94+
const currentValue = sessionsStatsMap.get(startDateStr) ?? 0;
9595
sessionsStatsMap.set(startDateStr, currentValue + session.duration);
9696
} else {
9797
// 跨天情况,按比例分配时间
@@ -118,10 +118,10 @@ export async function updateGameStatistics(gameId: number): Promise<void> {
118118
const secondDayMinutes = session.duration - firstDayMinutes;
119119

120120
// 添加到对应日期
121-
const firstDayValue = sessionsStatsMap.get(startDateStr) || 0;
121+
const firstDayValue = sessionsStatsMap.get(startDateStr) ?? 0;
122122
sessionsStatsMap.set(startDateStr, firstDayValue + firstDayMinutes);
123123

124-
const secondDayValue = sessionsStatsMap.get(endDateStr) || 0;
124+
const secondDayValue = sessionsStatsMap.get(endDateStr) ?? 0;
125125
sessionsStatsMap.set(endDateStr, secondDayValue + secondDayMinutes);
126126
}
127127
}
@@ -187,7 +187,7 @@ export async function updateGameStatistics(gameId: number): Promise<void> {
187187
}
188188

189189
// 6. 按日期降序排序
190-
dailyStats.sort((a, b) => b.date.localeCompare(a.date));
190+
dailyStats = dailyStats.toSorted((a, b) => b.date.localeCompare(a.date));
191191

192192
// 7. 通过后端服务更新统计表
193193
await statsService.updateGameStatistics(

0 commit comments

Comments
 (0)