regexpstructor
Version: 
Build regular expressions in a safe and readable way.
299 lines (297 loc) • 9.48 kB
TypeScript
interface Node<T> {
}
interface RootNode<T> {
    prefix?: string;
    suffix?: string;
    node: Node<T>;
    flags?: string;
}
interface ConstructorArg {
    source?: string;
    node?: Node<never>;
    flags?: string;
    prefix?: string;
    suffix?: string;
    sanitize?: boolean;
}
type ReadablePrimitive = RegExpstructor;
declare class RegExpstructor {
    #private;
    constructor(arg?: ConstructorArg, init?: boolean);
    private static _getRootNode;
    /**
     * @description creates a ReStructor from a rootNode type
     * @param node
     */
    static from(node: RootNode<never>): RegExpstructor;
    /**
     * @description creates ReStructors from a variety of source formats
     * @param x
     */
    static of(x: string | number | RegExp | RegExpstructor): RegExpstructor;
    /**
     * @description disjunction (one must match) between the argument expressions
     * @param xs
     */
    static or(...xs: (string | number | RegExp | RegExpstructor)[]): RegExpstructor;
    /**
     * @description conjunction (all must match) of the argument expressions
     * @param xs
     */
    static seq(...xs: (string | number | RegExp | RegExpstructor)[]): RegExpstructor;
    /**
     * @description a ReStructor that matches a whitespace
     */
    static whitespace: ReadablePrimitive;
    /**
     * @description an empty ReStructor
     */
    static empty: ReadablePrimitive;
    /**
     * @description match one digit
     */
    static digit: ReadablePrimitive;
    /**
     * @description match a tab-character
     */
    static tab: ReadablePrimitive;
    /**
     * @description matches a whole word
     */
    static word: ReadablePrimitive;
    /**
     * @description match any kind of line break or new-lines
     */
    static linebreak: ReadablePrimitive;
    static any: RegExpstructor;
    private static _preSanitize;
    /**
     * @description Control start-of-line matching
     * @param [enable=true] whether to enable this behaviour
     */
    assertStartOfLine(enable?: boolean): RegExpstructor;
    /**
     * @description Control end-of-line matching
     * @param [enable=true] whether to enable this behaviour
     */
    assertEndOfLine(enable?: boolean): RegExpstructor;
    /**
     * @description Look for the value passed
     * @param value value to find
     */
    then(value: string | RegExp | number | RegExpstructor): RegExpstructor;
    /**
     * @description Add an optional branch for matching
     * @param value value to find
     */
    maybe(value: string | RegExp | number | RegExpstructor): RegExpstructor;
    /**
     * @description Add alternative expressions
     * @param value value to find
     */
    or(value: string | RegExp | number | RegExpstructor): RegExpstructor;
    /**
     * @description Any character any number of times
     * @param lazy match least number of characters
     */
    anything(lazy?: boolean): RegExpstructor;
    /**
     * @description Anything but these characters
     * @param value characters to not match
     * @param lazy match least number of characters
     */
    anythingBut(value: string | number | string[] | number[], lazy?: boolean): RegExpstructor;
    /**
     * @description Any character(s) at least once
     */
    something(): RegExpstructor;
    /**
     * @description Any character at least one time except for these characters
     * @param value characters to not match
     */
    somethingBut(value: string | number | string[] | number[]): RegExpstructor;
    /**
     * @description Match any of the given characters
     * @param value characters to match
     */
    anyOf(value: string | number | string[] | number[]): RegExpstructor;
    /**
     * @description Match some of the given characters
     * @param {(string|number|string[]|number[])} value characters to match
     */
    someOf(value: string | number | string[] | number[]): RegExpstructor;
    /**
     * @description Match one chartacter of the given characters
     * @param {(string|number|string[]|number[])} value characters to match
     */
    oneOf(value: string | number | string[] | number[]): RegExpstructor;
    /**
     * @description Shorthand for anyOf(value)
     * @param {string|number} value value to find
     */
    any(value: string | number): RegExpstructor;
    /**
     * @description Ensure that the parameter does not follow (negative lookahead)
     * @param {string|number|RegExp|RegExpstructor} value
     */
    assertNotFollowedBy(value: string | number | RegExp | RegExpstructor): RegExpstructor;
    /**
     * @description Ensure that the parameter does not follow (negative lookahead)
     * @param {string|number|RegExp|RegExpstructor} value
     */
    notFollowedBy(value: string | number | RegExp | RegExpstructor): RegExpstructor;
    /**
     * @description Ensure that the parameter does follow (positive lookahead)
     * @param {string|number|RegExp|RegExpstructor} value
     */
    assertFollowedBy(value: string | number | RegExp | RegExpstructor): RegExpstructor;
    /**
     * @description Ensure that the parameter does follow (positive lookahead)
     * @param {string|number|RegExp|RegExpstructor} value
     */
    followedBy(value: string | number | RegExp | RegExpstructor): RegExpstructor;
    /**
     * @description Match any character in these ranges
     * @example RegExpstructor.empty.charOfRanges(["a","z"], ["0", "9"]) // [a-z0-9]
     * @param {...([string, string])} characterRanges total number of elements must be even
     *
     *
     */
    charOfRanges(...characterRanges: [string, string][]): RegExpstructor;
    /**
     * @description Match any character that is not in these ranges
     * @example RegExpstructor.empty.charNotOfRanges(["a","z"], ["0", "9"]) // [^a-z0-9]
     * @param {...([string, string])} characterRanges total number of elements must be even
     *
     *
     */
    charNotOfRanges(...characterRanges: [string, string][]): RegExpstructor;
    /**
     * @description Match a Line break
     */
    lineBreak(): RegExpstructor;
    /**
     * @description A shorthand for lineBreak() for html-minded users
     */
    br(): RegExpstructor;
    /**
     * @description Match a tab character
     */
    tab(): RegExpstructor;
    /**
     * @description Match any alphanumeric sequence
     */
    word(): RegExpstructor;
    /**
     * @description Match a single digit
     */
    digit(): RegExpstructor;
    /**
     * @description Match a single whitespace
     */
    whitespace(): RegExpstructor;
    /**
     * @description Add a regex flag - default flags are: "gi"
     * @param {string} flag
     */
    addFlag(flag?: string): RegExpstructor;
    /**
     * @description Remove a regex flag - default flags are: "gi"
     * @param {string} flag
     */
    removeFlag(flag: string): RegExpstructor;
    /**
     *
     * @param {string} flag
     * @returns {boolean}
     */
    hasFlag(flag: string): boolean;
    /**
     * @description Adds an "i" regex flag - default flags are: "gm"
     * @param enable
     */
    withAnyCase(enable?: boolean): RegExpstructor;
    /**
     * @description Removes a "g" regex flag - default flags are: "gm"
     * @param enable `true` means no "g" flag
     */
    stopAtFirst(enable?: boolean): RegExpstructor;
    /**
     *
     * @param enable
     *
     */
    global(enable?: boolean): RegExpstructor;
    /**
     *
     * @param flag
     *
     */
    toggleFlag(flag: string): RegExpstructor;
    /**
     * @description Removes any set "m" regex flag - default flags are: "gm"
     * @param enable `true` means "m" flag will be removed
     */
    searchOneLine(enable?: boolean): RegExpstructor;
    /**
     * @description match the expression <min> to <max> times
     * @example ```js
     * Sx("abc").whitespace().repeat(2, 4).compile().toString() === /(?:abc\w){2,4}/gm.toString()
     * ```
     * @param min
     * @param max
     */
    repeat(min: number, max: number): RegExpstructor;
    /**
     * @description match the expression exactly <n> times
     * @example ```js
     * Sx("abc").whitespace().repeatExactly(5).compile().toString() === /(?:abc\w){5}/gm.toString()
     * ```
     * @param n must be > 0
     */
    repeatExactly(n: number): RegExpstructor;
    /**
     * @description the expression should match at least once
     *
     */
    oneOrMore(): RegExpstructor;
    /**
     * @description the expression should match zero or more times
     * @param lazy enable lazy (non greedy) matching
     */
    zeroOrMore(lazy?: boolean): RegExpstructor;
    /**
     *
     * @param name optionally name your capturing group
     */
    capture(name?: string): RegExpstructor;
    /**
     *
     * @param name optionally name your capturing group
     */
    group(name?: string): RegExpstructor;
    /**
     * @description compile the ReStructor to a RegExp
     *
     */
    compile(): RegExp;
}
/**
 *
 * @param {string|number|RegExp|RegExpstructor} [value]
 *
 */
declare function ReStructor(value?: string | number | RegExp | RegExpstructor): RegExpstructor;
declare namespace ReStructor {
    var or: typeof RegExpstructor.or;
    var seq: typeof RegExpstructor.seq;
    var of: typeof RegExpstructor.of;
    var digit: RegExpstructor;
    var word: RegExpstructor;
    var empty: RegExpstructor;
    var linebreak: RegExpstructor;
    var tab: RegExpstructor;
    var whitespace: RegExpstructor;
    var any: RegExpstructor;
}
export { ReStructor as default };