Improve console.log output

This commit is contained in:
systimotic
2017-01-02 15:16:03 +01:00
parent 782f489576
commit 27713a9d21

View File

@ -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', '');
}