Commit 6d2dc138 by Leo Zheng

给util代码增加注释

parent bf81317a
/**
* DataExtractor.ts
* 该文件定义了数据提取器类,用于从 Excel 文件中提取数据。
*/
import { join } from 'path';
import { readdirSync, readFileSync } from 'fs';
import * as XLSX from 'xlsx';
/**
* 该文件定义了数据提取器类,采用单例模式,用于加载和解析 Excel 文件中的数据。
*/
export class DataExtractor {
private static instance: DataExtractor;
private data: { [key: string]: { [sheet: string]: any } } = {};
/**
* 私有构造函数,防止外部实例化,加载所有数据。
*/
private constructor() {
this.loadAllData();
}
/**
* 获取 DataExtractor 类的实例,确保单例模式。
* @returns DataExtractor 的实例。
*/
public static getInstance(): DataExtractor {
if (!DataExtractor.instance) {
DataExtractor.instance = new DataExtractor();
......@@ -17,6 +32,9 @@ export class DataExtractor {
return DataExtractor.instance;
}
/**
* 加载目录中所有 Excel 文件的数据。
*/
private loadAllData(): void {
const dirPath = join(__dirname, '../../src/data');
const files = readdirSync(dirPath).filter(file => file.endsWith('.xlsx'));
......@@ -27,6 +45,11 @@ export class DataExtractor {
});
}
/**
* 解析 Excel 文件,将其转换为 JSON 格式。
* @param filePath - Excel 文件的路径。
* @returns 解析后的数据,以工作表名称为键的对象。
*/
private parseExcel(filePath: string): { [sheet: string]: any } {
const file = readFileSync(filePath);
const workbook = XLSX.read(file, { type: 'buffer' });
......@@ -40,6 +63,13 @@ export class DataExtractor {
return data;
}
/**
* 获取指定文件和工作表中的数据。
* @param fileName - 文件名称。
* @param sheetName - 工作表名称。
* @returns 指定工作表中的数据。
* @throws 如果文件或工作表不存在,则抛出错误。
*/
public getData(fileName: string, sheetName: string): any {
const fileData = this.data[fileName];
if (!fileData) {
......@@ -51,16 +81,4 @@ export class DataExtractor {
}
return sheetData;
}
public getAvailableFiles(): string[] {
return Object.keys(this.data);
}
public getAvailableSheets(fileName: string): string[] {
const fileData = this.data[fileName + '.xlsx'];
if (!fileData) {
throw new Error(`File ${fileName} not found`);
}
return Object.keys(fileData);
}
}
/**
* excelSerialToJSDate.ts
* 该文件定义了一个函数,用于将 Excel 日期序列号转换为 JavaScript 日期对象。
*/
/**
* 将 Excel 日期序列号转换为 JavaScript 日期对象。
* @param serial - Excel 日期序列号。
* @returns 转换后的 JavaScript 日期对象。
*/
export default function excelSerialToJSDate(serial: number): Date {
// Excel 时间纪元(1970年1月1日之前的天数)
const EXCEL_EPOCH = 25569;
// 每天的毫秒数
const MS_PER_DAY = 86400000;
// Split the excelDate into integer and fractional parts
// 将 Excel 日期拆分为整数部分和小数部分
const days = Math.floor(serial);
const fractionalDay = serial - days;
// Convert to milliseconds
// 转换为毫秒数
const jsDateMilliseconds = (days - EXCEL_EPOCH) * MS_PER_DAY + fractionalDay * MS_PER_DAY;
// 返回 JavaScript 日期对象
return new Date(jsDateMilliseconds);
}
\ No newline at end of file
/**
* mapToObj.ts
* 该文件定义了一个函数,用于将 Map 对象转换为普通的 JavaScript 对象。
*/
/**
* 将 Map 对象转换为普通的 JavaScript 对象。
* @param map - 要转换的 Map 对象。
* @returns 转换后的 JavaScript 对象。
*/
export default function mapToObj(map) {
let Obj: { [key: string]: string } = {};
map.forEach((value, key) => {
......
/**
* getRandomNumber.ts
* 该文件定义了一个函数,用于生成指定范围内的随机数,并保留指定的小数位数。
*/
/**
* 生成指定范围内的随机数,并保留指定的小数位数。
* @param min - 最小值。
* @param max - 最大值。
* @param decimalPlaces - 保留的小数位数。
* @returns 生成的随机数。
*/
export default function getRandomNumber(min: number, max: number, decimalPlaces: number): number {
// 生成 min 到 max 范围内的随机数
const randomNum = Math.random() * (max - min) + min;
// 将随机数四舍五入到指定的小数位数
return Math.round(randomNum * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
}
\ No newline at end of file
/**
* randomStatusGenerator.ts
* 该文件定义了 randomStatusGenerator 类,用于生成随机状态。
*/
/**
* randomStatusGenerator 类,用于生成随机状态。
*/
export class randomStatusGenerator {
// 定义可用状态的数组
private static statuses: string[] = ['正常', '拥挤', '顺畅'];
/**
* 获取一个随机状态。
* @returns 一个随机选择的状态字符串。
*/
static getRandomStatus(): string {
// 生成一个随机索引
const randomIndex = Math.floor(Math.random() * randomStatusGenerator.statuses.length);
// 返回对应索引的状态
return this.statuses[randomIndex];
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment