From 9e6fb354915cbf8ac0eef10e3eb8bad90ea18097 Mon Sep 17 00:00:00 2001 From: surunzi Date: Wed, 24 Aug 2016 21:55:07 +0800 Subject: [PATCH] Add: Table support --- src/Console/Console.es6 | 2 +- src/Console/Log.es6 | 48 ++++++++++++++++++++++++++++++++++++++--- src/Console/Logger.scss | 2 +- test/console.js | 26 ++++++++++++++++++++-- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/Console/Console.es6 b/src/Console/Console.es6 index d6bc5e6..4fc7626 100644 --- a/src/Console/Console.es6 +++ b/src/Console/Console.es6 @@ -221,4 +221,4 @@ export default class Console extends Tool } } -const CONSOLE_METHOD = ['log', 'error', 'info', 'warn', 'dir', 'time', 'timeEnd', 'clear']; +const CONSOLE_METHOD = ['log', 'error', 'info', 'warn', 'dir', 'time', 'timeEnd', 'clear', 'table']; diff --git a/src/Console/Log.es6 b/src/Console/Log.es6 index dd2dc44..da1b8c9 100644 --- a/src/Console/Log.es6 +++ b/src/Console/Log.es6 @@ -60,14 +60,21 @@ export default class Log switch (type) { case 'log': + msg = formatMsg(args); + break; case 'info': + icon = 'info-circle'; + msg = formatMsg(args); + break; case 'warn': + icon = 'exclamation-triangle'; msg = formatMsg(args); break; case 'error': + args = substituteStr(args); let err = args[0]; icon = 'times-circle'; - err = util.isErr(args[0]) ? args[0] : new Error(err); + err = util.isErr(err) ? err : new Error(err); msg = formatErr(err); break; case 'table': @@ -95,7 +102,40 @@ export default class Log function formatTable(args) { - return ''; + let table = args[0], + ret = '', + filter = args[1], + columns = []; + + if (util.isStr(filter)) filter = util.toArr(filter); + if (!util.isArr(filter)) filter = null; + + if (!util.isArr(table)) return formatMsg(args); + + table.forEach(val => + { + if (!util.isObj(val)) return; + columns = columns.concat(Object.getOwnPropertyNames(val)); + }); + columns = util.unique(columns); + if (filter) columns = columns.filter(val => util.contain(filter, val)); + if (util.isEmpty(columns)) return formatMsg(args); + + ret += ''; + columns.forEach(val => ret += ``); + ret += ''; + + table.forEach((obj, idx) => + { + if (!util.isObj(obj)) return; + ret += ``; + columns.forEach(column => ret += ``); + ret += '' + }); + + ret += '
(index)${val}
${idx}${obj[column] || ''}
'; + + return ret; } var regJsUrl = /https?:\/\/([0-9.\-A-Za-z]+)(?::(\d+))?\/[A-Z.a-z0-9/]*\.js/g; @@ -119,7 +159,7 @@ function formatJs(code) function formatMsg(args) { - if (util.isStr(args[0])) args = substituteStr(args); + args = substituteStr(args); for (let i = 0, len = args.length; i < len; i++) { @@ -153,6 +193,8 @@ function formatMsg(args) function substituteStr(args) { + if (!util.isStr(args[0]) || args.length === 1) return args; + var str = util.escape(args[0]), isInCss = false, newStr = ''; diff --git a/src/Console/Logger.scss b/src/Console/Logger.scss index ae6ec42..0e8c54c 100644 --- a/src/Console/Logger.scss +++ b/src/Console/Logger.scss @@ -64,7 +64,7 @@ &.input { background: #fff; } - &.html { + &.html, &.table { table { width: 100%; background: #fff; diff --git a/test/console.js b/test/console.js index 2972449..3076f66 100644 --- a/test/console.js +++ b/test/console.js @@ -75,11 +75,33 @@ describe('substitution', function () }); }); +describe('table', function () +{ + it('wrong args', function () + { + tool.clear().table('test'); + expect($tool.find('.eruda-table')).not.toContainElement('table'); + }); + + it('basic', function () + { + tool.clear().table([{test: 1}, {test: 2, test2: 3}]); + expect($tool.find('.eruda-table tbody tr')).toHaveLength(2); + expect($tool.find('.eruda-table thead th')).toHaveLength(3); + }); + + it('filter', function () + { + tool.clear().table([{test: 1}, {test: 2, test2: 3}], 'test'); + expect($tool.find('.eruda-table thead th')).toHaveLength(2); + }); +}); + describe('filter', function () { // Test case from https://github.com/liriliri/eruda/issues/14 - /*it('function', function () + it('function', function () { tool.clear().filter(function (log) { @@ -97,5 +119,5 @@ describe('filter', function () }); tool.log(obj); expect($tool.find('.eruda-logs li').length).toEqual(1); - });*/ + }); }); \ No newline at end of file