1.0.9
This commit is contained in:
parent
7f8b2e4f7a
commit
3ecf33036c
119
Tool/number.js
119
Tool/number.js
@ -9,14 +9,14 @@
|
|||||||
|
|
||||||
//生成从minNum到maxNum的随机数
|
//生成从minNum到maxNum的随机数
|
||||||
export function randomNum (minNum, maxNum) {
|
export function randomNum (minNum, maxNum) {
|
||||||
switch (arguments.length) {
|
switch (arguments.length) {
|
||||||
case 1:
|
case 1:
|
||||||
return parseInt(Math.random() * minNum + 1, 10);
|
return parseInt(Math.random() * minNum + 1, 10);
|
||||||
case 2:
|
case 2:
|
||||||
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
|
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -25,16 +25,16 @@ export function randomNum (minNum, maxNum) {
|
|||||||
* @param {Array} arr
|
* @param {Array} arr
|
||||||
* @param {number} index
|
* @param {number} index
|
||||||
*/
|
*/
|
||||||
export function nextArray(arr, index) {
|
export function nextArray (arr, index) {
|
||||||
index--;
|
index--;
|
||||||
return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1));
|
return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 数组去重函数
|
* 数组去重函数
|
||||||
* @param {Array} arr
|
* @param {Array} arr
|
||||||
*/
|
*/
|
||||||
export function ArrayDeHeavy(arr) {
|
export function ArrayDeHeavy (arr) {
|
||||||
let newArr = new Set();
|
let newArr = new Set();
|
||||||
arr.forEach(item => {
|
arr.forEach(item => {
|
||||||
newArr.add(item);
|
newArr.add(item);
|
||||||
@ -47,38 +47,65 @@ export function ArrayDeHeavy(arr) {
|
|||||||
* 获取相对时间(中文)
|
* 获取相对时间(中文)
|
||||||
* @param {Date} date
|
* @param {Date} date
|
||||||
*/
|
*/
|
||||||
export function getRelativeTime(date) {
|
export function getRelativeTime (date) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
||||||
|
|
||||||
if (diffInSeconds < 60) {
|
if (diffInSeconds < 60) {
|
||||||
return '刚刚';
|
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],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
return error;
|
||||||
if (diffInMinutes < 60) {
|
}
|
||||||
return `${diffInMinutes}分钟前`;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 '很久以前';
|
|
||||||
}
|
|
2
index.js
2
index.js
@ -8,6 +8,6 @@
|
|||||||
* @FilePath: /index.js
|
* @FilePath: /index.js
|
||||||
*/
|
*/
|
||||||
export { animateStart, setClassVar, getStyleVar } from './Tool/animate.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!! 😺');
|
console.log('ddmt-tool Loading successfully!! 😺');
|
1
types/index.d.ts
vendored
1
types/index.d.ts
vendored
@ -8,5 +8,6 @@ declare module 'ddmt-tool' {
|
|||||||
export function randomNum(minNum: number): number;
|
export function randomNum(minNum: number): number;
|
||||||
export function randomNum(minNum: number, maxNum: number): number;
|
export function randomNum(minNum: number, maxNum: number): number;
|
||||||
export function nextArray(arr: any[], index: number): any[];
|
export function nextArray(arr: any[], index: number): any[];
|
||||||
|
export function parseNginxLog(log: string): object;
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user