Refactored emoji filtering to only loop once

Small perf improvment, also easier to read now because we can use 0
index which is more obvious as to what we're doing
This commit is contained in:
Fabien O'Carroll 2019-03-13 15:32:09 +01:00
parent c681b8e9be
commit df57b8c944

View File

@ -1,26 +1,32 @@
const emojiRegex = require('emoji-regex');
const _ = require('lodash');
module.exports.filterEmojiCommits = (content) => {
if (!_.isArray(content)) {
throw new Error('Expected array of strings.');
}
const timestamp = /^[0-9]{10} /;
const separator = /^\* /;
const hash = /^\[[0-9a-f]{9}\]/;
const url = /^\(https?:\/\/[^)]+\) /;
return content.map((line) => {
return '* ' + line
const getCommitMessageFromLine = line => line
.replace(timestamp, '')
.replace(separator, '')
.replace(hash, '')
.replace(url, '');
}).filter((line) => {
const match = emojiRegex().exec(line);
return match && match.index === 2;
});
module.exports.filterEmojiCommits = (content) => {
if (!_.isArray(content)) {
throw new Error('Expected array of strings.');
}
return content.reduce((emojiLines, currentLine) => {
const commitMessage = getCommitMessageFromLine(currentLine);
const match = emojiRegex().exec(commitMessage);
if (match && match.index === 0) {
return emojiLines.concat(`* ${commitMessage}`);
}
return emojiLines;
}, []);
};
module.exports.checkMissingOptions = (options = {}, ...requiredFields) => {