1-
2- // --- exported definitions ---
3- /** Defines all possible flags for regular expressions. */
4- export type RegExpFlags = "d" | "g" | "i" | "m" | "u" | "y" ;
5- /** A tool to create all possible strings that matches the defined flags */
6- export type RegExpFlagCombos < F extends { [ key in RegExpFlags ] ?: boolean } = { } > = FlagCombosGenerator < { [ key in RegExpFlags ] : boolean } & F > ;
7-
81export type TypedRegExpMatchArray < Groups extends string [ ] = string [ ] , NamedGroups extends { [ key : string ] : string } = { [ key : string ] : string } , InputString extends string = string > = {
92 index ?: number ;
103 input ?: InputString ;
11- groups : OneNotOptional < NamedGroups > ; // never instead to immitate optional
12- } & ( [ string , ...Groups ] /* BUG out of range is only undefiend */ & IntersectedArray < string , TypedRegExpMatchArray < Groups , NamedGroups , InputString > > ) ;
4+ groups : OneNotOptional < NamedGroups > ;
5+ } & ( [ string , ...Groups ] & IntersectedArray < string , TypedRegExpMatchArray < Groups , NamedGroups , InputString > > ) ;
136
147export type TypedRegExpExecArray < Groups extends string [ ] = string [ ] , NamedGroups extends { [ key : string ] : string } = { [ key : string ] : string } , InputString extends string = string > = TypedRegExpMatchArray < Groups , NamedGroups , InputString > & {
158 index : number ;
@@ -21,43 +14,39 @@ export type TypedRegExpExecArray<Groups extends string[] = string[], NamedGroups
2114export interface TypedRegExp <
2215 Groups extends string [ ] = string [ ] ,
2316 NamedGroups extends { [ key : string ] : string } = { [ key : string ] : string } ,
24- FlagCombo extends RegExpFlagCombos = RegExpFlagCombos , // IDEA force alphabetical order to reduce computation time
17+ FlagCombo extends RegExpFlagCombos = RegExpFlagCombos ,
2518> extends RegExp {
26- < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos = RegExpFlagCombos > ( pattern : TypedRegExp < Groups , NamedGroups > | string ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
27- < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos = RegExpFlagCombos > ( pattern : string , flags ?: FlagCombo ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
28-
29- // TODO only override if existing in the RegExp interface
19+ // IDEA only override if existing in the RegExp interface to support more versions
3020 exec < InputString extends string , Matches extends boolean = boolean > ( string : InputString ) : If < Matches , TypedRegExpExecArray < Groups , NamedGroups , InputString > , null > ;
31- // test(string: string): boolean;
32- // readonly source: string;
3321 readonly dotAll : HasFlag < FlagCombo , "d" > ;
3422 readonly global : HasFlag < FlagCombo , "g" > ;
3523 readonly ignoreCase : HasFlag < FlagCombo , "i" > ;
3624 readonly multiline : HasFlag < FlagCombo , "m" > ;
3725 readonly unicode : HasFlag < FlagCombo , "u" > ;
3826 readonly sticky : HasFlag < FlagCombo , "y" > ;
39- readonly flags : SortFlags < FlagCombo > ;
40- // lastIndex: number;
27+ readonly flags : SortRegExpFlags < FlagCombo > ;
4128 compile < NewGroups extends Groups = Groups , NewNamedGroups extends NamedGroups = NamedGroups , NewFlagCombo extends FlagCombo = FlagCombo > ( pattern : string , flags ?: NewFlagCombo ) : this & TypedRegExp < NewGroups , NewNamedGroups , NewFlagCombo > ;
4229 [ Symbol . match ] < InputString extends string , Matches extends boolean = boolean > ( string : InputString ) : If < Matches , TypedRegExpMatchArray < Groups , NamedGroups , InputString > , null > ;
43- [ Symbol . replace ] ( string : string , replaceValue : string ) : string ; // required
30+ [ Symbol . replace ] ( string : string , replaceValue : string ) : string ;
4431 [ Symbol . replace ] ( string : string , replacer : ( substring : string , ...args : [ ...Groups , number , string , ...( keyof NamedGroups extends never ? [ ] : [ NamedGroups ] ) ] ) => string ) : string ;
45- // [Symbol.search](string: string): number;
46- // [Symbol.split](string: string, limit?: number): string[];
4732 [ Symbol . matchAll ] < InputString extends string > ( str : InputString ) : IterableIterator < TypedRegExpMatchArray < Groups , NamedGroups , InputString > > ;
4833}
4934
50- /** A type constrained regular expression */
51- export class TypedRegExp <
52- Groups extends string [ ] = string [ ] ,
53- NamedGroups extends { [ key : string ] : string } = { [ key : string ] : string } ,
54- FlagCombo extends RegExpFlagCombos = RegExpFlagCombos ,
55- > extends RegExp {
56- constructor ( pattern : TypedRegExp < Groups , NamedGroups , FlagCombo > | string ) ;
57- constructor ( pattern : string , flags ?: FlagCombo ) ;
58- constructor ( pattern : string | TypedRegExp < Groups , NamedGroups , FlagCombo > , flags ?: FlagCombo ) {
59- super ( pattern , flags ) ;
60- }
35+ export const TypedRegExp = RegExp as TypedRegExpConstructor ;
36+
37+ type RegExpConstructor = typeof RegExp ;
38+ interface TypedRegExpConstructor extends RegExpConstructor {
39+ new < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } > ( ) : TypedRegExp < [ ] , { } , "" > ;
40+ new < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } > ( pattern : string ) : TypedRegExp < Groups , NamedGroups , "" > ;
41+ new < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : string , flags : FlagCombo ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
42+ new < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : TypedRegExp < Groups , NamedGroups , FlagCombo > ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
43+ new < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : TypedRegExp < Groups , NamedGroups > , flags : FlagCombo ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
44+ < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } > ( pattern : string ) : TypedRegExp < Groups , NamedGroups , "" > ;
45+ < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : string , flags : FlagCombo ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
46+ < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : TypedRegExp < Groups , NamedGroups , FlagCombo > ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
47+ < Groups extends string [ ] , NamedGroups extends { [ key : string ] : string } , FlagCombo extends RegExpFlagCombos > ( pattern : TypedRegExp < Groups , NamedGroups > , flags : FlagCombo ) : TypedRegExp < Groups , NamedGroups , FlagCombo > ;
48+ readonly prototype : TypedRegExp ;
49+ readonly [ Symbol . species ] : TypedRegExpConstructor ;
6150}
6251
6352/** @experimental This class is intended to prevent overloads from the default string methods. */
@@ -80,7 +69,13 @@ declare global {
8069 }
8170}
8271
83- // --- utils ---
72+ /* --- utils --- */
73+ /** Defines all possible flags for regular expressions. */
74+ export type RegExpFlags = "d" | "g" | "i" | "m" | "u" | "y" ;
75+
76+ /** A tool to create all possible strings that matches the defined flags */
77+ export type RegExpFlagCombos < F extends { [ key in RegExpFlags ] ?: boolean } = { } > = FlagCombosGenerator < { [ key in RegExpFlags ] : boolean } & F > ;
78+ // IDEA force alphabetical order to reduce computation time
8479
8580/** returns the string keys of an object */
8681type StringKeysOf < T > = Extract < keyof T , string > ;
@@ -104,26 +99,23 @@ type OneNotOptional<T> = {[K in keyof T]-?: {[OK in Exclude<OptionalKeys<T>,K>]+
10499type ValueOf < T > = T [ keyof T ] ;
105100
106101/** toggle if the object has no true values */
107- type NoTrueValue < O extends { [ k :string ] :boolean } , T , F = never > = { [ k in keyof O as O [ k ] extends true ? k : never ] :k } extends { [ k :string ] :never } ? T : F ;
102+ type NoAlwaysTrueValue < O extends { [ k :string ] :boolean } , T , F = never > = { [ k in keyof O as O [ k ] extends true ? k : never ] :k } extends { [ k :string ] :never } ? T : F ;
108103
104+ /** modified array that fixes return value from the sort function when the type is used in an intersection */
109105interface IntersectedArray < T , S > extends Array < T > {
110- // modified array that fixes return value from the sort function when the type is used in an intersection
111106 sort ( compareFn ?: ( a : T , b : T ) => number ) : this & S ;
112- // elements are defined by the intersected array
113- [ n : number ] : never ;
107+ [ n : number ] : never ; // elements are defined by the intersected array
114108}
115109
116- // /** converts a union string to all possible combinations including them */
117- // type StringWith<F extends string> = {[f in F]:`${f}${StringWith<Exclude<F,f>> extends (infer R extends string) ? R : ""}`}[F] | "";
118-
119- // /** returns true if both objects are equal else false */
120- // type Equals<T,U> = T extends U ? U extends T ? true : false : false;
110+ /** create object with a set value type */
111+ type ObjectWith < T > = { [ key : string ] : T } ;
121112
122113/** toggle if a substring is in a string */
123114type IfIn < String extends string , SubString extends string , True , False = never > = String extends `${string } ${SubString } ${string } ` ? True : False ;
124115
125116/** returns all possible combinations with the given flags */
126- type FlagCombosGenerator < F extends { [ k :string ] : boolean } > = ValueOf < { [ f in StringKeysOf < F > as F [ f ] extends false ? never : F [ f ] extends true ? never : f ] -?: `${f | "" } ${FlagCombosGenerator < Omit < F , f > > extends infer R ? R extends string ? R : "" : "" } ` } > | ValueOf < { [ f in StringKeysOf < F > as F [ f ] extends true ? f : never ] -?: `${f } ${FlagCombosGenerator < Omit < F , f > > extends infer R ? R extends string ? R : "" : "" } ` } > | NoTrueValue < F , "" > ;
117+ type FlagCombosGenerator < F extends { [ k :string ] : boolean } > = ValueOf < { [ f in StringKeysOf < F > as F [ f ] extends false ? never : F [ f ] extends true ? never : f ] -?: `${f | "" } ${FlagCombosGenerator < Omit < F , f > > extends infer R ? R extends string ? R : "" : "" } ` } > | ValueOf < { [ f in StringKeysOf < F > as F [ f ] extends true ? f : never ] -?: `${f } ${FlagCombosGenerator < Omit < F , f > > extends infer R ? R extends string ? R : "" : "" } ` } > | NoAlwaysTrueValue < F , "" > ;
127118
128119/** sorts the flag alphabetically */
129- type SortFlags < FlagCombo extends RegExpFlagCombos > = `${IfIn < FlagCombo , "d" , "d" , "" > } ${IfIn < FlagCombo , "g" , "g" , "" > } ${IfIn < FlagCombo , "i" , "i" , "" > } ${IfIn < FlagCombo , "m" , "m" , "" > } ${IfIn < FlagCombo , "u" , "u" , "" > } ${IfIn < FlagCombo , "y" , "y" , "" > } `;
120+ export type SortRegExpFlags < FlagCombo extends RegExpFlagCombos > = `${IfIn < FlagCombo , "d" , "d" , "" > } ${IfIn < FlagCombo , "g" , "g" , "" > } ${IfIn < FlagCombo , "i" , "i" , "" > } ${IfIn < FlagCombo , "m" , "m" , "" > } ${IfIn < FlagCombo , "u" , "u" , "" > } ${IfIn < FlagCombo , "y" , "y" , "" > } `;
121+ // IDEA generall sort based on alphabet
0 commit comments