UNPKG

rgex

Version:

A powerful, chainable regex builder platform with comprehensive validation utilities

120 lines 5.34 kB
/** * @fileoverview RGex Helper Utilities - Common utility functions used throughout the library * @module Utilities * @category Utilities * @group Helper Functions * @author duongnguyen321 - https://duonguyen.site */ import type { RegexBuilderOptions } from '../../types/index.js'; /** * Escapes special characters in a string for use in a regular expression. * @param text - The input string to escape. * @returns The escaped string, safe to be inserted into a regex. */ export declare function escapeRegex(text: string): string; /** * Converts a `RegexBuilderOptions` object into a regex flags string (e.g., "gi"). * @param options - The options object to convert. * @returns A string containing the regex flags. */ export declare function optionsToFlags(options: RegexBuilderOptions): string; /** * Converts a regex flags string into a `RegexBuilderOptions` object. * @param flags - The flags string to convert. * @returns An options object representing the flags. */ export declare function flagsToOptions(flags: string): RegexBuilderOptions; /** * Validates if a given string is a syntactically correct regular expression pattern. * @param pattern - The regex pattern string to validate. * @returns `true` if the pattern is valid, otherwise `false`. */ export declare function isValidRegex(pattern: string): boolean; /** * Cleans and normalizes a string by converting it to lowercase, trimming whitespace, * and collapsing multiple whitespace characters into a single space. * @param text - The string to normalize. * @returns The normalized string. */ export declare function normalizeText(text: string): string; /** * Extracts all numeric values from a string. * @param text - The string to extract numbers from. * @returns An array of numbers found in the string. */ export declare function extractNumbers(text: string): number[]; /** * Calculates a confidence score for a matched pattern. * The score is adjusted based on whether a test value was provided, if it passed, * and the complexity of the regex pattern itself. * @param baseScore - The initial confidence score. * @param hasTestValue - Whether a test value was provided. * @param testPassed - Whether the test value passed the regex validation. * @param complexity - A multiplier for the pattern's complexity. * @returns The calculated confidence score, clamped between 0 and 1. */ export declare function calculateConfidence(baseScore: number, hasTestValue: boolean, testPassed: boolean, complexity?: number): number; /** * Merges an array of regex pattern strings using the OR `|` operator. * @param patterns - An array of pattern strings to merge. * @param useGroups - If true, wraps the entire merged pattern in a non-capturing group. * @returns The combined regex pattern string. */ export declare function mergePatterns(patterns: string[], useGroups?: boolean): string; /** * Generates sample test data for a given pattern type. * @param type - The type of data to generate (e.g., 'email', 'phone'). * @returns An array of test strings, including both valid and invalid examples. */ export declare function generateTestData(type: string): string[]; /** * Checks if a single character is a special regex character. * @param char - The character to check. * @returns `true` if the character has a special meaning in regex, otherwise `false`. */ export declare function isSpecialChar(char: string): boolean; /** * Calculates a complexity score for a regex pattern. * More complex features like lookarounds and groups increase the score. * @param pattern - The regex pattern string to analyze. * @returns A numeric complexity score. */ export declare function calculatePatternComplexity(pattern: string): number; /** * Creates a debounced function that delays invoking `func` until after `wait` milliseconds * have elapsed since the last time the debounced function was invoked. * @param func - The function to debounce. * @param wait - The number of milliseconds to delay. * @returns The new debounced function. */ export declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number): (...args: Parameters<T>) => void; /** * Creates a deep clone of an object or array. * @param obj - The object to clone. * @returns A deep copy of the object. */ export declare function deepClone<T>(obj: T): T; /** * Validates if a string is in a correct email format. * @param email - The email string to validate. * @returns `true` if the email format is valid, otherwise `false`. */ export declare function isValidEmail(email: string): boolean; /** * Formats a confidence score (a number between 0 and 1) as a percentage string. * @param confidence - The confidence score. * @returns The formatted percentage string (e.g., "85%"). */ export declare function formatConfidence(confidence: number): string; /** * Checks if the current environment is a development environment. * It checks `process.env.NODE_ENV` in both Node.js and browser-like environments. * @returns `true` if in a development environment, otherwise `false`. */ export declare function isDevelopment(): boolean; /** * Gets the current timestamp in ISO 8601 format. * @returns The current timestamp as an ISO string. */ export declare function getTimestamp(): string; //# sourceMappingURL=helpers.d.ts.map