//#region src/core/flags.d.ts type Flag = 'd' | 'g' | 'i' | 'm' | 's' | 'u' | 'y'; /** Generate indices for substring matches */ declare const withIndices = "d"; /** Case-insensitive search */ declare const caseInsensitive = "i"; /** Global search */ declare const global = "g"; /** Multi-line search */ declare const multiline = "m"; /** Allows `.` to match newline characters */ declare const dotAll = "s"; /** Treat a pattern as a sequence of unicode code points */ declare const unicode = "u"; /** Perform a "sticky" search that matches starting at the current position in the target string */ declare const sticky = "y"; //#endregion //#region src/core/types/join.d.ts type Join = T extends [infer F extends string, ...infer R extends string[]] ? `${F}${Join}` : ''; type UnionToIntersection = (Union extends Union ? (a: Union) => any : never) extends ((a: infer I) => any) ? I : never; type UnionToTuple = UnionToIntersection Union : never> extends (() => infer Item) ? UnionToTuple, [...Tuple, Item]> : Tuple; //#endregion //#region src/core/wrap.d.ts /** * What an input's value is, structurally. `atom` takes a quantifier directly; * `quantified` is an atom that already has one, so it can still be rewritten * into a capture group but cannot take another. */ type InputKind = 'atom' | 'quantified' | 'other'; /** Whether `Value` is one character, optionally escaped. */ type IsSingleChar = StripEscapes extends `${infer Head}${infer Tail}` ? Tail extends '' ? Head extends '' ? false : true : false : false; /** Appends `Quantifier`, grouping `Value` first unless it is a single atom. */ type Quantified = [Kind] extends ['atom'] ? `${Value}${Quantifier}` : `(?:${Value})${Quantifier}`; //#endregion //#region src/core/inputs.d.ts /** The lone input's kind, or an atom when the joined value is one character. */ type JoinedKind = Inputs extends [{ readonly [KIND]: infer K extends InputKind; }] ? K : IsSingleChar extends true ? 'atom' : 'other'; /** This matches any character in the string provided */ declare const charIn: ((chars: T) => CharInput>) & CharInput<"">; /** This matches any character that is not in the string provided */ declare const charNotIn: ((chars: T) => CharInput<`^${Escape}`>) & CharInput<"^">; /** * This takes a variable number of inputs and matches any of them * @example * anyOf('foo', maybe('bar'), 'baz') // => /(?:foo|(?:bar)?|baz)/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ declare function anyOf(...inputs: Inputs): Input<`(?:${JoinValuesWith})`, MapToGroups, MapToCapturedGroupsArr, 'atom'>; declare const char: Input<".", never, [], "atom">; declare const word: Input<"\\b\\w+\\b", never, [], "other">; declare const wordChar: Input<"\\w", never, [], "atom">; declare const wordBoundary: Input<"\\b", never, [], "other">; declare const digit: Input<"\\d", never, [], "atom">; declare const whitespace: Input<"\\s", never, [], "atom">; declare const letter: Input<"[a-zA-Z]", never, [], "atom"> & { lowercase: Input<"[a-z]", never, [], "atom">; uppercase: Input<"[A-Z]", never, [], "atom">; }; declare const tab: Input<"\\t", never, [], "atom">; declare const linefeed: Input<"\\n", never, [], "atom">; declare const carriageReturn: Input<"\\r", never, [], "atom">; declare const not: { word: Input<"\\W+", never, [], "other">; wordChar: Input<"\\W", never, [], "atom">; wordBoundary: Input<"\\B", never, [], "other">; digit: Input<"\\D", never, [], "atom">; whitespace: Input<"\\S", never, [], "atom">; letter: Input<"[^a-zA-Z]", never, [], "atom"> & { lowercase: Input<"[^a-z]", never, [], "atom">; uppercase: Input<"[^A-Z]", never, [], "atom">; }; tab: Input<"[^\\t]", never, [], "atom">; linefeed: Input<"[^\\n]", never, [], "atom">; carriageReturn: Input<"[^\\r]", never, [], "atom">; }; /** * Equivalent to `?` - takes a variable number of inputs and marks them as optional * @example * maybe('foo', exactly('ba?r')) // => /(?:fooba\?r)?/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ declare function maybe>(...inputs: Inputs): Input, '?'>, MapToGroups, MapToCapturedGroupsArr, 'quantified'>; /** * This takes a variable number of inputs and concatenate their patterns, and escapes string inputs to match it exactly * @example * exactly('fo?o', maybe('bar')) // => /fo\?o(?:bar)?/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ declare function exactly>(...inputs: Inputs): Input, MapToCapturedGroupsArr, JoinedKind>; /** * Equivalent to `+` - this takes a variable number of inputs and marks them as repeatable, any number of times but at least once * @example * oneOrMore('foo', maybe('bar')) // => /(?:foo(?:bar)?)+/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ declare function oneOrMore>(...inputs: Inputs): Input, '+'>, MapToGroups, MapToCapturedGroupsArr, 'quantified'>; //#endregion //#region src/core/types/escape.d.ts type Escape = T extends `${infer Start}${EscapeChar}${string}` ? Start extends `${string}${EscapeChar}${string}` ? never : T extends `${Start}${infer Char}${infer Rest}` ? Char extends EscapeChar ? `${Start}\\${Char}${Escape}` : never : never : T; type EscapeChar = Escape; type StripEscapes = T extends `${infer A}\\${infer B}` ? `${A}${B}` : T; type ExactEscapeChar = '.' | '*' | '+' | '?' | '^' | '$' | '{' | '}' | '(' | ')' | '|' | '[' | ']' | '/'; type GetValue = T extends string ? Escape : T extends Input ? R : never; //#endregion //#region src/core/internal.d.ts declare const KIND: unique symbol; /** * A leading `(?:` only encloses the whole value when that value is a lone atom, * so anything else has to be wrapped rather than rewritten. */ type NamedGroup = 'other' extends K ? `(?<${Name}>${V})` : V extends `(?:${infer S})` ? `(?<${Name}>${S})` : `(?<${Name}>${V})`; type Grouped = 'other' extends K ? `(${V})` : V extends `(?:${infer S})${infer E}` ? `(${S})${E}` : `(${V})`; type GroupedCapture = 'other' extends K ? `(${V})` : V extends `(?:${infer S})${'' | '?' | '+' | '*' | `{${string}}`}` ? `(${S})` : `(${V})`; type Captures = (string | undefined)[]; interface And { >(...inputs: I): Input<`${V}${JoinValues}`, G | MapToGroups, [...C, ...CG]>; /** this adds a new pattern to the current input, with the pattern reference to a named group. */ referenceTo: (groupName: N) => Input<`${V}\\k<${N}>`, G, C>; } interface Or { >(...inputs: I): Input<`(?:${V}|${JoinValues})`, G | MapToGroups, [...C, ...CG], 'atom'>; } interface After { >(...inputs: I): Input<`(?<=${JoinValues})${V}`, G | MapToGroups, [...CG, ...C]>; } interface Before { >(...inputs: I): Input<`${V}(?=${JoinValues})`, G, [...C, ...CG]>; } interface NotAfter { >(...inputs: I): Input<`(?})${V}`, G, [...CG, ...C]>; } interface NotBefore { >(...inputs: I): Input<`${V}(?!${JoinValues})`, G, [...C, ...CG]>; } interface GroupedFn { (): Input, G, [GroupedCapture, ...C], [Kind] extends ['quantified'] ? 'quantified' : 'atom'>; } interface Optionally { >(): Input; } interface Times { >(number: N): Input; /** specify that the expression can repeat any number of times, _including none_ */ any: >() => Input; /** specify that the expression must occur at least `N` times */ atLeast: >(number: N) => Input; /** specify that the expression must occur at most `N` times */ atMost: >(number: N) => Input; /** specify a range of times to repeat the previous pattern */ between: >(min: Min, max: Max) => Input; } interface At { lineStart: () => Input<`^${V}`, G, C>; lineEnd: () => Input<`${V}$`, G, C>; } interface GroupedAs { (key: K): Input, G | K, [NamedGroup, ...C], 'atom'>; } interface Input { /** @internal */ readonly [KIND]: Kind; /** * this takes a variable number of inputs and adds them as new pattern to the current input, or you can use `and.referenceTo(groupName)` to adds a new pattern referencing to a named group * @example * exactly('foo').and('bar', maybe('baz')) // => /foobar(?:baz)?/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ and: And; /** * this takes a variable number of inputs and provides as an alternative to the current input * @example * exactly('foo').or('bar', maybe('baz')) // => /foo|bar(?:baz)?/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ or: Or; /** * this takes a variable number of inputs and activate 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) * @example * exactly('foo').after('bar', maybe('baz')) // => /(?<=bar(?:baz)?)foo/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ after: After; /** * this takes a variable number of inputs and activate a positive lookahead * @example * exactly('foo').before('bar', maybe('baz')) // => /foo(?=bar(?:baz)?)/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ before: Before; /** * these takes a variable number of inputs and activate 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) * @example * exactly('foo').notAfter('bar', maybe('baz')) // => /(?; /** * this takes a variable number of inputs and activate a negative lookahead * @example * exactly('foo').notBefore('bar', maybe('baz')) // => /foo(?!bar(?:baz)?)/ * @argument inputs - arbitrary number of `string` or `Input`, where `string` will be escaped */ notBefore: NotBefore; /** repeat the previous pattern an exact number of times */ times: Times; /** 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` */ as: GroupedAs; /** 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()` */ groupedAs: GroupedAs; /** this capture the entire input so far as an anonymous group */ grouped: GroupedFn; /** this allows you to match beginning/ends of lines with `at.lineStart()` and `at.lineEnd()` */ at: At; /** this allows you to mark the input so far as optional */ optionally: Optionally; toString: () => string; } interface CharInput extends Input<`[${T}]`, never, [], 'atom'> { orChar: ((chars: Or) => CharInput<`${T}${EscapeChar}`>) & CharInput; from: (charFrom: From, charTo: To) => CharInput<`${T}${EscapeChar}-${EscapeChar}`>; } //#endregion //#region src/core/types/sources.d.ts type InputSource = S | Input; type GroupsOf = Source extends Input ? K : never; type MapToGroups = T extends string[] ? never : GroupsOf; type MapToCapturedGroupsArr = Inputs extends string[] ? [] : Inputs extends [infer First, ...infer Rest] ? First extends Input ? [CaptureGroups] extends [never] ? MapToCapturedGroupsArr : [...CaptureGroups, ...MapToCapturedGroupsArr] : MapToCapturedGroupsArr : []; /** One `undefined` per capture group, for lookarounds whose captures never match. */ type MapToUndefinedCapturedGroupsArr = Inputs extends string[] ? [] : Inputs extends [infer First, ...infer Rest] ? First extends Input ? [CaptureGroups] extends [never] ? MapToUndefinedCapturedGroupsArr : [...{ [K in keyof CaptureGroups]: undefined; }, ...MapToUndefinedCapturedGroupsArr] : MapToUndefinedCapturedGroupsArr : []; type JoinValues = T extends [infer First, ...infer Rest] ? `${GetValue}${JoinValues}` : ''; type JoinValuesWith = T extends [infer First, ...infer Rest] ? `${Prefix}${GetValue}${JoinValuesWith}` : ''; //#endregion //#region src/core/types/magic-regexp.d.ts declare const NamedGroupsS: unique symbol; declare const ValueS: unique symbol; declare const CapturedGroupsArrS: unique symbol; declare const FlagsS: unique symbol; type MagicRegExp = RegExp & { [NamedGroupsS]: NamedGroups; [CapturedGroupsArrS]: CapturedGroupsArr; [ValueS]: Value; [FlagsS]: Flags; }; type ExtractGroups> = T extends { [NamedGroupsS]: infer V; } ? V : never; type StringCapturedBy = string & { _capturedBy: S; }; type MapToStringCapturedBy = { [K in keyof Ar]: Ar[K] extends string ? StringCapturedBy | undefined : undefined; }; type MagicRegExpMatchArray> = Omit & { groups: Record, string | undefined>; } & { [index: number | string | symbol]: never; } & (T extends { [CapturedGroupsArrS]: infer CapturedGroupsArr extends (string | undefined)[]; } ? readonly [string | undefined, ...MapToStringCapturedBy] : {}); //#endregion export { caseInsensitive as A, whitespace as C, Join as D, wordChar as E, unicode as F, withIndices as I, global as M, multiline as N, UnionToTuple as O, sticky as P, tab as S, wordBoundary as T, letter as _, InputSource as a, not as b, MapToGroups as c, carriageReturn as d, char as f, exactly as g, digit as h, StringCapturedBy as i, dotAll as j, Flag as k, Input as l, charNotIn as m, MagicRegExpMatchArray as n, JoinValues as o, charIn as p, MapToStringCapturedBy as r, MapToCapturedGroupsArr as s, MagicRegExp as t, anyOf as u, linefeed as v, word as w, oneOrMore as x, maybe as y };