From df57b8c944e9f4580ed6f234875bf14de6722c90 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 13 Mar 2019 15:32:09 +0100 Subject: [PATCH] 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 --- ghost/release-utils/lib/utils.js | 34 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ghost/release-utils/lib/utils.js b/ghost/release-utils/lib/utils.js index bef0f51c5a..f31a33745d 100644 --- a/ghost/release-utils/lib/utils.js +++ b/ghost/release-utils/lib/utils.js @@ -1,26 +1,32 @@ const emojiRegex = require('emoji-regex'); const _ = require('lodash'); +const timestamp = /^[0-9]{10} /; +const separator = /^\* /; +const hash = /^\[[0-9a-f]{9}\]/; +const url = /^\(https?:\/\/[^)]+\) /; + +const getCommitMessageFromLine = line => line + .replace(timestamp, '') + .replace(separator, '') + .replace(hash, '') + .replace(url, ''); + 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.reduce((emojiLines, currentLine) => { + const commitMessage = getCommitMessageFromLine(currentLine); - return content.map((line) => { - return '* ' + line - .replace(timestamp, '') - .replace(separator, '') - .replace(hash, '') - .replace(url, ''); - }).filter((line) => { - const match = emojiRegex().exec(line); - return match && match.index === 2; - }); + const match = emojiRegex().exec(commitMessage); + if (match && match.index === 0) { + return emojiLines.concat(`* ${commitMessage}`); + } + + return emojiLines; + }, []); }; module.exports.checkMissingOptions = (options = {}, ...requiredFields) => {