From 27713a9d21b4cc542364bd8967953b71bf2b5537 Mon Sep 17 00:00:00 2001 From: systimotic Date: Mon, 2 Jan 2017 15:16:03 +0100 Subject: [PATCH] Improve console.log output --- common/app/routes/challenges/utils.js | 35 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/common/app/routes/challenges/utils.js b/common/app/routes/challenges/utils.js index 8d1f0b6240..bf99e7b85d 100644 --- a/common/app/routes/challenges/utils.js +++ b/common/app/routes/challenges/utils.js @@ -46,16 +46,37 @@ export function createTests({ tests = [] }) { }); } +function logReplacer(value) { + if (Array.isArray(value)) { + const replaced = value.map(logReplacer); + return '[' + replaced.join(', ') + ']'; + } + if (typeof value === 'string' && !value.startsWith('//')) { + return '"' + value + '"'; + } + if (typeof value === 'number' && isNaN(value)) { + return value.toString(); + } + if (typeof value === 'undefined') { + return 'undefined'; + } + if (value === null) { + return 'null'; + } + if (typeof value === 'function') { + return value.name; + } + if (typeof value === 'object') { + return JSON.stringify(value, null, 2); + } + + return value; +} + export function loggerToStr(args) { args = Array.isArray(args) ? args : [args]; return args - .map(arg => typeof arg === 'undefined' ? 'undefined' : arg) - .map(arg => { - if (typeof arg !== 'string') { - return JSON.stringify(arg); - } - return arg; - }) + .map(logReplacer) .reduce((str, arg) => str + arg + '\n', ''); }