@kitql/helper
Advanced tools
Comparing version 0.3.1 to 0.3.2
42
index.js
@@ -21,10 +21,40 @@ 'use strict'; | ||
class Log { | ||
constructor(toolName) { | ||
constructor(toolName, logLevel = null, withDate = null) { | ||
this.toolName = toolName; | ||
this.logLevel = logLevel; | ||
this.withDate = withDate; | ||
} | ||
info(msg) { | ||
console.info(`${logMagneta(`[${this.toolName}]`)} ${msg}`); | ||
buildStr(msg, withError, withSuccess, indent) { | ||
const table = []; | ||
table.push(`${logMagneta(`[${this.toolName}]`)}`); | ||
if (this.withDate === 'dateTime') { | ||
table.push(`${logMagneta(`[${new Date().toISOString()}]`)}`); | ||
} | ||
else if (this.withDate === 'time') { | ||
table.push(`${logMagneta(`[${new Date().toISOString().split('T')[1]}]`)}`); | ||
} | ||
if (withError) { | ||
table.push(`❌`); | ||
} | ||
if (withSuccess) { | ||
table.push(`✅`); | ||
} | ||
table.push(indent); | ||
table.push(` ${msg}`); | ||
return table.join(''); | ||
} | ||
info(msg, conf = { level: 0, withSuccess: false }) { | ||
var _a, _b; | ||
const level = (_a = conf.level) !== null && _a !== void 0 ? _a : 0; | ||
const withSuccess = (_b = conf.withSuccess) !== null && _b !== void 0 ? _b : false; | ||
if (this.logLevel && level <= this.logLevel) { | ||
const indent = ' '.repeat(level); | ||
console.info(this.buildStr(msg, false, withSuccess, indent)); | ||
} | ||
} | ||
success(msg, conf = { level: 0 }) { | ||
this.info(msg, { level: conf.level, withSuccess: true }); | ||
} | ||
error(msg) { | ||
console.error(`${logMagneta(`[${this.toolName}]`)}${logRed(`[E]`)} ${msg}`); | ||
console.error(this.buildStr(msg, true, false, '')); | ||
} | ||
@@ -66,2 +96,6 @@ } | ||
const keys = Object.keys(object); | ||
// If there are no keys, the Object was not {}, let's return the object directly | ||
if (keys.length === 0) { | ||
return object; | ||
} | ||
keys.sort((key1, key2) => { | ||
@@ -68,0 +102,0 @@ (key1 = key1.toLowerCase()), (key2 = key2.toLowerCase()); |
13
Log.d.ts
@@ -8,5 +8,14 @@ export declare function logGreen(str: string): string; | ||
private toolName; | ||
constructor(toolName: string); | ||
info(msg: string): void; | ||
private logLevel; | ||
private withDate; | ||
constructor(toolName: string, logLevel?: null | 0 | 1 | 2, withDate?: null | 'dateTime' | 'time'); | ||
private buildStr; | ||
info(msg: string, conf?: { | ||
level?: 0 | 1 | 2; | ||
withSuccess?: boolean; | ||
}): void; | ||
success(msg: string, conf?: { | ||
level?: 0 | 1 | 2; | ||
}): void; | ||
error(msg: string): void; | ||
} |
{ | ||
"name": "@kitql/helper", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
Sorry, the diff of this file is not supported yet
10047
304