Skip to content

Commit

Permalink
fix trim throwing regex error (sodiray#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
apstanisic authored Jun 26, 2023
1 parent 9266717 commit 9981032
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const trim = (
charsToTrim: string = ' '
) => {
if (!str) return ''
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, 'g')
const toTrim = charsToTrim.replace(/[\W]{1}/g, '\\$&')
const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, 'g')
return str.replace(regex, '')
}
4 changes: 4 additions & 0 deletions src/tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,9 @@ describe('string module', () => {
assert.equal(_.trim('//repos////', '/'), 'repos')
assert.equal(_.trim('/repos/:owner/:repo/', '/'), 'repos/:owner/:repo')
})

test('handles when char to trim is special case in regex', () => {
assert.equal(_.trim('_- hello_- ', '_- '), 'hello')
})
})
})

0 comments on commit 9981032

Please sign in to comment.