Skip to content

Commit 040c940

Browse files
authored
feat: add wordBoundary helper input (#20)
1 parent f35efa4 commit 040c940

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

docs/content/2.getting-started/2.usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ There are a range of helpers that can be used to activate pattern matching, and
3939
| --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
4040
| `charIn`, `charNotIn` | this matches or doesn't match any character in the string provided. |
4141
| `anyOf` | this takes an array of inputs and matches any of them. |
42-
| `char`, `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` | these are helpers for specific RegExp characters. |
43-
| `not` | this can prefix `word`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`. |
42+
| `char`, `word`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` | these are helpers for specific RegExp characters. |
43+
| `not` | this can prefix `word`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`. |
4444
| `maybe` | equivalent to `?` - this marks the input as optional. |
4545
| `oneOrMore` | Equivalent to `+` - this marks the input as repeatable, any number of times but at least once. |
4646
| `exactly` | This escapes a string input to match it exactly. |

src/core/inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const anyOf = <New extends InputSource<V, T>[], V extends string, T exten
2424

2525
export const char = createInput('.')
2626
export const word = createInput('\\w')
27+
export const wordBoundary = createInput('\\b')
2728
export const digit = createInput('\\d')
2829
export const whitespace = createInput('\\s')
2930
export const letter = createInput('[a-zA-Z]')
@@ -33,6 +34,7 @@ export const carriageReturn = createInput('\\r')
3334

3435
export const not = {
3536
word: createInput('\\W'),
37+
wordBoundary: createInput('\\B'),
3638
digit: createInput('\\D'),
3739
whitespace: createInput('\\S'),
3840
letter: createInput('[^a-zA-Z]'),

test/inputs.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
maybe,
1212
oneOrMore,
1313
word,
14+
wordBoundary,
1415
digit,
1516
whitespace,
1617
letter,
@@ -76,6 +77,11 @@ describe('inputs', () => {
7677
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\d/')
7778
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'\\d'>()
7879
})
80+
it('wordBoundary', () => {
81+
const input = wordBoundary
82+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\b/')
83+
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'\\b'>()
84+
})
7985
it('whitespace', () => {
8086
const input = whitespace
8187
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\s/')
@@ -104,6 +110,8 @@ describe('inputs', () => {
104110
it('not', () => {
105111
expect(not.word.toString()).toMatchInlineSnapshot('"\\\\W"')
106112
expectTypeOf(extractRegExp(not.word)).toMatchTypeOf<'\\W'>()
113+
expect(not.wordBoundary.toString()).toMatchInlineSnapshot('"\\\\B"')
114+
expectTypeOf(extractRegExp(not.wordBoundary)).toMatchTypeOf<'\\B'>()
107115
expect(not.digit.toString()).toMatchInlineSnapshot('"\\\\D"')
108116
expectTypeOf(extractRegExp(not.digit)).toMatchTypeOf<'\\D'>()
109117
expect(not.whitespace.toString()).toMatchInlineSnapshot('"\\\\S"')

0 commit comments

Comments
 (0)