From 3ed0c720bc571cd45edac8e6f0c99478c9b63dac Mon Sep 17 00:00:00 2001 From: Joydip Roy Date: Sun, 27 May 2018 11:31:36 +0545 Subject: [PATCH 1/3] feat(): Customizable timer start/end text --- .gitignore | 3 +++ signale.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c102fc85..5ca46b19 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ node_modules yarn.lock +# folder +example + # logs *.log diff --git a/signale.js b/signale.js index 9347daff..60d19a7a 100644 --- a/signale.js +++ b/signale.js @@ -19,11 +19,14 @@ class Signale { this._config = Object.assign(this.packageConfiguration, options.config); this._customTypes = Object.assign({}, options.types); this._scopeName = options.scope || ''; - this._timers = options.timers || new Map(); + this._timers = new Map(); + this.__timers = options.timers || {}; this._types = Object.assign({}, types, this._customTypes); this._stream = options.stream || process.stdout; this._longestLabel = types.start.label.length; + // Object.keys(this.__timers).forEach(item => console.log(item)); + Object.keys(this._types).forEach(type => { this[type] = this._logger.bind(this, type); }); @@ -207,6 +210,25 @@ class Signale { this._scopeName = ''; } + textType(text) { + let _textType = null; + switch (text) { + case typeof text === 'string': + _textType = 'string'; + break; + case typeof text === 'object': + _textType = 'object'; + break; + case typeof text === 'boolean': + _textType = 'boolean'; + break; + default: + _textType = null; + break; + } + return _textType; + } + time(label) { if (!label) { label = `timer_${this._timers.size}`; @@ -215,14 +237,21 @@ class Signale { this._timers.set(label, Date.now()); const message = this._meta(); + const timerStart = this.__timers.start || {}; + + const __color = chalk[timerStart.color] || chalk.green; + const __badge = timerStart.badge || this._types.start.badge; + const __text = this.textType(timerStart.text) === 'object' ? timerStart.text.join(' ') : timerStart.text || 'Initialized timer...'; + const report = [ - chalk.green(this._types.start.badge.padEnd(2)), - chalk.green.underline(label).padEnd(this._longestLabel + 20), - 'Initialized timer...' + __color(__badge.padEnd(4)), + __color.underline(label).padEnd(this._longestLabel + 22), + __text ]; message.push(...report); this._log(message.join(' ')); + return label; } @@ -233,16 +262,26 @@ class Signale { return is(x) ? x : (is(y) ? y : null); }); } + if (this._timers.has(label)) { const span = timeSpan(this._timers.get(label)); this._timers.delete(label); const message = this._meta(); + + const executionTime = span < 1000 ? span + 'ms' : (span / 1000).toFixed(2) + 's'; + const timerEnd = this.__timers.end || {}; + + const __color = chalk[timerEnd.color] || chalk.red; + const __badge = timerEnd.badge || this._types.pause.badge; + const __text = timerEnd.text || 'Timer run for:'; + const __executionColor = chalk[timerEnd.time] || chalk.yellow; + const report = [ - chalk.red(this._types.pause.badge.padEnd(2)), - chalk.red.underline(label).padEnd(this._longestLabel + 20), - 'Timer run for:', - chalk.yellow(span < 1000 ? span + 'ms' : (span / 1000).toFixed(2) + 's') + __color(__badge.padEnd(4)), + __color.underline(label).padEnd(this._longestLabel + 22), + __text, + __executionColor(executionTime) ]; message.push(...report); From 6abe05e1b00fde79d9a94a4b947b5bb3ad440242 Mon Sep 17 00:00:00 2001 From: Joydip Roy Date: Sun, 27 May 2018 13:12:36 +0545 Subject: [PATCH 2/3] feat(): Additional context --- signale.js | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/signale.js b/signale.js index 60d19a7a..15faa8fd 100644 --- a/signale.js +++ b/signale.js @@ -20,12 +20,14 @@ class Signale { this._customTypes = Object.assign({}, options.types); this._scopeName = options.scope || ''; this._timers = new Map(); - this.__timers = options.timers || {}; + this.__timers = options.timers || {start: {}, end: {}}; this._types = Object.assign({}, types, this._customTypes); this._stream = options.stream || process.stdout; this._longestLabel = types.start.label.length; - // Object.keys(this.__timers).forEach(item => console.log(item)); + Object.keys(options).forEach(type => { + this[type] = options[type]; + }); Object.keys(this._types).forEach(type => { this[type] = this._logger.bind(this, type); @@ -211,33 +213,55 @@ class Signale { } textType(text) { - let _textType = null; - switch (text) { - case typeof text === 'string': - _textType = 'string'; + let textType = null; + switch (typeof text) { + case 'string': + textType = 'string'; break; - case typeof text === 'object': - _textType = 'object'; + case 'object': + textType = 'object'; break; - case typeof text === 'boolean': - _textType = 'boolean'; + case 'boolean': + textType = 'boolean'; break; default: - _textType = null; + textType = null; break; } - return _textType; + return textType; } - time(label) { + time(label, options) { if (!label) { label = `timer_${this._timers.size}`; } + if (options) { + if (options.both) { + Object.keys(options.both).forEach(key => { + this.__timers.start[key] = options.both[key]; + this.__timers.end[key] = options.both[key]; + }); + } else { + if (options.start) { + this.__timers.start = options.start; + } + + if (options.end) { + this.__timers.end = options.end; + } + + Object.keys(options).forEach(key => { + this.__timers.start[key] = options[key]; + this.__timers.end[key] = options[key]; + }); + } + } + this._timers.set(label, Date.now()); const message = this._meta(); - const timerStart = this.__timers.start || {}; + const timerStart = this.__timers.start; const __color = chalk[timerStart.color] || chalk.green; const __badge = timerStart.badge || this._types.start.badge; @@ -274,7 +298,7 @@ class Signale { const __color = chalk[timerEnd.color] || chalk.red; const __badge = timerEnd.badge || this._types.pause.badge; - const __text = timerEnd.text || 'Timer run for:'; + const __text = this.textType(timerEnd.text) === 'object' ? timerEnd.text.join(' ') : timerEnd.text || 'Timer run for:'; const __executionColor = chalk[timerEnd.time] || chalk.yellow; const report = [ From e3d8b509d95a0818d4a96f2e775ad3221b5f9990 Mon Sep 17 00:00:00 2001 From: Joydip Roy Date: Sun, 27 May 2018 13:31:24 +0545 Subject: [PATCH 3/3] fix(): display time in between end text array --- signale.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/signale.js b/signale.js index 15faa8fd..acebd991 100644 --- a/signale.js +++ b/signale.js @@ -298,14 +298,13 @@ class Signale { const __color = chalk[timerEnd.color] || chalk.red; const __badge = timerEnd.badge || this._types.pause.badge; - const __text = this.textType(timerEnd.text) === 'object' ? timerEnd.text.join(' ') : timerEnd.text || 'Timer run for:'; const __executionColor = chalk[timerEnd.time] || chalk.yellow; + const __text = this.textType(timerEnd.text) === 'object' ? timerEnd.text.join().replace(',', ` ${__executionColor(executionTime)} `) : timerEnd.text || `Timer run for: ${__executionColor(executionTime)}`; const report = [ __color(__badge.padEnd(4)), __color.underline(label).padEnd(this._longestLabel + 22), - __text, - __executionColor(executionTime) + __text ]; message.push(...report);