Skip to content

fix: char class ^- edge case #42

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

Merged
merged 2 commits into from
Jan 2, 2024
Merged
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
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ This library allows users to create regular expressions in a structured way, mak
const hexColor = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;

// After
const hexDigit = charClass(charRange('a', 'f'), charRange('A', 'F'), charRange('0', '9'));
const hexDigit = charClass(
charRange('a', 'f'), //
charRange('A', 'F'),
charRange('0', '9'),
);

// prettier-ignore
const hexColor = buildRegex(
startOfString,
optionally('#'),
capture(
choiceOf(
repeat({ count: 6 }, hexDigit),
repeat({ count: 3 }, hexDigit),
)
repeat({ count: 6 }, hexDigit), // #rrggbb
repeat({ count: 3 }, hexDigit), // #rgb
),
),
endOfString,
);
Expand Down Expand Up @@ -73,11 +76,11 @@ const currencyAmount = buildRegex([
choiceOf(
'$',
'€',
repeat({ count: 3 }, charRange('A', 'Z')) // ISO currency code
repeat({ count: 3 }, charRange('A', 'Z')), // ISO currency code
),
capture(
oneOrMore(digit), // Integer part
optionally(['.', repeat({ count: 2 }, digit)]) // Fractional part
optionally(['.', repeat({ count: 2 }, digit)]), // Fractional part
),
]);
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"quoteProps": "consistent",
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"trailingComma": "all",
"useTabs": false
},
"react-native-builder-bob": {
Expand Down
6 changes: 6 additions & 0 deletions src/components/__tests__/character-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ test('`anyOf` moves hyphen to the last position', () => {
expect(anyOf('a-bc')).toHavePattern(/[abc-]/);
});

test('`anyOf` edge case caret and hyphen', () => {
expect(anyOf('^-')).toHavePattern(/[\^-]/);
expect(anyOf('-^')).toHavePattern(/[\^-]/);
expect(anyOf('-^a')).toHavePattern(/[a^-]/);
});

test('`anyOf` throws on empty text', () => {
expect(() => anyOf('')).toThrowErrorMatchingInlineSnapshot(
`"\`anyOf\` should received at least one character"`
Expand Down
8 changes: 6 additions & 2 deletions src/components/character-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ function encodeCharacterClass(this: CharacterClass): EncodeOutput {
// first (or last) place in order to treat it as hyphen character and not a range.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types
const hyphen = this.chars.includes('-') ? '-' : '';
const otherChars = this.chars.filter((c) => c !== '-').join('');
const caret = this.chars.includes('^') ? '^' : '';
const otherChars = this.chars.filter((c) => c !== '-' && c !== '^').join('');
const ranges = this.ranges.map(({ start, end }) => `${start}-${end}`).join('');
const isInverted = this.isInverted ? '^' : '';

let pattern = `[${isInverted}${ranges}${otherChars}${caret}${hyphen}]`;
if (pattern === '[^-]') pattern = '[\\^-]';

return {
precedence: 'atom',
pattern: `[${isInverted}${ranges}${otherChars}${hyphen}]`,
pattern,
};
}

Expand Down