Skip to content

Commit

Permalink
Fix character class escaping an ending dash
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Aug 20, 2023
1 parent 56e6639 commit 5a48f3b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/modifiers/CharacterClassModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ function escapeForCharClass(option: string): string {
if (option.startsWith('-')) {
option = '\\-' + option.substring(1);
}
if (option.endsWith('-')) {
option = option.substring(0, option.length - 1) + '\\-';
}
let charEscaped = false;
for (let i = 0; i < option.length; i++) {
const char = option[i];
if (charEscaped) {
charEscaped = false;
} else if (char === '\\') {
} else if (char === '\\' || char === '-') {
if (i === option.length - 1) {
// escape backslash at the end of the string
option = option + '\\';
option = option.substring(0, option.length - 1) + '\\' + char;
break;
} else {
charEscaped = true;
Expand Down
2 changes: 2 additions & 0 deletions test/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,8 @@ describe('charIn', () => {
expect(charIn`\\\\`.toString()).toBe('[\\\\]');
expect(charIn`a-z\\``s`.toString()).toBe('[a-z\\\\s]');
expect(charIn`a-z``\\s`.toString()).toBe('[a-z\\s]');
expect(charIn`ab-``z`.toString()).toBe('[ab\\-z]');
expect(charIn`ab\\-``z`.toString()).toBe('[ab\\-z]');
});
it('is chainable and quantifiable', () => {
expect(charIn`a``b``c`.char.toString()).toBe('[abc].');
Expand Down

0 comments on commit 5a48f3b

Please sign in to comment.