OS/URL/PATH/BUFFER组件调整

This commit is contained in:
fofolee
2025-01-07 19:28:14 +08:00
parent 807da38e2e
commit e23d3e5e11
15 changed files with 902 additions and 2013 deletions

View File

@@ -1,32 +1,57 @@
const url = require("url");
// URL 解析
function parse(urlString, parseQueryString = false) {
return url.parse(urlString, parseQueryString);
function parse(urlString) {
try {
return url.parse(urlString, false);
} catch (error) {
throw new Error(`URL解析失败: ${error.message}`);
}
}
// URL 格式化
function format(urlObject) {
return url.format(urlObject);
// 格式化 URL
function format(protocol, auth, hostname, port, pathname, search, hash) {
try {
const urlObject = {
protocol,
auth,
hostname,
port,
pathname,
search,
hash,
};
return url.format(urlObject);
} catch (error) {
throw new Error(`URL格式化失败: ${error.message}`);
}
}
// 解析查询字符串
function parseQuery(query) {
const searchParams = new URLSearchParams(query);
const result = {};
for (const [key, value] of searchParams) {
result[key] = value;
function parseQuery(queryString) {
try {
const searchParams = new URLSearchParams(queryString);
const result = {};
for (const [key, value] of searchParams) {
result[key] = value;
}
return result;
} catch (error) {
throw new Error(`查询字符串解析失败: ${error.message}`);
}
return result;
}
// 格式化查询字符串
function formatQuery(queryObject) {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(queryObject)) {
searchParams.append(key, value);
function formatQuery(queryParams) {
try {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(queryParams)) {
searchParams.append(key, value);
}
return searchParams.toString();
} catch (error) {
throw new Error(`查询字符串格式化失败: ${error.message}`);
}
return searchParams.toString();
}
// 解析路径名

View File

@@ -6,7 +6,7 @@ function arch() {
}
// 获取CPU信息
function cpus({ format = "full" } = {}) {
function cpus(format = "full") {
const cpuInfo = os.cpus();
if (format === "simple") {
return cpuInfo.map(({ model, speed }) => ({ model, speed }));
@@ -15,7 +15,7 @@ function cpus({ format = "full" } = {}) {
}
// 获取内存信息
function memory({ type = "totalmem" } = {}) {
function memory(type = "totalmem") {
switch (type) {
case "totalmem":
return os.totalmem();
@@ -27,7 +27,7 @@ function memory({ type = "totalmem" } = {}) {
}
// 获取网络信息
function network({ type = "hostname", internal = false } = {}) {
function network(type = "hostname", internal = false) {
switch (type) {
case "hostname":
return os.hostname();
@@ -50,7 +50,7 @@ function network({ type = "hostname", internal = false } = {}) {
}
// 获取平台信息
function platform({ type = "platform" } = {}) {
function platform(type = "platform") {
switch (type) {
case "platform":
return os.platform();

View File

@@ -5,7 +5,7 @@ const path = require("path");
* @param {string} p 要规范化的路径
* @returns {string} 规范化后的路径
*/
async function normalize(p) {
function normalize(p) {
try {
return path.normalize(p);
} catch (error) {
@@ -18,7 +18,7 @@ async function normalize(p) {
* @param {...string} paths 路径片段
* @returns {string} 连接后的路径
*/
async function join(...paths) {
function join(...paths) {
try {
return path.join(...paths);
} catch (error) {
@@ -31,7 +31,7 @@ async function join(...paths) {
* @param {string} p 要解析的路径
* @returns {Object} 解析结果,包含 root, dir, base, ext, name
*/
async function parse(p) {
function parse(p) {
try {
return path.parse(p);
} catch (error) {
@@ -44,7 +44,7 @@ async function parse(p) {
* @param {string} p 路径
* @returns {string} 目录名
*/
async function dirname(p) {
function dirname(p) {
try {
return path.dirname(p);
} catch (error) {
@@ -58,7 +58,7 @@ async function dirname(p) {
* @param {string} [ext] 可选的扩展名,如果提供则从结果中移除
* @returns {string} 文件名
*/
async function basename(p, ext) {
function basename(p, ext = undefined) {
try {
return path.basename(p, ext);
} catch (error) {
@@ -71,7 +71,7 @@ async function basename(p, ext) {
* @param {string} p 路径
* @returns {string} 扩展名
*/
async function extname(p) {
function extname(p) {
try {
return path.extname(p);
} catch (error) {
@@ -84,7 +84,7 @@ async function extname(p) {
* @param {string} p 路径
* @returns {boolean} 是否为绝对路径
*/
async function isAbsolute(p) {
function isAbsolute(p) {
try {
return path.isAbsolute(p);
} catch (error) {
@@ -98,7 +98,7 @@ async function isAbsolute(p) {
* @param {string} to 目标路径
* @returns {string} 相对路径
*/
async function relative(from, to) {
function relative(from, to) {
try {
return path.relative(from, to);
} catch (error) {
@@ -111,7 +111,7 @@ async function relative(from, to) {
* @param {...string} paths 路径片段
* @returns {string} 解析后的绝对路径
*/
async function resolve(...paths) {
function resolve(...paths) {
try {
return path.resolve(...paths);
} catch (error) {
@@ -121,11 +121,16 @@ async function resolve(...paths) {
/**
* 格式化路径对象为路径字符串
* @param {Object} pathObject 路径对象,包含 dir, root, base, name, ext
* @param {string} root 根路径
* @param {string} dir 目录
* @param {string} base 基本名称
* @param {string} name 文件名
* @param {string} ext 扩展名
* @returns {string} 格式化后的路径
*/
async function format(pathObject) {
function format(root, dir, base, name, ext) {
try {
const pathObject = { root, dir, base, name, ext };
return path.format(pathObject);
} catch (error) {
throw new Error(`格式化路径失败: ${error.message}`);