From 27c93117d64acf36a0ec3f3c63b52a2977359e98 Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Fri, 30 Jun 2017 16:23:58 -0400 Subject: [PATCH] used shorter function syntax --- index.js | 27 +++++++++------------------ test/index.js | 6 ++---- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index a3c77cd..8c3c102 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,7 @@ const _ = require('lodash'); module.exports = () => { // initialize arrays for all levels const levels = ['error', 'warn', 'info', 'verbose', 'debug', 'silly'].reduce((acc, level) => { - acc[level] = []; - return acc; + return _.set(acc, level, []); }, {}); // establish the base functions @@ -12,25 +11,20 @@ module.exports = () => { get: (layer) => { // add error(msg), info(msg), etc to getLayer() to form logger functionality return Object.keys(levels).reduce((acc, level) => { - acc[level] = (msg) => levels[level].push(msg); - return acc; - }, { getLayer: () => { return layer; } } ); + return _.set(acc, level, msg => levels[level].push(msg)); + }, { getLayer: () => layer } ); }, // return the supported logging levels - getLevels: () => { - return Object.keys(levels); - }, + getLevels: () => Object.keys(levels), // get all logged messages at a particular level or all levels getMessages: (level, pattern) => { if (levels.hasOwnProperty(level)) { if (_.isRegExp(pattern)) { - return levels[level].filter((message) => { - return message.match(pattern); - }); + return levels[level].filter(message => message.match(pattern)); } // return a clone so it can't be modified externally - return levels[level].slice(); + return _.cloneDeep(levels[level]); } // if no level was specified, return all messages @@ -43,9 +37,8 @@ module.exports = () => { isMessage: (level, pattern) => { if (levels.hasOwnProperty(level)) { if (_.isRegExp(pattern) || _.isString(pattern)) { - return levels[level].some((message) => { - return _.isRegExp(pattern) ? message.match(pattern) : message === pattern; - }); + return levels[level].some(message => + _.isRegExp(pattern) ? message.match(pattern) : message === pattern); } throw 'pattern must be a regexp or string'; @@ -56,9 +49,7 @@ module.exports = () => { }; // return whether there are logged messages at a particular level, optionally matching a pattern - base.hasMessages = (level, pattern) => { - return base.getMessages(level, pattern).length > 0; - } + base.hasMessages = (level, pattern) => !_.isEmpty(base.getMessages(level, pattern)); // dynamically add getErrorMessages(), hasErrorMessages(), isErrorMessage(), etc. return Object.keys(levels).reduce((acc, level) => { diff --git a/test/index.js b/test/index.js index 440a221..003fd6e 100644 --- a/test/index.js +++ b/test/index.js @@ -108,15 +108,13 @@ test('0-parameter getMessages should return clone of entire object', (t) => { }; Object.keys(expected).forEach((level) => { - expected[level].forEach((msg) => { logger[level](msg); }); + expected[level].forEach(msg => logger[level](msg)); }); const actual = rootLogger.getMessages(); // log some more messages to show that getMessages returned a clone - Object.keys(expected).forEach((level) => { - logger[level](`${level} message 3`); - }); + Object.keys(expected).forEach(level => logger[level](`${level} message 3`)); t.deepEquals(actual, expected); t.end();