fix(TGMap): toString 写的好累啊

This commit is contained in:
BTMuli
2023-03-09 12:57:26 +08:00
parent 91f030133a
commit 6ec61108c2
3 changed files with 15 additions and 72 deletions

View File

@@ -15,5 +15,5 @@
* @return Map
*/
export interface Map<T> {
[key: string]: T;
[key: number]: T;
}

View File

@@ -188,7 +188,7 @@ export default defineComponent({
const transAchievement: TGMap<TGAchievementMap> = new TGMap<TGAchievementMap>();
// 先遍历成就系列生成成就系列数据
oriSeries.map(oriSeriesItem => {
transSeries.set(oriSeriesItem.id.toString(), {
transSeries.set(oriSeriesItem.id, {
id: oriSeriesItem.id,
order: oriSeriesItem.order,
name: oriSeriesItem.name,
@@ -200,7 +200,7 @@ export default defineComponent({
// 遍历成就
oriAchievement.map(oriAchievementItem => {
// 生成成就数据
transAchievement.set(oriAchievementItem.id.toString(), {
transAchievement.set(oriAchievementItem.id, {
id: oriAchievementItem.id,
series: oriAchievementItem.series,
order: oriAchievementItem.order,
@@ -211,10 +211,10 @@ export default defineComponent({
});
// 默认成就系列是完备的,所以不需要判断成就系列是否存在
// 更新成就系列数据的 achievements 跟 total_count
const seriesItem = transSeries.get(oriAchievementItem.series.toString());
const seriesItem = transSeries.get(oriAchievementItem.series);
seriesItem.achievements.push(oriAchievementItem.id);
seriesItem.total_count += 1;
transSeries.set(oriAchievementItem.series.toString(), seriesItem);
transSeries.set(oriAchievementItem.series, seriesItem);
});
console.log("处理完成!");
// 写入文件

View File

@@ -45,106 +45,49 @@ class TGMap<T> {
return this.map;
}
/**
* @description Map<T> 的 keys 方法
* @since Alpha
* @return string[]
*/
keys(): string[] {
return Object.keys(this.map);
}
/**
* @description Map<T> 的 values 方法
* @since Alpha
* @return ArrayLike<T>
*/
values(): ArrayLike<T> {
return Object.values(this.map);
}
/**
* @description Map<T> 的 forEach 方法
* @since Alpha
* @param {(value: T, key: string, map: Map<T>) => void} callback - 回调函数
* @param {(value: T, key: number, map: Map<T>) => void} callback - 回调函数
* @return void
*/
forEach(callback: (value: T, key: string, map: Map<T>) => void): void {
forEach(callback: (value: T, key: number, map: Map<T>) => void): void {
Object.keys(this.map).forEach(key => {
callback(this.map[key], key, this.map);
callback(this.map[Number(key)], Number(key), this.map);
});
}
/**
* @description Map<T> 的 get 方法
* @since Alpha
* @param {string} key - 键
* @param {number} key - 键
* @return T
*/
get(key: string): T {
get(key: number): T {
return this.map[key];
}
/**
* @description Map<T> 的 set 方法
* @since Alpha
* @param {string} key - 键
* @param {number} key - 键
* @param {T} value - 值
* @return void
*/
set(key: string, value: T): void {
set(key: number, value: T): void {
this.map[key] = value;
}
/**
* @description Map<T> 的 delete 方法
* @since Alpha
* @param {string} key - 键
* @return void
*/
delete(key: string): void {
delete this.map[key];
}
/**
* @description Map<T> 的 clear 方法
* @since Alpha
* @return void
*/
clear(): void {
Object.keys(this.map).forEach(key => {
delete this.map[key];
});
}
/**
* @description Map<T> 的 has 方法
* @since Alpha
* @param {string} key - 键
* @param {number} key - 键
* @return boolean
*/
has(key: string): boolean {
has(key: number): boolean {
return this.map.hasOwnProperty(key);
}
/**
* @description Map<T> 的 size 方法
* @since Alpha
* @return number
*/
size(): number {
return Object.keys(this.map).length;
}
/**
* @description Map<T> 的 isEmpty 方法
* @since Alpha
* @return boolean
*/
isEmpty(): boolean {
return Object.keys(this.map).length === 0;
}
/**
* @description Map<T> 的 sort 方法
* @since Alpha
@@ -157,7 +100,7 @@ class TGMap<T> {
const sortedValues: T[] = values.sort(callback);
const sortedMap: Map<T> = {};
keys.forEach((key, index) => {
sortedMap[key] = sortedValues[index];
sortedMap[Number(key)] = sortedValues[index];
});
return new TGMap(sortedMap);
}