diff --git a/Tool/number.js b/Tool/number.js index f34cbfb..0b3b18a 100644 --- a/Tool/number.js +++ b/Tool/number.js @@ -1,13 +1,18 @@ /* * @Author: ddmt * @Date: 2024-9-29 20:50:12 - * @LastEditTime: 2024-9-30 0:13:12 + * @LastEditTime: 2025-2-18 00:55:00 * @LastEditors: ddmt * @Description: ddmt-index file * @FilePath: /Tool/number.js */ -//生成从minNum到maxNum的随机数 +/** + * 生成从minNum到maxNum的随机数 + * @param {number} minNum - 最小值 + * @param {number} [maxNum] - 最大值 + * @returns {number} - 生成的随机数 + */ export function randomNum (minNum, maxNum) { switch (arguments.length) { case 1: @@ -20,19 +25,21 @@ export function randomNum (minNum, maxNum) { } -/* +/** * 数组循环函数 - * @param {Array} arr - * @param {number} index + * @param {Array} arr - 数组 + * @param {number} index - 索引 + * @returns {Array} - 循环后的数组 */ export function nextArray (arr, index) { index--; return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1)); } -/* +/** * 数组去重函数 - * @param {Array} arr + * @param {Array} arr - 数组 + * @returns {Array} - 去重后的数组 */ export function ArrayDeHeavy (arr) { let newArr = new Set(); @@ -43,9 +50,10 @@ export function ArrayDeHeavy (arr) { } -/* +/** * 获取相对时间(中文) - * @param {Date} date + * @param {Date} date - 日期对象 + * @returns {string} - 相对时间字符串 */ export function getRelativeTime (date) { const now = new Date(); @@ -83,11 +91,12 @@ export function getRelativeTime (date) { return '很久以前'; } -/* +/** * 解析nginx日志(单行) - * @param {string} log + * @param {string} log - 日志字符串 + * @returns {Object|null} - 解析后的日志对象或null */ -function parseNginxLog (log) { +export function parseNginxLog (log) { const logPattern = /^(\S+) - - \[([^\]]+)\] "(\S+) (\S+) HTTP\/\d\.\d" (\d+) (\d+) "([^"]*)" "([^"]*)"/; try { @@ -110,12 +119,12 @@ function parseNginxLog (log) { return null; } -/* +/** * cookies 解析函数 - * @param {string} cookiesStr - * @return {Array} CookieArray + * @param {string} cookies - cookies字符串 + * @returns {Object} - 解析后的cookies对象 */ -function toCookiesArray(cookies) { +export function toCookiesArray(cookies) { if (!cookies || cookies.length === 0) return; // 解析旧的 cookies @@ -129,11 +138,13 @@ function toCookiesArray(cookies) { } -/* +/** * cookies 更新函数 - * @param {Array} setCookieArray 新的 Set-Cookie 数组 + * @param {string} cookies - 原始cookies字符串 + * @param {Array} setCookieArray - 新的 Set-Cookie 数组 + * @returns {string} - 更新后的cookies字符串 */ -function updateCookies(cookies, setCookieArray) { +export function updateCookies(cookies, setCookieArray) { if (!setCookieArray || setCookieArray.length === 0) return; // 解析旧的 cookies @@ -148,4 +159,31 @@ function updateCookies(cookies, setCookieArray) { // 重新拼接成字符串存回全局 cookies return Object.entries(cookieMap).map(([key, value]) => `${key}=${value}`).join("; "); +} + + +/** + * object 转 json + * @param {string} ObjectText - 对象字符串 + * @returns {string|undefined} - 标准 JSON 字符串或undefined + */ +export function objectToJSON(ObjectText) { + try { + // 修复键没有双引号的问题 + let fixedString = ObjectText.replace(/(\w+):/g, '"$1":'); + + // 修复单引号字符串值的问题 + fixedString = fixedString.replace(/'/g, '"'); + + // 修复尾随逗号的问题 + fixedString = fixedString.replace(/,\s*([}\]])/g, '$1'); + + // 解析为对象 + const outputObj = JSON.parse(fixedString); + + // 转换为标准 JSON 字符串 + return JSON.stringify(outputObj); + } catch (error) { + console.error("解析失败:", error); + } } \ No newline at end of file diff --git a/index.js b/index.js index 8e7b94b..2909edb 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,26 @@ /* * @Author: ddmt - * @version: 1.0.8 + * @version: 1.0.11 * @Date: 2024-9-29 20:50:12 - * @LastEditTime: 2024-11-15 20:50:12 + * @LastEditTime: 2025-2-18 00:55:00 * @LastEditors: ddmt * @Description: ddmt-index file * @FilePath: /index.js */ -export { animateStart, setClassVar, getStyleVar } from './Tool/animate.js'; -export { randomNum, nextArray, ArrayDeHeavy, getRelativeTime, parseNginxLog } from './Tool/number.js'; +export { + animateStart, + setClassVar, + getStyleVar +} from './Tool/animate.js'; +export { + randomNum, + nextArray, + ArrayDeHeavy, + getRelativeTime, + parseNginxLog, + toCookiesArray, + updateCookies, + objectToJSON +} from './Tool/number.js'; console.log('ddmt-tool Loading successfully!! 😺'); \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index d6fda63..a8d44df 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,6 +8,10 @@ declare module 'ddmt-tool' { export function randomNum(minNum: number): number; export function randomNum(minNum: number, maxNum: number): number; export function nextArray(arr: any[], index: number): any[]; - export function parseNginxLog(log: string): object; - + export function ArrayDeHeavy(arr: any[]): any[]; + export function getRelativeTime(date: Date): string; + export function parseNginxLog(log: string): { ip: string, timestamp: string, method: string, url: string, status: number, responseSize: number, referrer: string, userAgent: string } | null; + export function toCookiesArray(cookies: string): { [key: string]: string }; + export function updateCookies(cookies: string, setCookieArray: string[]): string; + export function objectToJSON(ObjectText: string): string | undefined; } \ No newline at end of file