mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 02:42:13 +00:00
Added base support for object
This commit is contained in:
parent
e57c934edb
commit
bea311ae50
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Florian Bouillon
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -1,23 +1,43 @@
|
|||||||
import chalk from 'chalk'
|
import { white, blue, yellow, green } from 'chalk'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger Class
|
||||||
|
*/
|
||||||
export default class Logger {
|
export default class Logger {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If it is set to true all message won't be shown until it is set to false
|
||||||
|
*/
|
||||||
public static isBlocked = false
|
public static isBlocked = false
|
||||||
private static queue: Array<string> = []
|
private static queue: Array<Array<any>> = []
|
||||||
private static prefixLen = 0
|
private static prefixLen = 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log a message into the console
|
||||||
|
* @param prefix the prefix used
|
||||||
|
* @param message the message to log
|
||||||
|
*/
|
||||||
public static log(prefix: string, ...message: Array<any>) {
|
public static log(prefix: string, ...message: Array<any>) {
|
||||||
|
|
||||||
this.queue.push(this.formatMessage(prefix, ...message))
|
this.queue.push(this.formatMessage(prefix, ...message))
|
||||||
while (this.queue.length > 0 && !this.isBlocked) {
|
while (this.queue.length > 0 && !this.isBlocked) {
|
||||||
console.log(this.queue.shift())
|
const item = this.queue.shift()
|
||||||
|
if (!item) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
console.log(...item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static urgent(prefix: string, ...message: Array<any>) {
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log a message into the console (passthrough the `Logger.isBlocked` boolean)
|
||||||
|
* @param prefix The prefix used
|
||||||
|
* @param message the message to log
|
||||||
|
*/
|
||||||
|
public static urgent(prefix: string, ...message: Array<any>) {
|
||||||
console.log(this.formatMessage(prefix, ...message))
|
console.log(this.formatMessage(prefix, ...message))
|
||||||
}
|
}
|
||||||
|
|
||||||
private static formatMessage(prefix: string, ...message: Array<any>): string {
|
private static formatMessage(prefix: string, ...message: Array<any>): Array<any> {
|
||||||
if (this.prefixLen < prefix.length) {
|
if (this.prefixLen < prefix.length) {
|
||||||
this.prefixLen = prefix.length
|
this.prefixLen = prefix.length
|
||||||
}
|
}
|
||||||
@ -27,7 +47,17 @@ export default class Logger {
|
|||||||
els[0] = this.buildSpace(diff / 2 - (diff % 2 !== 0 ? 1 : 0))
|
els[0] = this.buildSpace(diff / 2 - (diff % 2 !== 0 ? 1 : 0))
|
||||||
els[1] = this.buildSpace(diff / 2)
|
els[1] = this.buildSpace(diff / 2)
|
||||||
}
|
}
|
||||||
return `${chalk.white('[ ')}${els[0]}${chalk.blue(prefix)}${els[1]}${chalk.white(' ]')}: ${message.map((el) => typeof el === 'number' ? chalk.yellow(el.toString()) : chalk.white(el)).join(' ')}`
|
const res: Array<any> = [
|
||||||
|
`${white('[ ')}${els[0]}${blue(prefix)}${els[1]}${white(' ]')}:` // prefix
|
||||||
|
].concat(
|
||||||
|
message.map((el) => {
|
||||||
|
if (typeof el === 'object') {
|
||||||
|
return el
|
||||||
|
}
|
||||||
|
return typeof el !== 'string' ? yellow(el.toString()) : green(el)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
private static buildSpace(count: number): string {
|
private static buildSpace(count: number): string {
|
||||||
|
@ -14,4 +14,4 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user