第一版本的初略修改,

This commit is contained in:
rh
2024-10-31 10:37:08 +08:00
parent 5dee50969c
commit 824da453f3
14 changed files with 1577 additions and 0 deletions

64
source/util/FileUtil.ts Normal file
View File

@@ -0,0 +1,64 @@
const Fs = require('fs');
const Path = require('path');
/**
* 文件工具
*/
export default class FileUtil {
/**
* 复制文件/文件夹
* @param {Fs.PathLike} srcPath 源路径
* @param {Fs.PathLike} destPath 目标路径
*/
static copy(srcPath, destPath) {
if (!Fs.existsSync(srcPath)) return;
const stats = Fs.statSync(srcPath);
if (stats.isDirectory()) {
if (!Fs.existsSync(destPath)) Fs.mkdirSync(destPath);
const names = Fs.readdirSync(srcPath);
for (const name of names) {
this.copy(Path.join(srcPath, name), Path.join(destPath, name));
}
} else if (stats.isFile()) {
Fs.writeFileSync(destPath, Fs.readFileSync(srcPath));
}
}
/**
* 删除文件/文件夹
* @param {Fs.PathLike} path 路径
*/
static delete(path) {
if (!Fs.existsSync(path)) return;
const stats = Fs.statSync(path);
if (stats.isDirectory()) {
const names = Fs.readdirSync(path);
for (const name of names) {
this.delete(Path.join(path, name));
}
Fs.rmdirSync(path);
} else if (stats.isFile()) {
Fs.unlinkSync(path);
}
}
/**
* 遍历文件/文件夹并执行函数
* @param {Fs.PathLike} path 路径
* @param {(filePath: Fs.PathLike, stat: Fs.Stats) => void} handler 处理函数
*/
static async map(path:string, handler: (string,any)=>Promise<void>) {
if (!Fs.existsSync(path)) return
const stats = Fs.statSync(path);
if (stats.isDirectory()) {
const names = Fs.readdirSync(path);
for (const name of names) {
await this.map(Path.join(path, name), handler);
}
} else if (stats.isFile()) {
await handler(path, stats);
}
}
}

66
source/util/ObjectUtil.ts Normal file
View File

@@ -0,0 +1,66 @@
/**
* 对象工具
*/
export default class ObjectUtil {
/**
* 判断指定值是否是一个对象
* @param {any} arg 参数
*/
static isObject(arg) {
return Object.prototype.toString.call(arg) === '[object Object]';
}
/**
* 对象中是否包含指定的属性
* @param {object} object 对象
* @param {any} name 属性名
*/
static containsProperty(object, name) {
let result = false;
const search = (_object) => {
if (this.isObject(_object)) {
for (const key in _object) {
if (key == name) {
result = true;
return;
}
search(_object[key]);
}
} else if (Array.isArray(_object)) {
for (let i = 0; i < _object.length; i++) {
search(_object[i]);
}
}
}
search(object);
return result;
}
/**
* 对象中是否包含指定的值
* @param {object} object 对象
* @param {any} value 值
*/
static containsValue(object, value) {
let result = false;
const search = (_object) => {
if (this.isObject(_object)) {
for (const key in _object) {
if (_object[key] === value) {
result = true;
return;
}
search(_object[key]);
}
} else if (Array.isArray(_object)) {
for (let i = 0; i < _object.length; i++) {
search(_object[i]);
}
}
}
search(object);
return result;
}
}