第一版本的初略修改,

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

80
dist/util/FileUtil.js vendored Normal file
View File

@@ -0,0 +1,80 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require('fs');
const Path = require('path');
/**
* 文件工具
*/
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 map(path, handler) {
return __awaiter(this, void 0, void 0, function* () {
if (!Fs.existsSync(path))
return;
const stats = Fs.statSync(path);
if (stats.isDirectory()) {
const names = Fs.readdirSync(path);
for (const name of names) {
yield this.map(Path.join(path, name), handler);
}
}
else if (stats.isFile()) {
yield handler(path, stats);
}
});
}
}
exports.default = FileUtil;

67
dist/util/ObjectUtil.js vendored Normal file
View File

@@ -0,0 +1,67 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 对象工具
*/
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;
}
}
exports.default = ObjectUtil;