chore: vault tests refactors and lint (#2374)

This commit is contained in:
Karol Sójko
2023-08-02 00:23:56 +02:00
committed by GitHub
parent a0bc1d2488
commit 247daddf5a
96 changed files with 1463 additions and 751 deletions

View File

@@ -0,0 +1 @@
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none'

View File

@@ -0,0 +1,64 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { LogLevel } from './LogLevel'
export class Logger {
private level: LogLevel = 'none'
constructor(private appIdentifier: string) {}
private canLog(level: LogLevel): boolean {
if (this.level === 'none') {
return false
}
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error']
return levels.indexOf(level) >= levels.indexOf(this.level)
}
public setLevel(level: LogLevel): void {
this.level = level
}
public debug(message: string, ...optionalParams: any[]): void {
if (this.canLog('debug')) {
this.logWithColor(message, ...optionalParams)
}
}
public info(message: string, ...optionalParams: any[]): void {
if (this.canLog('info')) {
this.logWithColor(message, ...optionalParams)
}
}
public warn(message: string, ...optionalParams: any[]): void {
if (this.canLog('warn')) {
console.warn(message, ...optionalParams)
}
}
public error(message: string, ...optionalParams: any[]): void {
if (this.canLog('error')) {
console.error(message, ...optionalParams)
}
}
private logWithColor(...args: any[]): void {
const date = new Date()
const timeString = `${date.toLocaleTimeString().replace(' PM', '').replace(' AM', '')}.${date.getMilliseconds()}`
this.customLog(
`%c${this.appIdentifier}%c${timeString}`,
'color: font-weight: bold; margin-right: 4px',
'color: gray',
...args,
)
}
private customLog(..._args: any[]) {
// eslint-disable-next-line no-console, prefer-rest-params
Function.prototype.apply.call(console.log, console, arguments)
}
}
export default Logger

View File

@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { LogLevel } from './LogLevel'
export interface LoggerInterface {
setLevel(level: LogLevel): void
debug(message: string, ...optionalParams: any[]): void
info(message: string, ...optionalParams: any[]): void
warn(message: string, ...optionalParams: any[]): void
error(message: string, ...optionalParams: any[]): void
}

View File

@@ -1,8 +1,11 @@
export * from './Date/DateUtils'
export * from './Deferred/Deferred'
export * from './Logger/Logger'
export * from './Logger/LoggerInterface'
export * from './Logger/LogLevel'
export * from './Utils/ClassNames'
export * from './Utils/Utils'
export * from './Utils/Debounce'
export * from './Utils/Utils'
export * from './Uuid/Utils'
export * from './Uuid/UuidGenerator'
export * from './Uuid/UuidMap'