Skip to content

Commit 033ee5c

Browse files
authored
fix!: rename word to wordChar and add semantic word helper (#23)
BREAKING CHANGE: `word` has been renamed to `wordChar`
1 parent 740e245 commit 033ee5c

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ By default, all helpers from `magic-regexp` assume that input that is passed sho
3535

3636
There are a range of helpers that can be used to activate pattern matching, and they can be chained. Each one of these returns an object of type `Input` that can be passed directly to `new RegExp`, `createRegExp`, to another helper or chained to produce more complex patterns.
3737

38-
| | |
39-
| --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
40-
| `charIn`, `charNotIn` | this matches or doesn't match any character in the string provided. |
41-
| `anyOf` | this takes an array of inputs and matches any of them. |
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)`. |
44-
| `maybe` | equivalent to `?` - this marks the input as optional. |
45-
| `oneOrMore` | Equivalent to `+` - this marks the input as repeatable, any number of times but at least once. |
46-
| `exactly` | This escapes a string input to match it exactly. |
38+
| | |
39+
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
40+
| `charIn`, `charNotIn` | this matches or doesn't match any character in the string provided. |
41+
| `anyOf` | this takes an array of inputs and matches any of them. |
42+
| `char`, `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` and `carriageReturn` | these are helpers for specific RegExp characters. |
43+
| `not` | this can prefix `word`, `wordChar`, `wordBoundary`, `digit`, `whitespace`, `letter`, `tab`, `linefeed` or `carriageReturn`. For example `createRegExp(not.letter)`. |
44+
| `maybe` | equivalent to `?` - this marks the input as optional. |
45+
| `oneOrMore` | Equivalent to `+` - this marks the input as repeatable, any number of times but at least once. |
46+
| `exactly` | This escapes a string input to match it exactly. |
4747

4848
## Chaining inputs
4949

5050
All of the helpers above return an object of type `Input` that can be chained with the following helpers:
5151

5252
| | |
5353
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54-
| `and` | this adds a new pattern to the current input, or you can use `and.referenceTo(groupName)` to adds a new pattern referencing to a named group. |
54+
| `and` | this adds a new pattern to the current input, or you can use `and.referenceTo(groupName)` to adds a new pattern referencing to a named group. |
5555
| `or` | this provides an alternative to the current input. |
5656
| `after`, `before`, `notAfter` and `notBefore` | these activate positive/negative lookahead/lookbehinds. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari). |
5757
| `times` | this is a function you can call directly to repeat the previous pattern an exact number of times, or you can use `times.between(min, max)` to specify a range, `times.atLeast(num)` to indicate it must repeat x times or `times.any()` to indicate it can repeat any number of times, _including none_. |

playground/index.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert'
2-
import { createRegExp, exactly, digit, oneOrMore, char, word } from 'magic-regexp'
2+
import { createRegExp, exactly, digit, oneOrMore, char, wordChar } from 'magic-regexp'
33

44
// Typed capture groups
55
const ID_RE = createRegExp(exactly('id-').and(digit.times(5).as('id')))
@@ -20,12 +20,12 @@ assert.equal(createRegExp(exactly('foo/test.js').after('bar/')).test('bar/foo/te
2020

2121
// References to previously captured groups using the group name
2222
const TENET_RE = createRegExp(
23-
word
24-
.as('firstWord')
25-
.and(word.as('secondWord'))
23+
wordChar
24+
.as('firstChar')
25+
.and(wordChar.as('secondChar'))
2626
.and(oneOrMore(char))
27-
.and.referenceTo('secondWord')
28-
.and.referenceTo('firstWord')
27+
.and.referenceTo('secondChar')
28+
.and.referenceTo('firstChar')
2929
)
3030

3131
assert.equal(TENET_RE.test('TEN<==O==>NET'), true)

src/core/inputs.ts

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

2525
export const char = createInput('.')
26-
export const word = createInput('\\w')
26+
export const word = createInput('\\b\\w+\\b')
27+
export const wordChar = createInput('\\w')
2728
export const wordBoundary = createInput('\\b')
2829
export const digit = createInput('\\d')
2930
export const whitespace = createInput('\\s')
@@ -33,7 +34,7 @@ export const linefeed = createInput('\\n')
3334
export const carriageReturn = createInput('\\r')
3435

3536
export const not = {
36-
word: createInput('\\W'),
37+
wordChar: createInput('\\W'),
3738
wordBoundary: createInput('\\B'),
3839
digit: createInput('\\D'),
3940
whitespace: createInput('\\S'),

test/inputs.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
maybe,
1212
oneOrMore,
1313
word,
14+
wordChar,
1415
wordBoundary,
1516
digit,
1617
whitespace,
@@ -69,6 +70,11 @@ describe('inputs', () => {
6970
})
7071
it('word', () => {
7172
const input = word
73+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\b\\\\w\\+\\\\b/')
74+
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'\\b\\w+\\b'>()
75+
})
76+
it('wordChar', () => {
77+
const input = wordChar
7278
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\\\w/')
7379
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'\\w'>()
7480
})
@@ -108,8 +114,8 @@ describe('inputs', () => {
108114
expectTypeOf(extractRegExp(input)).toMatchTypeOf<'\\r'>()
109115
})
110116
it('not', () => {
111-
expect(not.word.toString()).toMatchInlineSnapshot('"\\\\W"')
112-
expectTypeOf(extractRegExp(not.word)).toMatchTypeOf<'\\W'>()
117+
expect(not.wordChar.toString()).toMatchInlineSnapshot('"\\\\W"')
118+
expectTypeOf(extractRegExp(not.wordChar)).toMatchTypeOf<'\\W'>()
113119
expect(not.wordBoundary.toString()).toMatchInlineSnapshot('"\\\\B"')
114120
expectTypeOf(extractRegExp(not.wordBoundary)).toMatchTypeOf<'\\B'>()
115121
expect(not.digit.toString()).toMatchInlineSnapshot('"\\\\D"')

0 commit comments

Comments
 (0)