Skip to content

Commit 56e9efb

Browse files
committed
feat : findOne method added / incomplete
1 parent 440ab71 commit 56e9efb

File tree

5 files changed

+121
-19
lines changed

5 files changed

+121
-19
lines changed

Diff for: README.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ const regExp = new RegExpBuilder().from('initialValue').getOne(); // RegExp, sam
3838
If you want to create a more complex pattern, you can also write the `sub-expression` below.
3939
All methods to be implemented in the future will be made all sub-expression possible.
4040

41-
## getOne
41+
## getOne method
4242

4343
Returns regexp instances based on methods written so far.
4444

45-
## getRawOne
45+
## getRawOne method
4646

4747
Returns string based on methods written so far.
4848
You can use this as the first parameter of the regular expression constructor.
4949

50-
## include & andInclude
50+
## include & andInclude method
5151

5252
```typescript
5353
/**
@@ -83,11 +83,11 @@ The options means where the string of this inclusion relationship should be loca
8383

8484
The include method should only be used once per builder. If you want to use a second inclusion, sconsider and include.
8585

86-
## isOptinonal (to be created)
86+
## isOptinonal method (incomplete)
8787

88-
## whatever (to be created)
88+
## whatever method (incomplete)
8989

90-
## and
90+
## and method
9191

9292
```typescript
9393
const leftHand = new RegExpBuilder('Hand').and('left', { isForehead: true }).getRawOne();
@@ -192,6 +192,23 @@ it('1. string "cat" but lessThanEqual 3', () => {
192192
});
193193
```
194194

195+
# findOne method (incomplete)
196+
197+
```typescript
198+
it('from method set initial value & include other', () => {
199+
const test = new RegExpBuilder().findOne({
200+
from: 'test',
201+
include: { partial: 'left', options: {} },
202+
});
203+
204+
expect(test).toBe('(test)(?=(left))');
205+
});
206+
```
207+
208+
The test variable is deduced from type to `'(test)(?=(left))'`, without having to check whether it is `'(test)(?=(left))'`.
209+
Therefore, the findOne method is useful when you immediately deduce and write a value, even if it may be less free than other RegExpBuilder methods.
210+
now from and include method options available.
211+
195212
# order by execution
196213

197214
1. from ( or constructor )

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "regexp-manager",
3-
"version": "0.8.2",
3+
"version": "0.9.0",
44
"description": "regexp builder for node.js developer",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

Diff for: src/regexp-builder.ts

+81-10
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,62 @@ export class RegExpBuilder {
2626
moreThanEqual,
2727
}: {
2828
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> };
3074
lessThanEqual?: number;
3175
moreThanEqual?: number;
3276
}) {
3377
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+
}
3485

3586
return expression;
3687
}
@@ -187,31 +238,37 @@ export class RegExpBuilder {
187238
includeStatement.value = `${value}${includeStatement.value}`;
188239
return this;
189240
}
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 });
191246
}
192247

193248
/**
194249
* @param partial A function returns RegExpBuilder instance to prevent making human error
195250
* @param options
196251
*/
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;
199254
/**
200255
* @param partial sub-regular expression builder that returns a string
201256
* @param options isForehead's default is true. If it's false, first parameter(partial) will set after present expression
202257
*/
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;
204260

205261
/**
206262
* Specifies the string that must be included before and after the current expression.
207263
* @param partial string (=pattern) or sub-expression / string to be included but not captured.
208264
* @param options isForehead's default is true. If it's false, first parameter(partial) will set after present expression
209265
* @returns
210266
*/
211-
include(partial: string, options?: IncludeOptions): this;
267+
include(partial: string, options?: IncludeOptions<true>): this;
268+
include(partial: string, options?: IncludeOptions<false>): this;
212269
include<T extends string>(
213270
partial: string | SubExpressionBilder<T>,
214-
options: IncludeOptions = { isForehead: true },
271+
options: IncludeOptions<boolean> = { isForehead: true },
215272
) {
216273
const beforeStatus = this.getRawOne();
217274
const value: string = slove(partial);
@@ -416,7 +473,11 @@ export class RegExpBuilder {
416473
);
417474
}
418475

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+
}
420481
} else if (name === 'lessThanEqual') {
421482
return this.executeMoreOrLessThanEqual(acc);
422483
} else if (name === 'moreThanEqual') {
@@ -443,8 +504,18 @@ export class RegExpBuilder {
443504
private excuteIncludeStatement<T extends string, P extends string>(
444505
lastExpression: T,
445506
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}))` {
448519
if (options.isForehead) {
449520
return this.lookbehind(value, lastExpression);
450521
} else {

Diff for: src/test/regexp-repository.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@ describe('Check regexp-builder is type-safe', () => {
1010
expect(test).toBe('test');
1111
});
1212
});
13+
14+
describe('from & include infer response type', () => {
15+
it('from method set initial value & include other', () => {
16+
const test = new RegExpBuilder().findOne({
17+
from: 'test',
18+
include: { partial: 'left', options: {} },
19+
});
20+
21+
expect(test).toBe('(test)(?=(left))');
22+
});
23+
});
1324
});

Diff for: types/index.d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { RegExpBuilder } from '../src/regexp-builder';
22

3-
type IncludeOptions = { isForehead?: boolean };
3+
// type IncludeOptionIsBehind = { isForehead?: true };
4+
// type IncludeOptionIsForehead = { isForehead?: true };
5+
6+
type IncludeOptions<T extends boolean> = { isForehead?: T };
47
type AndOptions = { isForehead?: boolean };
58
type SubExpressionBilder<T extends string> = (subBuilder: RegExpBuilder) => T | string | RegExpBuilder;
69
type Push<T extends any[], val> = [...T, val];
@@ -21,7 +24,7 @@ type Status<T extends RegExpMethodNames> = {
2124
: T extends 'lessThanEqual'
2225
? number
2326
: string;
24-
options: T extends 'include' ? IncludeOptions : T extends 'and' ? AndOptions : null;
27+
options: T extends 'include' ? IncludeOptions<boolean> : T extends 'and' ? AndOptions : null;
2528
beforeStatus: string;
2629
order: number;
2730
};

0 commit comments

Comments
 (0)