Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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') {
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.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)
}

Expand Down Expand Up @@ -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,
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')
})
})