koishi-utils
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -5,2 +5,6 @@ declare type CQCodeData = Record<string, string | number | boolean>; | ||
data: CQCodeData; | ||
} | ||
interface ParsedCQCode { | ||
type: string; | ||
data: Record<string, string>; | ||
capture?: RegExpMatchArray; | ||
@@ -13,5 +17,5 @@ } | ||
function stringifyAll(codes: (string | CQCode)[]): string; | ||
function parse(source: string): CQCode; | ||
function parseAll(source: string): (string | CQCode)[]; | ||
function parse(source: string): ParsedCQCode; | ||
function parseAll(source: string): (string | ParsedCQCode)[]; | ||
} | ||
export default CQCode; |
@@ -14,3 +14,4 @@ export declare function setTimezoneOffset(offset: number): void; | ||
export declare function parseDate(date: string): Date; | ||
export declare function formatTimeShort(ms: number): string; | ||
export declare function formatTime(ms: number): string; | ||
export declare function formatTimeInterval(time: Date, interval: number): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatTimeInterval = exports.formatTime = exports.parseDate = exports.parseTime = exports.fromDateNumber = exports.getDateNumber = exports.Time = exports.getTimezoneOffset = exports.setTimezoneOffset = void 0; | ||
exports.formatTimeInterval = exports.formatTime = exports.formatTimeShort = exports.parseDate = exports.parseTime = exports.fromDateNumber = exports.getDateNumber = exports.Time = exports.getTimezoneOffset = exports.setTimezoneOffset = void 0; | ||
let timezoneOffset = new Date().getTimezoneOffset(); | ||
@@ -49,4 +49,5 @@ function setTimezoneOffset(offset) { | ||
function parseDate(date) { | ||
let parsed; | ||
if (parsed = parseTime(date)) { | ||
const parsed = parseTime(date); | ||
// eslint-disable-next-line no-cond-assign | ||
if (parsed) { | ||
date = Date.now() + parsed; | ||
@@ -63,2 +64,19 @@ } | ||
exports.parseDate = parseDate; | ||
function formatTimeShort(ms) { | ||
const abs = Math.abs(ms); | ||
if (abs >= Time.day - Time.hour / 2) { | ||
return Math.round(ms / Time.day) + 'd'; | ||
} | ||
else if (abs >= Time.hour - Time.minute / 2) { | ||
return Math.round(ms / Time.hour) + 'h'; | ||
} | ||
else if (abs >= Time.minute - Time.second / 2) { | ||
return Math.round(ms / Time.minute) + 'm'; | ||
} | ||
else if (abs >= Time.second) { | ||
return Math.round(ms / Time.second) + 's'; | ||
} | ||
return ms + 'ms'; | ||
} | ||
exports.formatTimeShort = formatTimeShort; | ||
function formatTime(ms) { | ||
@@ -65,0 +83,0 @@ let result; |
@@ -6,8 +6,12 @@ /// <reference types="node" /> | ||
private name; | ||
private showDiff; | ||
static baseLevel: number; | ||
static showDiff: boolean; | ||
static levels: Record<string, number>; | ||
static lastTime: number; | ||
static options: InspectOptions; | ||
static formatters: Record<string, (this: Logger, value: any) => string>; | ||
static create(name?: string): Logger; | ||
private colorCode; | ||
static create(name?: string, showDiff?: boolean): Logger; | ||
static color(code: number, value: any, decoration?: string): string; | ||
private code; | ||
private displayName; | ||
@@ -20,3 +24,3 @@ success: LogFunction; | ||
private constructor(); | ||
private wrapColor; | ||
private color; | ||
private createMethod; | ||
@@ -23,0 +27,0 @@ get level(): number; |
@@ -5,5 +5,6 @@ "use strict"; | ||
const util_1 = require("util"); | ||
const date_1 = require("./date"); | ||
const supports_color_1 = require("supports-color"); | ||
const tty_1 = require("tty"); | ||
const isTTY = tty_1.isatty(process.stderr['fd']); | ||
const isTTY = tty_1.isatty(process.stderr.fd); | ||
const colors = supports_color_1.stderr.level < 2 ? [6, 2, 3, 4, 5, 1] : [ | ||
@@ -18,4 +19,5 @@ 20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, | ||
class Logger { | ||
constructor(name) { | ||
constructor(name, showDiff = false) { | ||
this.name = name; | ||
this.showDiff = showDiff; | ||
this.extend = (namespace) => { | ||
@@ -44,2 +46,9 @@ return Logger.create(`${this.name}:${namespace}`); | ||
}).split('\n').join('\n '); | ||
if (Logger.showDiff || this.showDiff) { | ||
const now = Date.now(); | ||
if (Logger.lastTime) { | ||
args.push(this.color('+' + date_1.formatTimeShort(now - Logger.lastTime))); | ||
} | ||
Logger.lastTime = now; | ||
} | ||
return util_1.format(...args); | ||
@@ -52,3 +61,3 @@ }; | ||
} | ||
this.colorCode = colors[Math.abs(hash) % colors.length]; | ||
this.code = colors[Math.abs(hash) % colors.length]; | ||
instances[this.name] = this; | ||
@@ -59,3 +68,3 @@ this.displayName = name; | ||
if (isTTY) | ||
this.displayName = this.wrapColor(this.displayName, ';1'); | ||
this.displayName = this.color(this.displayName, ';1'); | ||
this.createMethod('success', '[S] ', 1); | ||
@@ -67,11 +76,16 @@ this.createMethod('error', '[E] ', 1); | ||
} | ||
static create(name = '') { | ||
return instances[name] || new Logger(name); | ||
static create(name = '', showDiff = false) { | ||
const logger = instances[name] || new Logger(name); | ||
if (showDiff !== undefined) | ||
logger.showDiff = showDiff; | ||
return logger; | ||
} | ||
wrapColor(value, decoration = '') { | ||
static color(code, value, decoration = '') { | ||
if (!Logger.options.colors) | ||
return '' + value; | ||
const code = this.colorCode; | ||
return `\u001B[3${code < 8 ? code : '8;5;' + code}${decoration}m${value}\u001B[0m`; | ||
} | ||
color(value, decoration = '') { | ||
return Logger.color(this.code, value, decoration); | ||
} | ||
createMethod(name, prefix, minLevel) { | ||
@@ -91,3 +105,5 @@ this[name] = (...args) => { | ||
Logger.baseLevel = 2; | ||
Logger.showDiff = false; | ||
Logger.levels = {}; | ||
Logger.lastTime = 0; | ||
Logger.options = { | ||
@@ -97,5 +113,6 @@ colors: isTTY, | ||
Logger.formatters = { | ||
c: Logger.prototype.wrapColor, | ||
c: Logger.prototype.color, | ||
C: value => Logger.color(15, value, ';1'), | ||
o: value => util_1.inspect(value, Logger.options).replace(/\s*\n\s*/g, ' '), | ||
}; | ||
//# sourceMappingURL=logger.js.map |
{ | ||
"name": "koishi-utils", | ||
"description": "Utilities for Koishi", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"main": "dist/index.js", | ||
@@ -12,3 +12,3 @@ "typings": "dist/index.d.ts", | ||
"author": "Shigma <1700011071@pku.edu.cn>", | ||
"license": "MIT", | ||
"license": "LGPL-3.0-or-later", | ||
"scripts": { | ||
@@ -15,0 +15,0 @@ "jest": "jest tests/.*\\.spec\\.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
827
72973
33
2
70