Skip to content

add regex flags option #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -61,11 +62,16 @@ const encodeHTML = (string) => {
}

const replaceText = (string, options) => {
let defaultFlags = 'g'

if (!options.replaceText) {
return string
}
if (options.flags && typeof options.flags === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this throw if flags is not a string or undefined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, keep the default behavior if those conditions are not met

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would we want to silently ignore bad input?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cookpete what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I wanted to keep the default behavior, what do you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone is passing a flags option, they're already at risk of breakage if they're passing a string, so I don't think we need to worry about that.

defaultFlags = filterRegexFlags(defaultFlags + 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)
}

Expand Down Expand Up @@ -99,6 +105,19 @@ const readJson = async (path) => {
return JSON.parse(await readFile(path))
}

const filterRegexFlags = (flags) => {
const finalFlags = new Set()

flags.split("").forEach((item) => {
if (REGEX_FLAGS.has(item)) {
finalFlags.add(item)
}
})

return Array.from(finalFlags).join("")
}


module.exports = {
updateLog,
formatBytes,
Expand All @@ -112,5 +131,6 @@ module.exports = {
readFile,
writeFile,
fileExists,
readJson
readJson,
filterRegexFlags
}
13 changes: 13 additions & 0 deletions test/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
13 changes: 13 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
writeFile,
fileExists,
readJson,
filterRegexFlags,
__Rewire__: mock,
__ResetDependency__: unmock
} = require('../src/utils')
Expand Down Expand Up @@ -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')
})
})