regexp-manager
Advanced tools
Comparing version
@@ -132,3 +132,4 @@ import type { RegExpPatternBuilder } from './regexp-pattern-builder'; | ||
export type RegExpTypeName = Exclude<MethodNames<RegExpPatternBuilder<any>>, 'expression' | 'getRegExp'> | 'init' | 'lookahead' | 'lookbehind' | 'negativeLookahead' | 'negativeLookbehind'; | ||
export type EntriesToObject<T extends NTuple<2>[]> = T extends [infer F, ...infer Rest] ? F extends [infer K, infer V] ? Rest extends NTuple<2>[] ? Merge<Record<ToString<K>, V>, EntriesToObject<Rest>> : never : never : {}; | ||
export type ObjectToEntries<T extends Object> = T extends EntriesToObject<infer O> ? O : never; | ||
export type EntriesToObject<T extends Array<NTuple<2>>> = T extends [infer F, ...infer Rest] ? F extends [infer K, infer V] ? Rest extends NTuple<2>[] ? Merge<Record<ToString<K>, V>, EntriesToObject<Rest>> : never : never : {}; | ||
export type IsRecord<T> = T extends Record<PropertyKey, any> ? T : never; | ||
export type ObjectToEntries<T extends Object, Entries extends Array<NTuple<2>> = []> = T extends Merge<Record<infer Key, infer Type>, infer Rest> ? Equal<Rest, {}> extends true ? [...Entries, [Key, Type]] : ObjectToEntries<IsRecord<Rest>, [...Entries, [Key, Type]]> : []; |
{ | ||
"name": "regexp-manager", | ||
"version": "0.16.4", | ||
"version": "0.16.5", | ||
"description": "regexp builder for node.js developer", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -15,17 +15,60 @@ # regexp-manager | ||
# How to use a builder | ||
```typescript | ||
import { RegExpPatternBuilder } from 'regexp-manager'; | ||
/** | ||
* result : '(010|011)[0-9]{3,4}[0-9]{4,4}' | ||
*/ | ||
const koreanPhoneNumber = new RegExpPatternBuilder() | ||
.capturing((qb) => qb.and('010').or('011')) // '(010|011)' | ||
.and('-') // '-' | ||
.and((qb) => qb, and('[0-9]').between(3, 4)) // '[0-9]{3,4}' | ||
.and('-') // '-' | ||
.and((qb) => qb.and('[0-9]').between(4, 4)).expression; // '[0-9]{4,4}' | ||
``` | ||
If you write a function that returns the string or additional builder according to the inferred type, you can check the result at the time of compilation. If you want to use a sub-builder inside, you can use the `qb` given as a parameter. It works even if you just write it as a string. | ||
# Methods currently implemented | ||
## or | ||
It means 'or syntax' using pipe characters in regular expressions. The 'or syntax' can also be checked in advance by type level. | ||
```typescript | ||
// `or` method | ||
// Below return 'left|right'. And it's also inferred from the type. | ||
const leftOrRight = new RegExpPatternBuilder('left').or('right').expression; | ||
// It's also inferred from the type. | ||
// `and` method | ||
// Below return 'leftright'. And it's also inferred from the type. | ||
const leftAndRight = new RegExpPatternBuilder('left').and('right').expression; | ||
const leftOrRight = new RegExpPatternBuilder('left').or('right').expression; // 'left|right' | ||
const redOrBlue = new RegExpPatternBuilder('red').or('blue').expression; // 'red|blue' | ||
const dogOrCat = new RegExpPatternBuilder('dog').or('cat').expression; // 'dog|cat' | ||
``` | ||
// `capturing` method | ||
// Below return '(A)'. And it's also inferred from the type. | ||
const capturingA = new RegExpPatternBuilder('').capturing('A').expression; | ||
## and | ||
The 'and syntax' simply connects strings. In a regular expression, sometimes it is more readable to divide and combine characters in semantic units. | ||
```typescript | ||
// It's also inferred from the type. | ||
const leftOrRight = new RegExpPatternBuilder('left').and('right').expression; // 'leftright' | ||
const redOrBlue = new RegExpPatternBuilder('red').and('blue').expression; // 'redblue' | ||
const dogOrCat = new RegExpPatternBuilder('dog').and('cat').expression; // 'dogcat' | ||
``` | ||
## capturing | ||
There is a syntax called 'capturing' in the regular expression. This syntax uses parentheses to lock the string, making the `test`, `match` method of the regular expression different in the future. | ||
```typescript | ||
// It's also inferred from the type. | ||
const capturingA = new RegExpPatternBuilder().capturing('A').expression; // '(A)' | ||
``` | ||
## quantifier methods | ||
There are four methods in `quantifier`: `lessThan`, `lessThanOrEqual`, `moreThan`, and `moreThanOrEqual`. They define how many times a previously specified character or string will exist based on a given number. | ||
```typescript | ||
// `lessThan` method | ||
@@ -46,34 +89,30 @@ // Below return 'a{1,9}'. And it's also inferred from the type. | ||
const moreThanOrEqualThree = new RegExpPatternBuilder('a').moreThanOrEqual(3).expression; | ||
``` | ||
## includes | ||
It means `lookahead`, `lookbehind` of the regular expression. These are strings that should be included in a string but should not be caught in a regular expression. | ||
```typescript | ||
// `include`("left", P) method means lookbehind | ||
// Below return '(?<=a)b'. And it's also inferred from the type. | ||
const lookhehind = new RegExpPatternBuilder('b').includes('LEFT', 'a'); | ||
const lookhehind = new RegExpPatternBuilder('b').includes('LEFT', 'a').expression; | ||
// `include`("right", P) method means lookahead | ||
// Below return 'b(?=a)'. And it's also inferred from the type. | ||
const lookahead = new RegExpPatternBuilder('b').includes('RIGHT', 'a'); | ||
const lookahead = new RegExpPatternBuilder('b').includes('RIGHT', 'a').expression; | ||
``` | ||
## excludes | ||
It means `lookahead`, `lookbehind` of the regular expression. These are strings that should be excluded in a string. | ||
```typescript | ||
// `exclude`("left", P) method means negative lookbehind | ||
// Below return '(?<!a)b'. And it's also inferred from the type. | ||
const nagativeLookbhind = new RegExpPatternBuilder('b').excludes('LEFT', 'a'); | ||
const nagativeLookbhind = new RegExpPatternBuilder('b').excludes('LEFT', 'a').expression; | ||
// `exclude`("right", P) method means negative lookbehind | ||
// Below return 'b(?!a)'. And it's also inferred from the type. | ||
const negativeLookahead = new RegExpPatternBuilder('b').excludes('RIGHT', 'a'); | ||
const negativeLookahead = new RegExpPatternBuilder('b').excludes('RIGHT', 'a').expression; | ||
``` | ||
# How to use a builder | ||
```typescript | ||
import { RegExpPatternBuilder } from 'regexp-manager'; | ||
/** | ||
* result : '(010|011)[0-9]{3,4}[0-9]{4,4}' | ||
*/ | ||
const koreanPhoneNumber = new RegExpPatternBuilder('') | ||
.capturing((qb) => qb.and('010').or('011')) | ||
.and((qb) => qb,and('[0-9]').between(3, 4)) | ||
.and((qb) => qb.and('[0-9]').between(4, 4)).expression; | ||
``` | ||
If you write a function that returns the string or additional builder according to the inferred type, you can check the result at the time of compilation. |
26252
7.83%362
0.28%117
50%