ztextutils
Version:
A powerful text utility library for parsing, masking, and formatting text content. Parse URLs, hashtags, mentions, emails, markdown elements, and format phone numbers, dates, and more.
266 lines (262 loc) • 6.86 kB
TypeScript
type ParseOptions = {
urls?: boolean;
hashtags?: boolean;
mentions?: boolean;
emails?: boolean;
phones?: boolean;
markdownLinks?: boolean;
markdownHeadings?: boolean;
markdownLists?: boolean;
markdownEmphasis?: boolean;
all?: boolean;
};
type TextMatch = {
type: "url" | "hashtag" | "mention" | "email" | "markdown-link" | "phone";
text: string;
value: string;
position: {
start: number;
end: number;
};
url?: string;
};
declare const patterns: {
url: RegExp;
hashtag: RegExp;
mention: RegExp;
email: RegExp;
phone: RegExp;
markdown: {
links: RegExp;
headings: RegExp;
bulletList: RegExp;
numberedList: RegExp;
bold: RegExp;
italic: RegExp;
};
};
declare class TextParser {
private options;
constructor(options?: ParseOptions);
/**
* Parse a single string and return matches
*/
parse(text: string): {
urls: string[];
hashtags: string[];
mentions: string[];
emails: string[];
phones: string[];
markdown: {
links: Array<{
text: string;
url: string;
}>;
headings: Array<{
level: number;
text: string;
}>;
lists: {
bullet: string[];
numbered: string[];
};
emphasis: {
bold: string[];
italic: string[];
};
};
};
/**
* Parse multiple strings and return matches for each
*/
parseMany(texts: string[]): {
urls: string[];
hashtags: string[];
mentions: string[];
emails: string[];
phones: string[];
markdown: {
links: Array<{
text: string;
url: string;
}>;
headings: Array<{
level: number;
text: string;
}>;
lists: {
bullet: string[];
numbered: string[];
};
emphasis: {
bold: string[];
italic: string[];
};
};
}[];
/**
* Find all linkable elements in text with their positions
*/
findLinkableElements(text: string, baseUrls?: {
hashtags?: string;
mentions?: string;
assets?: string;
}): TextMatch[];
}
declare const createParser: (options?: ParseOptions) => TextParser;
declare const defaultParser: TextParser;
type MaskToken = {
pattern: RegExp;
optional?: boolean;
transform?: (char: string) => string;
};
type MaskOptions = {
mask: string | string[];
tokens?: Record<string, MaskToken>;
placeholder?: string;
autoClear?: boolean;
stripMask?: boolean;
allowEmpty?: boolean;
};
declare class InputMask {
private static readonly DEFAULT_TOKENS;
private static readonly PRESET_MASKS;
private options;
private tokens;
constructor(options?: MaskOptions);
/**
* Apply mask to input value
*/
mask(value: string, maskOpt?: string | MaskOptions): string;
/**
* Apply mask with specific options
*/
private applyMask;
/**
* Process value against a single mask pattern
*/
private processValue;
/**
* Remove mask characters from value
*/
stripMask(value: string): string;
/**
* Validate if value matches mask pattern completely
*/
isComplete(value: string): boolean;
}
declare const createMask: (options?: MaskOptions) => InputMask;
declare const defaultMask: InputMask;
declare class TextUtils {
private parser;
private inputMask;
constructor(options?: {
parser?: ParseOptions;
mask?: MaskOptions;
});
/**
* Text Parsing Methods
*/
parse(text: string): {
urls: string[];
hashtags: string[];
mentions: string[];
emails: string[];
phones: string[];
markdown: {
links: Array<{
text: string;
url: string;
}>;
headings: Array<{
level: number;
text: string;
}>;
lists: {
bullet: string[];
numbered: string[];
};
emphasis: {
bold: string[];
italic: string[];
};
};
};
parseMany(texts: string[]): {
urls: string[];
hashtags: string[];
mentions: string[];
emails: string[];
phones: string[];
markdown: {
links: Array<{
text: string;
url: string;
}>;
headings: Array<{
level: number;
text: string;
}>;
lists: {
bullet: string[];
numbered: string[];
};
emphasis: {
bold: string[];
italic: string[];
};
};
}[];
findLinkableElements(text: string, baseUrls?: {
hashtags?: string;
mentions?: string;
assets?: string;
}): TextMatch[];
/**
* Input Masking Methods
*/
mask(value: string, maskOpt: string | MaskOptions): string;
stripMask(value: string): string;
isComplete(value: string): boolean;
/**
* Convenience Methods
*/
formatPhoneNumber(phone: string): string;
formatDate(date: string): string;
formatCurrency(amount: string | number): string;
/**
* Extract specific types of matches from text
*/
extractUrls(text: string): string[];
extractMentions(text: string): string[];
extractHashtags(text: string): string[];
extractPhones(text: string): string[];
/**
* Enhanced Combined Utilities
*/
processText(text: string, options: {
parse?: {
types?: ("urls" | "hashtags" | "mentions" | "phones" | "emails")[];
baseUrls?: {
hashtags?: string;
mentions?: string;
assets?: string;
};
};
mask?: {
phones?: boolean;
dates?: boolean;
currency?: boolean;
};
}): {
matches: TextMatch[];
filtered: TextMatch[];
originalText: string;
};
}
declare const createTextUtils: (options?: {
parser?: ParseOptions;
mask?: MaskOptions;
}) => TextUtils;
declare const defaultUtils: TextUtils;
export { InputMask, type MaskOptions, type MaskToken, type ParseOptions, type TextMatch, TextParser, TextUtils, createMask, createParser, createTextUtils, defaultMask, defaultParser, defaultUtils, patterns };