@@ -26,11 +26,62 @@ export class RegExpBuilder {
26
26
moreThanEqual,
27
27
} : {
28
28
from : T ;
29
- include ?: IncludeOptions ; // TODO : approve array of include's options
29
+ include ?: null ;
30
+ lessThanEqual ?: number ;
31
+ moreThanEqual ?: number ;
32
+ } ) : T ;
33
+ findOne < T extends string , P extends string > ( {
34
+ from,
35
+ include,
36
+ lessThanEqual,
37
+ moreThanEqual,
38
+ } : {
39
+ from : T ;
40
+ include : { partial : P ; options ?: IncludeOptions < true > } ;
41
+ lessThanEqual ?: number ;
42
+ moreThanEqual ?: number ;
43
+ } ) : `(${T } )(?=(${P } ))`;
44
+ findOne < T extends string , P extends string > ( {
45
+ from,
46
+ include,
47
+ lessThanEqual,
48
+ moreThanEqual,
49
+ } : {
50
+ from : T ;
51
+ include : { partial : P ; options ?: IncludeOptions < false > } ;
52
+ lessThanEqual ?: number ;
53
+ moreThanEqual ?: number ;
54
+ } ) : `(?<=(${P } ))(${T } )`;
55
+ findOne < T extends string , P extends string > ( {
56
+ from,
57
+ include,
58
+ lessThanEqual,
59
+ moreThanEqual,
60
+ } : {
61
+ from : T ;
62
+ include : { partial : P ; options ?: IncludeOptions < boolean > } ;
63
+ lessThanEqual ?: number ;
64
+ moreThanEqual ?: number ;
65
+ } ) : `(?<=(${P } ))(${T } )` | `(${T } )(?=(${P } ))`;
66
+ findOne < T extends string , P extends string > ( {
67
+ from,
68
+ include,
69
+ lessThanEqual,
70
+ moreThanEqual,
71
+ } : {
72
+ from : T ;
73
+ include ?: { partial : P ; options ?: IncludeOptions < boolean > } ;
30
74
lessThanEqual ?: number ;
31
75
moreThanEqual ?: number ;
32
76
} ) {
33
77
let expression = from ;
78
+ if ( include ) {
79
+ if ( include . options . isForehead ) {
80
+ return this . excuteIncludeStatement ( expression , include . partial , { isForehead : true } ) ;
81
+ } else {
82
+ return this . excuteIncludeStatement ( expression , include . partial , { isForehead : false } ) ;
83
+ }
84
+ }
34
85
35
86
return expression ;
36
87
}
@@ -187,31 +238,37 @@ export class RegExpBuilder {
187
238
includeStatement . value = `${ value } ${ includeStatement . value } ` ;
188
239
return this ;
189
240
}
190
- return this . include ( value , options ) ;
241
+
242
+ if ( options . isForehead ) {
243
+ return this . include ( value , { isForehead : true } ) ;
244
+ }
245
+ return this . include ( value , { isForehead : false } ) ;
191
246
}
192
247
193
248
/**
194
249
* @param partial A function returns RegExpBuilder instance to prevent making human error
195
250
* @param options
196
251
*/
197
- include ( partial : ( subBuilder : RegExpBuilder ) => RegExpBuilder , options ?: IncludeOptions ) : this;
198
-
252
+ include ( partial : ( subBuilder : RegExpBuilder ) => RegExpBuilder , options ?: IncludeOptions < true > ) : this;
253
+ include ( partial : ( subBuilder : RegExpBuilder ) => RegExpBuilder , options ?: IncludeOptions < false > ) : this ;
199
254
/**
200
255
* @param partial sub-regular expression builder that returns a string
201
256
* @param options isForehead's default is true. If it's false, first parameter(partial) will set after present expression
202
257
*/
203
- include ( partial : ( subBuilder : RegExpBuilder ) => string , options ?: IncludeOptions ) : this;
258
+ include ( partial : ( subBuilder : RegExpBuilder ) => string , options ?: IncludeOptions < true > ) : this;
259
+ include ( partial : ( subBuilder : RegExpBuilder ) => string , options ?: IncludeOptions < false > ) : this;
204
260
205
261
/**
206
262
* Specifies the string that must be included before and after the current expression.
207
263
* @param partial string (=pattern) or sub-expression / string to be included but not captured.
208
264
* @param options isForehead's default is true. If it's false, first parameter(partial) will set after present expression
209
265
* @returns
210
266
*/
211
- include ( partial : string , options ?: IncludeOptions ) : this;
267
+ include ( partial : string , options ?: IncludeOptions < true > ) : this;
268
+ include ( partial : string , options ?: IncludeOptions < false > ) : this;
212
269
include < T extends string > (
213
270
partial : string | SubExpressionBilder < T > ,
214
- options : IncludeOptions = { isForehead : true } ,
271
+ options : IncludeOptions < boolean > = { isForehead : true } ,
215
272
) {
216
273
const beforeStatus = this . getRawOne ( ) ;
217
274
const value : string = slove ( partial ) ;
@@ -416,7 +473,11 @@ export class RegExpBuilder {
416
473
) ;
417
474
}
418
475
419
- return this . excuteIncludeStatement ( acc , value , options ) ;
476
+ if ( options . isForehead ) {
477
+ return this . excuteIncludeStatement ( acc , value , { isForehead : true } ) ;
478
+ } else {
479
+ return this . excuteIncludeStatement ( acc , value , { isForehead : false } ) ;
480
+ }
420
481
} else if ( name === 'lessThanEqual' ) {
421
482
return this . executeMoreOrLessThanEqual ( acc ) ;
422
483
} else if ( name === 'moreThanEqual' ) {
@@ -443,8 +504,18 @@ export class RegExpBuilder {
443
504
private excuteIncludeStatement < T extends string , P extends string > (
444
505
lastExpression : T ,
445
506
value : P ,
446
- options : IncludeOptions ,
447
- ) {
507
+ options : IncludeOptions < true > ,
508
+ ) : `(?<=(${P } ))(${T } )`;
509
+ private excuteIncludeStatement < T extends string , P extends string > (
510
+ lastExpression : T ,
511
+ value : P ,
512
+ options : IncludeOptions < false > ,
513
+ ) : `(${T } )(?=(${P } ))`;
514
+ private excuteIncludeStatement < T extends string , P extends string > (
515
+ lastExpression : T ,
516
+ value : P ,
517
+ options : IncludeOptions < boolean > ,
518
+ ) : `(?<=(${P } ))(${T } )` | `(${T } )(?=(${P } ))` {
448
519
if ( options . isForehead ) {
449
520
return this . lookbehind ( value , lastExpression ) ;
450
521
} else {
0 commit comments