Skip to content

Commit

Permalink
feat: implement letter.lowercase and letter.uppercase (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccjmne authored Oct 27, 2022
1 parent 736c4b9 commit 70afa5b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/content/2.getting-started/2.usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ There are a range of helpers that can be used to activate pattern matching, and
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `charIn`, `charNotIn` | this matches or doesn't match any character in the string provided. |
| `anyOf` | this takes an array of inputs and matches any of them. |
| `char`, `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` | these are helpers for specific RegExp characters. |
| `not` | this can prefix `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`. |
| `char`, `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `letter.lowercase`, `letter.uppercase`, `tab`, `linefeed` and `carriageReturn` | these are helpers for specific RegExp characters. |
| `not` | this can prefix `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `letter.lowercase`, `letter.uppercase`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`. |
| `maybe` | equivalent to `?` - this marks the input as optional. |
| `oneOrMore` | Equivalent to `+` - this marks the input as repeatable, any number of times but at least once. |
| `exactly` | This escapes a string input to match it exactly. |
Expand Down
10 changes: 8 additions & 2 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export const wordChar = createInput('\\w')
export const wordBoundary = createInput('\\b')
export const digit = createInput('\\d')
export const whitespace = createInput('\\s')
export const letter = createInput('[a-zA-Z]')
export const letter = Object.assign(createInput('[a-zA-Z]'), {
lowercase: createInput('[a-z]'),
uppercase: createInput('[A-Z]'),
})
export const tab = createInput('\\t')
export const linefeed = createInput('\\n')
export const carriageReturn = createInput('\\r')
Expand All @@ -47,7 +50,10 @@ export const not = {
wordBoundary: createInput('\\B'),
digit: createInput('\\D'),
whitespace: createInput('\\S'),
letter: createInput('[^a-zA-Z]'),
letter: Object.assign(createInput('[^a-zA-Z]'), {
lowercase: createInput('[^a-z]'),
uppercase: createInput('[^A-Z]'),
}),
tab: createInput('[^\\t]'),
linefeed: createInput('[^\\n]'),
carriageReturn: createInput('[^\\r]'),
Expand Down
14 changes: 14 additions & 0 deletions test/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ describe('inputs', () => {
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[a-zA-Z\\]/')
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[a-zA-Z]'>()
})
it('letter.lowercase', () => {
const input = letter.lowercase
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[a-z\\]/')
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[a-z]'>()
})
it('letter.uppercase', () => {
const input = letter.uppercase
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[A-Z\\]/')
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[A-Z]'>()
})
it('tab', () => {
const input = tab
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\t/')
Expand All @@ -146,6 +156,10 @@ describe('inputs', () => {
expectTypeOf(extractRegExp(not.whitespace)).toEqualTypeOf<'\\S'>()
expect(not.letter.toString()).toMatchInlineSnapshot('"[^a-zA-Z]"')
expectTypeOf(extractRegExp(not.letter)).toEqualTypeOf<'[^a-zA-Z]'>()
expect(not.letter.lowercase.toString()).toMatchInlineSnapshot('"[^a-z]"')
expectTypeOf(extractRegExp(not.letter.lowercase)).toEqualTypeOf<'[^a-z]'>()
expect(not.letter.uppercase.toString()).toMatchInlineSnapshot('"[^A-Z]"')
expectTypeOf(extractRegExp(not.letter.uppercase)).toEqualTypeOf<'[^A-Z]'>()
expect(not.tab.toString()).toMatchInlineSnapshot('"[^\\\\t]"')
expectTypeOf(extractRegExp(not.tab)).toEqualTypeOf<'[^\\t]'>()
expect(not.linefeed.toString()).toMatchInlineSnapshot('"[^\\\\n]"')
Expand Down

1 comment on commit 70afa5b

@vercel
Copy link

@vercel vercel bot commented on 70afa5b Oct 27, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.