@@ -7,13 +7,13 @@ const GROUPED_AS_REPLACE_RE = /^(?:\(\?:(.+)\)|(\(?.+\)?))$/
77const GROUPED_REPLACE_RE = / ^ (?: \( \? : ( .+ ) \) ( [ ? + * ] | { [ \d , ] + } ) ? | ( .+ ) ) $ /
88
99export interface Input <
10- V extends string ,
10+ in V extends string ,
1111 G extends string = never ,
1212 C extends ( string | undefined ) [ ] = [ ]
1313> {
14+ /** this adds a new pattern to the current input */
1415 and : {
15- /** this adds a new pattern to the current input */
16- < I extends InputSource < string , any > > ( input : I ) : Input <
16+ < I extends InputSource > ( input : I ) : Input <
1717 `${V } ${GetValue < I > } `,
1818 G | ( I extends Input < any , infer NewGroups > ? NewGroups : never ) ,
1919 [ ...C , ...GetCapturedGroupsArr < I > ]
@@ -22,63 +22,59 @@ export interface Input<
2222 referenceTo : < N extends G > ( groupName : N ) => Input < `${V } \\k < ${N } >`, G , C >
2323 }
2424 /** this provides an alternative to the current input */
25- or : < I extends InputSource < string , any > > (
25+ or : < I extends InputSource > (
2626 input : I
2727 ) => Input <
2828 `(?:${V } |${GetValue < I > } )`,
2929 G | ( I extends Input < any , infer NewGroups > ? NewGroups : never ) ,
3030 [ ...C , ...GetCapturedGroupsArr < I > ]
3131 >
3232 /** this is a positive lookbehind. 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) */
33- after : < I extends InputSource < string > > (
33+ after : < I extends InputSource > (
3434 input : I
3535 ) => Input < `(?<=${GetValue < I > } )${V } `, G , [ ...GetCapturedGroupsArr < I > , ...C ] >
3636 /** this is a positive lookahead */
37- before : < I extends InputSource < string > > (
37+ before : < I extends InputSource > (
3838 input : I
3939 ) => Input < `${V } (?=${GetValue < I > } )`, G , [ ...C , ...GetCapturedGroupsArr < I > ] >
4040 /** these is a negative lookbehind. 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) */
41- notAfter : < I extends InputSource < string > > (
41+ notAfter : < I extends InputSource > (
4242 input : I
4343 ) => Input < `(?<!${GetValue < I > } )${V } `, G , [ ...GetCapturedGroupsArr < I , true > , ...C ] >
4444 /** this is a negative lookahead */
45- notBefore : < I extends InputSource < string > > (
45+ notBefore : < I extends InputSource > (
4646 input : I
4747 ) => Input < `${V } (?!${GetValue < I > } )`, G , [ ...C , ...GetCapturedGroupsArr < I , true > ] >
48+ /** repeat the previous pattern an exact number of times */
4849 times : {
49- /** repeat the previous pattern an exact number of times */
50- < N extends number > ( number : N ) : IfUnwrapped <
51- V ,
52- Input < `(?:${V } ){${N } }`, G , C > ,
53- Input < `${V } {${N } }`, G , C >
54- >
50+ < N extends number > ( number : N ) : Input < IfUnwrapped < V , `(?:${V } ){${N } }`, `${V } {${N } }`> , G , C >
5551 /** specify that the expression can repeat any number of times, _including none_ */
56- any : ( ) => IfUnwrapped < V , Input < `(?:${V } )*`, G > , Input < `${V } *`, G , C > >
52+ any : ( ) => Input < IfUnwrapped < V , `(?:${V } )*`, `${V } *`> , G , C >
5753 /** specify that the expression must occur at least x times */
5854 atLeast : < N extends number > (
5955 number : N
60- ) => IfUnwrapped < V , Input < `(?:${V } ){${N } ,}`, G > , Input < `${V } {${N } ,}`, G , C > >
56+ ) => Input < IfUnwrapped < V , `(?:${V } ){${N } ,}`, `${V } {${N } ,}`> , G , C >
6157 /** specify a range of times to repeat the previous pattern */
6258 between : < Min extends number , Max extends number > (
6359 min : Min ,
6460 max : Max
65- ) => IfUnwrapped < V , Input < `(?:${V } ){${Min } ,${Max } }`, G , C > , Input < `${V } {${Min } ,${Max } }`, G , C > >
61+ ) => Input < IfUnwrapped < V , `(?:${V } ){${Min } ,${Max } }`, `${V } {${Min } ,${Max } }`> , G , C >
6662 }
6763 /** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()`. Alias for `groupedAs` */
6864 as : < K extends string > (
6965 key : K
7066 ) => Input <
71- `(?<${ K } >${ V extends `(?:${infer S } )` ? S : V } )`,
67+ V extends `(?:${infer S } )` ? `(?<${ K } >${ S } )` : `(?<${ K } >${ V } )`,
7268 G | K ,
73- [ `(?<${ K } >${ V extends `(?:${infer S } )` ? S : V } )`, ...C ]
69+ [ V extends `(?:${infer S } )` ? `(?<${ K } >${ S } )` : `(?<${ K } >${ V } )`, ...C ]
7470 >
7571 /** this defines the entire input so far as a named capture group. You will get type safety when using the resulting RegExp with `String.match()` */
7672 groupedAs : < K extends string > (
7773 key : K
7874 ) => Input <
79- `(?<${ K } >${ V extends `(?:${infer S } )` ? S : V } )`,
75+ V extends `(?:${infer S } )` ? `(?<${ K } >${ S } )` : `(?<${ K } >${ V } )`,
8076 G | K ,
81- [ `(?<${ K } >${ V extends `(?:${infer S } )` ? S : V } )`, ...C ]
77+ [ V extends `(?:${infer S } )` ? `(?<${ K } >${ S } )` : `(?<${ K } >${ V } )`, ...C ]
8278 >
8379 /** this capture the entire input so far as an anonymous group */
8480 grouped : ( ) => Input <
@@ -92,7 +88,7 @@ export interface Input<
9288 lineEnd : ( ) => Input < `${V } $`, G , C >
9389 }
9490 /** this allows you to mark the input so far as optional */
95- optionally : ( ) => IfUnwrapped < V , Input < `(?:${V } )?`, G , C > , Input < `${V } ?`, G , C > >
91+ optionally : ( ) => Input < IfUnwrapped < V , `(?:${V } )?`, `${V } ?`> , G , C >
9692 toString : ( ) => string
9793}
9894
@@ -108,7 +104,7 @@ export const createInput = <
108104
109105 return {
110106 toString : ( ) => s . toString ( ) ,
111- and : Object . assign ( ( input : InputSource < string , any > ) => createInput ( `${ s } ${ exactly ( input ) } ` ) , {
107+ and : Object . assign ( ( input : InputSource ) => createInput ( `${ s } ${ exactly ( input ) } ` ) , {
112108 referenceTo : ( groupName : string ) => createInput ( `${ s } \\k<${ groupName } >` ) ,
113109 } ) ,
114110 or : input => createInput ( `(?:${ s } |${ exactly ( input ) } )` ) ,
0 commit comments