UNPKG

regulas

Version:

Regulas is a zero-dependency, lightweight regex builder for JavaScript/TypeScript.

111 lines (109 loc) 3.98 kB
/** * Base class for Regulas patterns. * Handles pattern string, quantifiers, named groups, OR segments, and variables. * * @example * const pattern = new RegulasBase("abc").oneOrMore.optional.save("myPattern"); * console.log(pattern.toString()); // (?:abc+?) */ declare class RegulasBase { protected pattern: string | (() => string); protected groupName: string; protected quantifier: string; protected orSegment: string; protected negate: boolean; variableName: string; constructor(pattern: string | (() => string)); /** Adds an OR segment `|` to the pattern */ get or(): this; /** Matches one or more of the pattern */ get oneOrMore(): this; /** Matches zero or more of the pattern */ get zeroOrMore(): this; /** Makes previous quantifier lazy (non-greedy) */ get lazy(): this; /** Makes the pattern optional (`?`) */ get optional(): this; /** Wraps the pattern in a named capturing group */ group(name: string): this; /** Saves the pattern to VARIABLE_REGISTRY for reuse */ save(name: string): this; /** Repeats the pattern a specific number of times */ repeat(...times: number[]): this; /** Converts the pattern to a regex string */ toString(): string; } /** * Group wrapper for patterns, inherits RegulasBase. * Example: `or("foo", "bar")` */ declare class RegulasGroup extends RegulasBase { constructor(pattern: string | (() => string)); } /** * Handles lookahead and lookbehind assertions. * * Example: * ```ts * const r = next("foo"); // positive lookahead (?=foo) * const r2 = prev("bar").not; // negative lookbehind (?<!bar) * ``` */ declare class RegulasLookAround { protected pattern: string | ((negate: boolean) => string); protected groupName: string; protected negate: boolean; variableName: string; constructor(pattern: string | ((negate: boolean) => string)); /** Wraps lookaround in a named capturing group */ group(name: string): this; /** Negates the lookaround */ get not(): this; /** Converts lookaround to regex string */ toString(): string; } /** * Builder for combining multiple patterns and converting to RegExp. * * @example: * const r = Regulas("foo", next("bar")).toRegex(); // /foo(?=bar)/ * */ declare class RegulasBuilder { private compiledPattern; constructor(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]); /** Returns the compiled pattern as a RegExp */ toRegex(flags?: string): RegExp; /** Wraps the compiled pattern for full string match */ get fullMatch(): this; } /** * Escapes regex special characters in a string. * Example: `escape(".+*")` -> `\\.\\+\\*` */ declare function escape(input: string): string | undefined; /** * Generates a Regulas pattern for numeric or character ranges. * * Example: * ```ts * const r = range("0-9", "a-z"); * ``` */ declare function range(...values: string[]): RegulasBase; /** * Converts a numeric range into a regex string. * Example: `generateRange(0, 9)` -> `[0-9]` */ declare function generateRange(start: number, end: number): string; /** Main Regulas entry point */ declare function Regulas(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]): RegulasBuilder; /** Matches one of the patterns */ declare function oneOf(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]): RegulasGroup; /** Matches all of the patterns */ declare function allOf(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]): RegulasGroup; /** Positive/Negative lookahead */ declare function next(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]): RegulasLookAround; /** Positive/Negative lookbehind */ declare function prev(...patterns: (string | RegulasBase | RegulasGroup | RegulasLookAround)[]): RegulasLookAround; export { Regulas, allOf, escape, generateRange, next, oneOf, prev, range };