From ee79bc5a0460ee8ff11e8f4add9a255bc0617bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jhony=20L=C3=B3pez?= Date: Tue, 10 Jan 2023 12:44:36 -0500 Subject: [PATCH 1/2] add regex flags option --- src/utils.js | 24 ++++++++++++++++++++++-- test/commits.js | 13 +++++++++++++ test/utils.js | 13 +++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index 7fd934f6..544d9ec4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -61,11 +61,16 @@ const encodeHTML = (string) => { } const replaceText = (string, options) => { + let defaultFlags = 'g' + if (!options.replaceText) { return string } + if (options.flags && typeof options.flags === 'string') { + defaultFlags = filterRegexFlags(defaultFlags.concat(String(options.flags))); + } return Object.keys(options.replaceText).reduce((string, pattern) => { - return string.replace(new RegExp(pattern, 'g'), options.replaceText[pattern]) + return string.replace(new RegExp(pattern, defaultFlags), options.replaceText[pattern]) }, string) } @@ -99,6 +104,20 @@ const readJson = async (path) => { return JSON.parse(await readFile(path)) } +const filterRegexFlags = (flags) => { + const finalFlags = new Set() + let mainFlags = ["g", "i", "m", "d", "s", "u", "y"] + + flags.split("").forEach((item) => { + if (mainFlags.includes(item)) { + finalFlags.add(item) + } + }) + + return Array.from(finalFlags).join("") +} + + module.exports = { updateLog, formatBytes, @@ -112,5 +131,6 @@ module.exports = { readFile, writeFile, fileExists, - readJson + readJson, + filterRegexFlags } diff --git a/test/commits.js b/test/commits.js index 60f1bee2..f0b8bba0 100644 --- a/test/commits.js +++ b/test/commits.js @@ -65,6 +65,19 @@ describe('parseCommits', () => { const result = parseCommits(gitLog, options) expect(result.filter(c => c.subject === 'Some **BREAKING** change')).to.have.length(1) }) + + it('supports regex flags option', async () => { + const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt')) + const options = { + replaceText: { + BREAKING: '**BREAKING**' + }, + flags: "i", + ...remotes.github + } + const result = parseCommits(gitLog, options) + expect(result.filter(c => c.subject === 'Some **BREAKING** change')).to.have.length(1) + }) }) describe('getFixes', () => { diff --git a/test/utils.js b/test/utils.js index 7ea15ca4..c086a7bf 100644 --- a/test/utils.js +++ b/test/utils.js @@ -10,6 +10,7 @@ const { writeFile, fileExists, readJson, + filterRegexFlags, __Rewire__: mock, __ResetDependency__: unmock } = require('../src/utils') @@ -103,3 +104,15 @@ describe('readJson', () => { unmock('fs') }) }) + +describe('filterRegexFlags', () => { + it('filter available input regex flags', () => { + expect(filterRegexFlags('i')).to.equal('i') + expect(filterRegexFlags('gi')).to.equal('gi') + }) + + it('filter unavailable input regex flags', () => { + expect(filterRegexFlags('ixyz')).to.equal('iy') + expect(filterRegexFlags('gixyzm')).to.equal('giym') + }) +}) From 3f1b0edd201113e3dba2ebb2ddc81e0ba2aa798d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jhony=20L=C3=B3pez?= Date: Tue, 10 Jan 2023 15:56:17 -0500 Subject: [PATCH 2/2] apply review comments to filter regex function --- src/utils.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils.js b/src/utils.js index 544d9ec4..122a3e48 100644 --- a/src/utils.js +++ b/src/utils.js @@ -3,6 +3,7 @@ const fs = require('fs') const { spawn } = require('child_process') const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] +const REGEX_FLAGS = new Set(["g", "i", "m", "d", "s", "u", "y"]) const updateLog = (string, clearLine = true) => { if (clearLine) { @@ -67,7 +68,7 @@ const replaceText = (string, options) => { return string } if (options.flags && typeof options.flags === 'string') { - defaultFlags = filterRegexFlags(defaultFlags.concat(String(options.flags))); + defaultFlags = filterRegexFlags(defaultFlags + options.flags); } return Object.keys(options.replaceText).reduce((string, pattern) => { return string.replace(new RegExp(pattern, defaultFlags), options.replaceText[pattern]) @@ -106,10 +107,9 @@ const readJson = async (path) => { const filterRegexFlags = (flags) => { const finalFlags = new Set() - let mainFlags = ["g", "i", "m", "d", "s", "u", "y"] flags.split("").forEach((item) => { - if (mainFlags.includes(item)) { + if (REGEX_FLAGS.has(item)) { finalFlags.add(item) } })