From 70afa5b88019ca4147706a50b0085193a53b7900 Mon Sep 17 00:00:00 2001 From: Eric NICOLAS Date: Thu, 27 Oct 2022 17:22:49 +0200 Subject: [PATCH] feat: implement `letter.lowercase` and `letter.uppercase` (#77) --- docs/content/2.getting-started/2.usage.md | 4 ++-- src/core/inputs.ts | 10 ++++++++-- test/inputs.test.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/content/2.getting-started/2.usage.md b/docs/content/2.getting-started/2.usage.md index 12275f6f..a85f91c3 100644 --- a/docs/content/2.getting-started/2.usage.md +++ b/docs/content/2.getting-started/2.usage.md @@ -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. | diff --git a/src/core/inputs.ts b/src/core/inputs.ts index f7dd9d75..597e882c 100644 --- a/src/core/inputs.ts +++ b/src/core/inputs.ts @@ -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') @@ -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]'), diff --git a/test/inputs.test.ts b/test/inputs.test.ts index e8e0cbd1..32169a08 100644 --- a/test/inputs.test.ts +++ b/test/inputs.test.ts @@ -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/') @@ -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]"')