rgex
Version:
A powerful, chainable regex builder platform with comprehensive validation utilities
379 lines • 13.6 kB
TypeScript
/**
* @fileoverview Core RGex class with fluent API for building regular expressions
* @module Core
* @category Core
* @group RGex Builder
* @author duongnguyen321 - https://duonguyen.site
*/
/**
* @class RGex
* @classdesc The main class for building and manipulating regular expressions using a fluent, chainable API.
* It also provides static methods for AI-powered text-to-regex and validation rule generation.
* @category Core
* @group RGex Builder
*/
import type { PasswordValidationOptions, PasswordValidationResult, RegexBuilderOptions, TextExtractionResult, ValidationExtractionResult } from '../../types/index.js';
/**
* Main RGex class with fluent API
*/
export declare class RGex {
private pattern;
private options;
/**
* Initializes a new instance of the RGex class.
* @param pattern - The initial regex pattern string.
* @param options - The initial regex options.
*/
constructor(pattern?: string, options?: RegexBuilderOptions);
/**
* Creates a new RGex instance, providing a static entry point to the fluent API.
* @param pattern - The initial regex pattern.
* @param options - The regex options.
* @returns A new RGex instance.
*/
static create(pattern?: string, options?: RegexBuilderOptions): RGex;
/**
* Converts a human-readable text description into a regex pattern.
* @param humanText - The natural language description of the pattern.
* @param testValue - An optional string to test the generated pattern against.
* @returns A result object containing the pattern and analysis.
*/
static toRegex(humanText: string, testValue?: string): TextExtractionResult;
/**
* Converts a human-readable text description into a set of validation rules.
* @param humanText - The natural language description of the validation rules.
* @param testValue - An optional string to test the generated rules against.
* @returns A result object containing the validation rules.
*/
static toValidate(humanText: string, testValue?: string): ValidationExtractionResult;
/**
* Gets pattern suggestions based on a human-readable text input.
* @param text - The input text to get suggestions for.
* @returns An array of suggested pattern keywords.
*/
static getSuggestions(text: string): string[];
/**
* Creates a new RGex instance from a human-readable text description.
* @param humanText - The natural language description of the pattern.
* @param testValue - An optional string to test the generated pattern against.
* @returns A new RGex instance.
*/
static humanText(humanText: string, testValue?: string): RGex;
/**
* Replaces the current pattern with one generated from a human-readable text description.
* @param humanText - The natural language description of the pattern.
* @param testValue - An optional string to test the generated pattern against.
* @returns The current RGex instance for chaining.
*/
humanText(humanText: string, testValue?: string): RGex;
/**
* Appends a literal string to the pattern, escaping special regex characters.
* @param text - The literal string to append.
* @returns The current RGex instance for chaining.
*/
literal(text: string): RGex;
/**
* Appends a raw (unescaped) regex pattern string.
* @param pattern - The raw pattern string to append.
* @returns The current RGex instance for chaining.
*/
raw(pattern: string): RGex;
/**
* Appends a character class to the pattern.
* @param chars - The characters to include in the class.
* @param negate - If true, creates a negated character class.
* @returns The current RGex instance for chaining.
*/
charClass(chars: string, negate?: boolean): RGex;
/**
* Appends a digit character class (\\d).
* @returns The current RGex instance for chaining.
*/
digit(): RGex;
/**
* Appends a word character class (\\w).
* @returns The current RGex instance for chaining.
*/
word(): RGex;
/**
* Appends a whitespace character class (\\s).
* @returns The current RGex instance for chaining.
*/
whitespace(): RGex;
/**
* Appends an "any character" pattern (.).
* @returns The current RGex instance for chaining.
*/
any(): RGex;
/**
* Adds a start-of-string anchor (^) to the beginning of the pattern.
* @returns The current RGex instance for chaining.
*/
start(): RGex;
/**
* Adds an end-of-string anchor ($) to the end of the pattern.
* @returns The current RGex instance for chaining.
*/
end(): RGex;
/**
* Appends an OR operator (|) followed by another pattern.
* @param pattern - The alternative pattern.
* @returns The current RGex instance for chaining.
*/
or(pattern: string): RGex;
/**
* Appends a quantifier to the preceding element.
* @param min - The minimum number of repetitions.
* @param max - The maximum number of repetitions. Use Infinity for no upper limit.
* @returns The current RGex instance for chaining.
*/
quantifier(min: number, max?: number): RGex;
/**
* Appends a zero-or-more quantifier (*).
* @returns The current RGex instance for chaining.
*/
zeroOrMore(): RGex;
/**
* Appends a one-or-more quantifier (+).
* @returns The current RGex instance for chaining.
*/
oneOrMore(): RGex;
/**
* Appends a zero-or-one quantifier (?).
* @returns The current RGex instance for chaining.
*/
optional(): RGex;
/**
* Wraps a pattern in a capturing group.
* @param pattern - The pattern to group.
* @returns The current RGex instance for chaining.
*/
group(pattern: string): RGex;
/**
* Wraps a pattern in a non-capturing group.
* @param pattern - The pattern to group.
* @returns The current RGex instance for chaining.
*/
nonCapturingGroup(pattern: string): RGex;
/**
* Appends a lookahead assertion.
* @param pattern - The pattern for the lookahead.
* @param negative - If true, creates a negative lookahead.
* @returns The current RGex instance for chaining.
*/
lookahead(pattern: string, negative?: boolean): RGex;
/**
* Appends a lookbehind assertion.
* @param pattern - The pattern for the lookbehind.
* @param negative - If true, creates a negative lookbehind.
* @returns The current RGex instance for chaining.
*/
lookbehind(pattern: string, negative?: boolean): RGex;
/**
* Appends a pre-built email validation pattern.
* @returns The current RGex instance for chaining.
*/
email(): RGex;
/**
* Appends a pre-built URL validation pattern.
* @returns The current RGex instance for chaining.
*/
url(): RGex;
/**
* Appends a pre-built phone number validation pattern.
* @returns The current RGex instance for chaining.
*/
phone(): RGex;
/**
* Appends a pre-built date (YYYY-MM-DD) validation pattern.
* @returns The current RGex instance for chaining.
*/
date(): RGex;
/**
* Appends a pre-built time (HH:MM or HH:MM:SS) validation pattern.
* @returns The current RGex instance for chaining.
*/
time(): RGex;
/**
* Appends a pre-built number (integer or decimal) validation pattern.
* @returns The current RGex instance for chaining.
*/
number(): RGex;
/**
* Appends a pre-built UUID validation pattern.
* @returns The current RGex instance for chaining.
*/
uuid(): RGex;
/**
* Appends a pre-built IPv4 validation pattern.
* @returns The current RGex instance for chaining.
*/
ipv4(): RGex;
/**
* Appends a pre-built hex color code validation pattern.
* @returns The current RGex instance for chaining.
*/
hexColor(): RGex;
/**
* Enables or disables the global (g) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
global(enabled?: boolean): RGex;
/**
* Enables or disables the ignore case (i) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
ignoreCase(enabled?: boolean): RGex;
/**
* Enables or disables the multiline (m) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
multiline(enabled?: boolean): RGex;
/**
* Enables or disables the dotAll (s) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
dotAll(enabled?: boolean): RGex;
/**
* Enables or disables the unicode (u) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
unicode(enabled?: boolean): RGex;
/**
* Enables or disables the sticky (y) flag.
* @param enabled - If true, the flag is enabled.
* @returns The current RGex instance for chaining.
*/
sticky(enabled?: boolean): RGex;
/**
* Performs advanced password validation using the current pattern as the password to test.
* @param options - The password validation criteria.
* @returns A result object with detailed analysis of the password's strength.
*/
passwordCase(options?: PasswordValidationOptions): PasswordValidationResult;
/**
* Tests if the generated regex pattern matches a given string.
* @param input - The string to test.
* @returns True if the pattern matches, otherwise false.
*/
test(input: string): boolean;
/**
* Executes the regex pattern against a string and returns the match details.
* @param input - The string to execute the pattern on.
* @returns A `RegExpExecArray` if a match is found, otherwise null.
*/
exec(input: string): RegExpExecArray | null;
/**
* Finds all matches of the pattern in a string.
* @param input - The string to search.
* @returns An array of matches, or null if no matches are found.
*/
match(input: string): string[] | null;
/**
* Replaces occurrences of the pattern in a string.
* @param input - The string to perform the replacement on.
* @param replacement - The string or function to use for replacement.
* @returns The modified string.
*/
replace(input: string, replacement: string | ((match: string, ...args: any[]) => string)): string;
/**
* Splits a string using the regex pattern as a delimiter.
* @param input - The string to split.
* @returns An array of strings.
*/
split(input: string): string[];
/**
* Checks if the current regex pattern is syntactically valid.
* @returns True if the pattern is valid, otherwise false.
*/
isValid(): boolean;
/**
* Gets the raw regex pattern string.
* @returns The pattern string.
*/
getPattern(): string;
/**
* Gets the flags string (e.g., "gi").
* @returns The flags string.
*/
getFlags(): string;
/**
* Builds and returns the final `RegExp` object.
* @returns A `RegExp` object.
*/
build(): RegExp;
/**
* Creates a new RGex instance with the same pattern and options.
* @returns A new RGex instance.
*/
clone(): RGex;
/**
* Resets the pattern and options to their initial state.
* @returns The current RGex instance for chaining.
*/
reset(): RGex;
/**
* Returns the string representation of the regex pattern.
* @returns The pattern string.
*/
toString(): string;
/**
* Returns a JSON representation of the RGex instance.
* @returns An object containing the pattern, flags, and validity.
*/
toJSON(): {
pattern: string;
flags: string;
valid: boolean;
};
/**
* Creates a new RGex instance from a JSON object.
* @param json - An object containing the pattern and optional flags.
* @returns A new RGex instance.
* @throws Will throw an error if the pattern in the JSON is invalid.
*/
static fromJSON(json: {
pattern: string;
flags?: string;
}): RGex;
/**
* Escapes special characters in a string for use in a regex.
* @param text - The string to escape.
* @returns The escaped string.
*/
static escape(text: string): string;
/**
* Validates if a given string is a valid regex pattern.
* @param pattern - The pattern to validate.
* @returns True if the pattern is valid, otherwise false.
*/
static validate(pattern: string): boolean;
/**
* Normalizes text by converting to lowercase and removing extra whitespace.
* @param text - The text to normalize.
* @returns The normalized text.
*/
static normalize(text: string): string;
/**
* Alias for `RGex.toRegex`.
*/
static h2r: typeof RGex.toRegex;
/**
* Alias for `RGex.toValidate`.
*/
static h2v: typeof RGex.toValidate;
/**
* Alias for `RGex.humanText`.
*/
static human: typeof RGex.humanText;
/**
* Alias for `RGex.getSuggestions`.
*/
static suggest: typeof RGex.getSuggestions;
}
//# sourceMappingURL=RGex.d.ts.map