Skip to content

Commit

Permalink
fix: Merge pull request #7 from pelias/use-shorter-syntax
Browse files Browse the repository at this point in the history
used shorter function syntax
  • Loading branch information
trescube authored Jul 5, 2017
2 parents 84f6f86 + 27c9311 commit ca50aa2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
27 changes: 9 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,28 @@ 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
const base = {
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
Expand All @@ -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';
Expand All @@ -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) => {
Expand Down
6 changes: 2 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ca50aa2

Please sign in to comment.