2019-03-12 23:53:32 +03:00
|
|
|
const emojiRegex = require('emoji-regex');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-03-13 17:32:09 +03:00
|
|
|
const timestamp = /^[0-9]{10} /;
|
|
|
|
const separator = /^\* /;
|
2019-03-14 18:51:18 +03:00
|
|
|
const hash = /^\[[0-9a-f]{0,10}\]/;
|
2019-03-13 17:32:09 +03:00
|
|
|
const url = /^\(https?:\/\/[^)]+\) /;
|
|
|
|
|
|
|
|
const getCommitMessageFromLine = line => line
|
|
|
|
.replace(timestamp, '')
|
|
|
|
.replace(separator, '')
|
|
|
|
.replace(hash, '')
|
|
|
|
.replace(url, '');
|
|
|
|
|
2020-03-09 23:03:18 +03:00
|
|
|
const emojiOrder = ['💡', '🐛', '🎨', '💄', '✨', '🔒'];
|
2020-02-10 16:31:52 +03:00
|
|
|
|
2019-03-12 23:53:32 +03:00
|
|
|
module.exports.filterEmojiCommits = (content) => {
|
|
|
|
if (!_.isArray(content)) {
|
|
|
|
throw new Error('Expected array of strings.');
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:32:09 +03:00
|
|
|
return content.reduce((emojiLines, currentLine) => {
|
|
|
|
const commitMessage = getCommitMessageFromLine(currentLine);
|
|
|
|
const match = emojiRegex().exec(commitMessage);
|
2019-03-14 18:51:18 +03:00
|
|
|
|
2019-03-13 17:32:09 +03:00
|
|
|
if (match && match.index === 0) {
|
|
|
|
return emojiLines.concat(`* ${commitMessage}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return emojiLines;
|
|
|
|
}, []);
|
2019-03-12 23:53:32 +03:00
|
|
|
};
|
|
|
|
|
2020-02-10 16:31:52 +03:00
|
|
|
module.exports.sortByEmoji = (content) => {
|
|
|
|
if (!_.isArray(content)) {
|
|
|
|
throw new Error('Expected array of strings.');
|
|
|
|
}
|
|
|
|
|
|
|
|
content.sort((a, b) => {
|
|
|
|
let firstEmoji = [...a][2];
|
|
|
|
let secondEmoji = [...b][2];
|
|
|
|
|
|
|
|
let firstEmojiIndex = _.indexOf(emojiOrder, firstEmoji);
|
|
|
|
let secondEmojiIndex = _.indexOf(emojiOrder, secondEmoji);
|
|
|
|
|
|
|
|
return secondEmojiIndex - firstEmojiIndex;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-12 23:53:32 +03:00
|
|
|
module.exports.checkMissingOptions = (options = {}, ...requiredFields) => {
|
|
|
|
const missing = requiredFields.filter((requiredField) => {
|
|
|
|
return !_.get(options, requiredField);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missing.length) {
|
|
|
|
throw new Error(`Missing options: ${missing.join(', ')}`);
|
|
|
|
}
|
|
|
|
};
|