funtool
Version:
A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.
149 lines (143 loc) • 4.96 kB
TypeScript
declare const plugins: {
readonly email: DefineRegexPlugin<"email">;
readonly mobile: DefineRegexPlugin<"mobile">;
readonly alpha: DefineRegexPlugin<"alpha">;
readonly chinese: DefineRegexPlugin<"chinese">;
readonly ipv6: DefineRegexPlugin<"ipv6">;
readonly postal: DefineRegexPlugin<"postal">;
readonly username: DefineRegexPlugin<"username">;
readonly ipv4: DefineRegexPlugin<"ipv4">;
readonly IDCard: DefineRegexPlugin<"IDCard">;
readonly url: DefineRegexPlugin<"url">;
readonly qq: DefineRegexPlugin<"qq">;
readonly landline: DefineRegexPlugin<"landline">;
readonly number: DefineRegexPlugin<"number">;
readonly nonAscii: DefineRegexPlugin<"nonAscii">;
readonly nonLatin: DefineRegexPlugin<"nonLatin">;
readonly password: DefineRegexPlugin<"password">;
readonly html: DefineRegexPlugin<"html">;
};
type RuleName = keyof typeof plugins;
/**
* Class for validating a string against registered rules or regular expressions.
*/
/**
* @description Class for validating a string against registered rules or regular expressions.
* @author xiaoqiujun
* @date 23/05/2025
* @export
* @class Checker
*/
declare class Checker {
/** Input string to be validated */
private input;
/** Current validation result */
private result;
/** Flag indicating whether the next validation should be negated */
private isNegated;
/**
* Create a new Checker instance.
* @param input The input string to validate.
*/
constructor(input: string);
/**
* Apply a rule or regular expression to the input string.
* @param rule A registered rule name or a RegExp instance.
* @returns The Checker instance for method chaining.
* @throws Will throw an error if the rule name is not registered.
*/
use(rule: RuleName | RegExp | (string & {})): this;
/**
* Negate the result of the next validation rule.
* @returns The Checker instance for method chaining.
*/
not(): this;
/**
* Get the final validation result.
* @returns `true` if the validation passed, otherwise `false`.
*/
isValid(): boolean;
}
/**
* @description Class for replacing parts of a string using a rule or regular expression.
* @author xiaoqiujun
* @date 23/05/2025
* @export
* @class Replacer
*/
declare class Replacer {
/** The original input string */
private input;
/** The pattern to use for replacement */
private pattern?;
/**
* Create a new Replacer instance.
* @param input The input string to perform replacements on.
*/
constructor(input: string);
/**
* Set the rule or regular expression to use for replacement.
* @param rule A registered rule name or a RegExp object.
* @returns The Replacer instance for method chaining.
* @throws Will throw an error if the rule name is not registered.
*/
use(rule: RuleName | RegExp | (string & {})): this;
/**
* Perform the replacement operation.
* @param replacer A replacement string or a function to transform matches.
* @returns The Replacer instance for method chaining.
* @throws Will throw an error if no pattern has been defined via `.use()`.
*/
with(replacer: string | ((match: string) => string)): this;
/**
* Get the final string after all replacements.
* @returns The transformed string.
*/
result(): string;
}
type RegexValidateContext<T extends string> = {
name: T;
pattern: RegExp;
input: string;
};
interface DefineRegexPlugin<T extends string> {
name: T;
pattern: RegExp;
validate: (ctx: RegexValidateContext<T>) => boolean;
}
/**
* Checker and Replacer tools.
*/
declare class Regex {
private static instance;
private constructor();
static getInstance(): Regex;
/**
* Create a new Checker instance for validating input strings.
*
* @param input The string to validate.
* @returns {Checker} A Checker instance for rule validation.
*/
checker(input: string): Checker;
/**
* Create a new Replacer instance for performing regex-based replacements.
*
* @param input The string to perform replacements on.
* @returns {Replacer} A Replacer instance for rule-based replacements.
*/
replacer(input: string): Replacer;
/**
* Define and register a new custom plugin rule.
*
* Throws an error if the plugin name conflicts with internal reserved names.
*
* @template Name
* @param {RulePlugin<Name>} plugin - The plugin definition object to register.
* @returns {RulePlugin<Name>} The registered plugin.
* @throws {Error} If plugin name is reserved/internal.
*/
definePlugin<RuleName extends string>(plugin: DefineRegexPlugin<RuleName>): DefineRegexPlugin<RuleName>;
}
declare const regex: Regex & (typeof plugins);
export { regex };
export type { DefineRegexPlugin, RuleName };