From 3ecf33036ca98d245f826b39d8ee1a985a892ed3 Mon Sep 17 00:00:00 2001 From: ddmt Date: Fri, 13 Dec 2024 22:07:20 +0800 Subject: [PATCH] 1.0.9 --- Tool/number.js | 119 +++++++++++++++++++++++++++++------------------ index.js | 2 +- types/index.d.ts | 1 + 3 files changed, 75 insertions(+), 47 deletions(-) diff --git a/Tool/number.js b/Tool/number.js index 87c81d7..380d912 100644 --- a/Tool/number.js +++ b/Tool/number.js @@ -9,14 +9,14 @@ //生成从minNum到maxNum的随机数 export function randomNum (minNum, maxNum) { - switch (arguments.length) { - case 1: - return parseInt(Math.random() * minNum + 1, 10); - case 2: - return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); - default: - return 0; - } + switch (arguments.length) { + case 1: + return parseInt(Math.random() * minNum + 1, 10); + case 2: + return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); + default: + return 0; + } } @@ -25,16 +25,16 @@ export function randomNum (minNum, maxNum) { * @param {Array} arr * @param {number} index */ -export function nextArray(arr, index) { - index--; - return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1)); +export function nextArray (arr, index) { + index--; + return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1)); } /* * 数组去重函数 * @param {Array} arr */ -export function ArrayDeHeavy(arr) { +export function ArrayDeHeavy (arr) { let newArr = new Set(); arr.forEach(item => { newArr.add(item); @@ -47,38 +47,65 @@ export function ArrayDeHeavy(arr) { * 获取相对时间(中文) * @param {Date} date */ -export function getRelativeTime(date) { - const now = new Date(); - const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); - - if (diffInSeconds < 60) { - return '刚刚'; +export function getRelativeTime (date) { + const now = new Date(); + const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); + + if (diffInSeconds < 60) { + return '刚刚'; + } + + const diffInMinutes = Math.floor(diffInSeconds / 60); + if (diffInMinutes < 60) { + return `${diffInMinutes}分钟前`; + } + + const diffInHours = Math.floor(diffInMinutes / 60); + if (diffInHours < 24) { + return `${diffInHours}小时前`; + } + + const diffInDays = Math.floor(diffInHours / 24); + if (diffInDays < 30) { + return `${diffInDays}天前`; + } + + const diffInMonths = Math.floor(diffInDays / 30); + if (diffInMonths < 12) { + return `${diffInMonths}个月前`; + } + + const diffInYears = Math.floor(diffInMonths / 12); + if (diffInYears < 5) { + return `${diffInYears}年前`; + } + + return '很久以前'; +} + +/* + * 解析nginx日志(单行) + * @param {string} log + */ +function parseNginxLog (log) { + const logPattern = /^(\S+) - - \[([^\]]+)\] "(\S+) (\S+) HTTP\/\d\.\d" (\d+) (\d+) "([^"]*)" "([^"]*)"/; + + try { + const match = log.match(logPattern); + if (match) { + return { + ip: match[1], + timestamp: match[2], + method: match[3], + url: match[4], + status: parseInt(match[5]), + responseSize: parseInt(match[6]), + referrer: match[7], + userAgent: match[8], + }; } - - const diffInMinutes = Math.floor(diffInSeconds / 60); - if (diffInMinutes < 60) { - return `${diffInMinutes}分钟前`; - } - - const diffInHours = Math.floor(diffInMinutes / 60); - if (diffInHours < 24) { - return `${diffInHours}小时前`; - } - - const diffInDays = Math.floor(diffInHours / 24); - if (diffInDays < 30) { - return `${diffInDays}天前`; - } - - const diffInMonths = Math.floor(diffInDays / 30); - if (diffInMonths < 12) { - return `${diffInMonths}个月前`; - } - - const diffInYears = Math.floor(diffInMonths / 12); - if (diffInYears < 5) { - return `${diffInYears}年前`; - } - - return '很久以前'; - } \ No newline at end of file + } catch (error) { + return error; + } + return null; +} \ No newline at end of file diff --git a/index.js b/index.js index 1767148..8e7b94b 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,6 @@ * @FilePath: /index.js */ export { animateStart, setClassVar, getStyleVar } from './Tool/animate.js'; -export { randomNum, nextArray, ArrayDeHeavy, getRelativeTime } from './Tool/number.js'; +export { randomNum, nextArray, ArrayDeHeavy, getRelativeTime, parseNginxLog } 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 1cba973..d6fda63 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,5 +8,6 @@ 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; } \ No newline at end of file