feat(TGMap): 手写 Map

This commit is contained in:
BTMuli
2023-03-09 11:01:42 +08:00
parent 6adcfd344d
commit 3a0afedc52
4 changed files with 200 additions and 21 deletions

View File

@@ -42,7 +42,6 @@ export interface Achievement {
* @description 像是天地万象这种一直更新的成就系列,这边的 version 可能为 undefined
* @property {string} version - 成就系列版本
* @property {string} icon - 成就系列图标 todo
* @todo 为了方便列表展示,可能的话也加上成就系列包含的成就数以及已完成的成就数
* @return AchievementSeries
*/
export interface AchievementSeries {
@@ -55,23 +54,25 @@ export interface AchievementSeries {
}
/**
* @description 用于展示的成就系列类型-菜单栏左侧
* @interface AchievementSeriesDisplay
* @description 成就系列 Map 的 value 类型
* @since Alpha
* @interface SeriesMap
* @property {string} key - 成就系列 ID
* @property {number} id - 成就系列 ID
* @property {number} order - 成就系列排列顺序,用于展示全部成就系列
* @property {string} name - 成就系列名称
* @property {AchievementDisplay[]} achievements - 成就系列包含的成就
* @property {number[]} achievements - 成就系列包含的成就
* @property {number} total_count - 成就系列包含的成就数
* @property {number} completed_count - 成就系列已完成的成就数
* @property {number} card - 成就系列奖励,这边是名片 ID todo
* @property {string} icon - 成就系列图标 todo
* @return AchievementSeriesDisplay
* @return SeriesMap
*/
export interface AchievementSeriesDisplay {
export interface SeriesMap {
id: number;
order: number;
name: string;
achievements: AchievementDisplay[];
achievements: number[];
total_count: number;
completed_count: number;
card?: number;
@@ -79,19 +80,22 @@ export interface AchievementSeriesDisplay {
}
/**
* @description 用于展示的成就类型-菜单栏右侧
* @interface AchievementDisplay
* @description 成就 Map 的 value 类型
* @since Alpha
* @interface AchievementMap
* @property {number} id - 成就 ID
* @property {number} series - 成就系列 ID
* @property {number} order - 成就排列顺序,用于展示全部成就
* @property {string} name - 成就名称
* @property {string} description - 成就描述
* @property {number} reward - 成就奖励
* @property {boolean} completed - 成就是否已完成
* @property {string} completed_time - 成就完成时间
* @return AchievementDisplay
* @return AchievementMap
*/
export interface AchievementDisplay {
export interface AchievementMap {
id: number;
series: number;
order: number;
name: string;
description: string;

19
src/interface/Base.ts Normal file
View File

@@ -0,0 +1,19 @@
/**
* @file interface Base
* @description interface Base
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
/**
* @description 定义一个 Map<T> 接口
* @since Alpha
* @description 该接口的方法实现在 TGMap<T> 中
* @see TGMap
* @interface Map
* @template T
* @return Map
*/
export interface Map<T> {
[key: string]: T;
}

View File

@@ -6,16 +6,6 @@
* @version v3.5
*/
/**
* @description Paimon.moe Achievement json data interface
* @interface AchievementJson
* @example [key: string]: { AchievementSeries }
* @return {AchievementJson}
*/
export interface AchievementJson {
[key: string]: AchievementSeries;
}
/**
* @description Paimon.moe Achievement series
* @interface AchievementSeries

166
src/utils/TGMap.ts Normal file
View File

@@ -0,0 +1,166 @@
/**
* @file utils TGMap
* @description TGMap 的具体实现
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha
*/
import { Map } from "../interface/Base";
/**
* @description TGMap 的具体实现
* @since Alpha
* @class TGMap
* @template T
* @see Map
* @return TGMap
*/
class TGMap<T> {
/**
* @description TGMap 的实例化数据
* @see Map
* @description 使用 protected 修饰符,防止外部直接修改
* @since Alpha
* @return Map<T>
*/
protected map: Map<T>;
/**
* @description TGMap 的构造函数
* @since Alpha
* @param {Map<T>} map - 传入的 Map<T>
* @description 可选参数
* @return TGMap
*/
constructor(map: Map<T> = {}) {
this.map = map;
}
/**
* @description 返回 TGMap 的实例化数据
* @since Alpha
* @return Map<T>
*/
getMap(): Map<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 - 回调函数
* @return void
*/
forEach(callback: (value: T, key: string, map: Map<T>) => void): void {
Object.keys(this.map).forEach(key => {
callback(this.map[key], key, this.map);
});
}
/**
* @description Map<T> 的 get 方法
* @since Alpha
* @param {string} key - 键
* @return T
*/
get(key: string): T {
return this.map[key];
}
/**
* @description Map<T> 的 set 方法
* @since Alpha
* @param {string} key - 键
* @param {T} value - 值
* @return void
*/
set(key: string, 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 - 键
* @return boolean
*/
has(key: string): 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
* @param {(a: T, b: T) => number} callback - 回调函数
* @return TGMap<T>
*/
sort(callback: (a: T, b: T) => number): TGMap<T> {
const keys: string[] = Object.keys(this.map);
const values: T[] = Object.values(this.map);
const sortedValues: T[] = values.sort(callback);
const sortedMap: Map<T> = {};
keys.forEach((key, index) => {
sortedMap[key] = sortedValues[index];
});
return new TGMap(sortedMap);
}
}
export default TGMap;