Skip to content

Commit 740e245

Browse files
authored
refactor: rename referenceToGroup to referenceTo (#22)
1 parent a18fccb commit 740e245

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ All of the helpers above return an object of type `Input` that can be chained wi
5151

5252
| | |
5353
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54-
| `and` | this adds a new pattern to the current input, or you can use `and.referenceToGroup(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_. |

docs/content/2.getting-started/3.examples.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ createRegExp(
1616
)
1717
// /(?<major>(\d)+)\.(?<minor>(\d)+)(\.(?<patch>(.)+))?/
1818
```
19+
20+
### References to previously captured groups using the group name
21+
22+
```js
23+
import assert from 'node:assert'
24+
import { createRegExp, word, char, oneOrMore } from 'magic-regexp'
25+
26+
const TENET_RE = createRegExp(
27+
word
28+
.as('firstWord')
29+
.and(word.as('secondWord'))
30+
.and(oneOrMore(char))
31+
.and.referenceTo('secondWord')
32+
.and.referenceTo('firstWord')
33+
)
34+
// /(?<firstWord>\w)(?<secondWord>\w)(.)+\k<secondWord>\k<firstWord>/
35+
36+
assert.equal(TENET_RE.test('TEN<==O==>NET'), true)
37+
```
38+

playground/index.mjs

Lines changed: 13 additions & 1 deletion
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 } from 'magic-regexp'
2+
import { createRegExp, exactly, digit, oneOrMore, char, word } from 'magic-regexp'
33

44
// Typed capture groups
55
const ID_RE = createRegExp(exactly('id-').and(digit.times(5).as('id')))
@@ -17,3 +17,15 @@ const SEMVER_RE = createRegExp(
1717
console.log(SEMVER_RE)
1818

1919
assert.equal(createRegExp(exactly('foo/test.js').after('bar/')).test('bar/foo/test.js'), true)
20+
21+
// References to previously captured groups using the group name
22+
const TENET_RE = createRegExp(
23+
word
24+
.as('firstWord')
25+
.and(word.as('secondWord'))
26+
.and(oneOrMore(char))
27+
.and.referenceTo('secondWord')
28+
.and.referenceTo('firstWord')
29+
)
30+
31+
assert.equal(TENET_RE.test('TEN<==O==>NET'), true)

src/core/internal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Input<V extends string, G extends string = never> {
1010
G | (I extends Input<any, infer NewGroups> ? NewGroups : never)
1111
>
1212
/** this adds a new pattern to the current input, with the pattern reference to a named group. */
13-
referenceToGroup: <N extends G>(groupName: N) => Input<`${V}\\k<${N}>`, G>
13+
referenceTo: <N extends G>(groupName: N) => Input<`${V}\\k<${N}>`, G>
1414
}
1515
/** this provides an alternative to the current input */
1616
or: <I extends InputSource<string, any>>(
@@ -58,7 +58,7 @@ export const createInput = <Value extends string, Groups extends string = never>
5858
return {
5959
toString: () => s.toString(),
6060
and: Object.assign((input: InputSource<string, any>) => createInput(`${s}${exactly(input)}`), {
61-
referenceToGroup: (groupName: string) => createInput(`${s}\\k<${groupName}>`),
61+
referenceTo: (groupName: string) => createInput(`${s}\\k<${groupName}>`),
6262
}),
6363
or: input => createInput(`(${s}|${exactly(input)})`),
6464
after: input => createInput(`(?<=${exactly(input)})${s}`),

test/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ describe('inputs', () => {
106106
.as('fooGroup')
107107
.and(exactly('bar').as('barGroup'))
108108
.and('baz')
109-
.and.referenceToGroup('barGroup')
110-
.and.referenceToGroup('fooGroup')
111-
.and.referenceToGroup('barGroup')
109+
.and.referenceTo('barGroup')
110+
.and.referenceTo('fooGroup')
111+
.and.referenceTo('barGroup')
112112

113113
expect('foobarbazbarfoobar'.match(createRegExp(pattern))).toMatchInlineSnapshot(`
114114
[
@@ -117,8 +117,8 @@ describe('inputs', () => {
117117
"bar",
118118
]
119119
`)
120-
expectTypeOf(pattern.and.referenceToGroup).toBeCallableWith('barGroup')
120+
expectTypeOf(pattern.and.referenceTo).toBeCallableWith('barGroup')
121121
// @ts-expect-error
122-
pattern.and.referenceToGroup('bazgroup')
122+
pattern.and.referenceTo('bazgroup')
123123
})
124124
})

test/inputs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ describe('chained inputs', () => {
135135
expect(regexp).toMatchInlineSnapshot('/\\\\\\?test\\\\\\.js/')
136136
expectTypeOf(extractRegExp(val)).toMatchTypeOf<'\\?test\\.js'>()
137137
})
138-
it('and.referenceToGroup', () => {
139-
const val = input.as('namedGroup').and(exactly('any')).and.referenceToGroup('namedGroup')
138+
it('and.referenceTo', () => {
139+
const val = input.as('namedGroup').and(exactly('any')).and.referenceTo('namedGroup')
140140
const regexp = new RegExp(val as any)
141141
expect(regexp).toMatchInlineSnapshot('/\\(\\?<namedGroup>\\\\\\?\\)any\\\\k<namedGroup>/')
142142
expectTypeOf(extractRegExp(val)).toMatchTypeOf<'(?<namedGroup>\\?)any\\k<namedGroup>'>()

0 commit comments

Comments
 (0)