This is a library that compiles a collection of useful regex patterns to be able to format texts in English and Arabic in a consistent manner.
This is useful for translators that want to be able to produce consistent looking translations with their original source texts in various lifecycles of any given text.
At the moment there are three phases for formatting:
- onPaste: These are formatting one wants to apply to a text when a text is pasted into a input field. Generally these are preformatting rules.
- onBlur: These are the formatting one wants to apply to a text anytime a text field containing the text is unfocused (ie: the blur event).
- onSanitize: These are formatting one wants to apply during a sanitization phase, generally saves or querying and normalization of text so that an app can eliminate noise such as diacritics from the text.
See it in action here.
To install Pattern Matcha, use npm:
npm install pattern-matcha
Returns a Rules
object with rules applicable for Arabic text formatting.
import { getArabicRules } from 'pattern-matcha';
const arabicRules = getArabicRules();
Returns a Rules
object with rules applicable for English text formatting.
import { getEnglishRules } from 'pattern-matcha';
const englishRules = getEnglishRules();
Retrieves rules for Arabic text formatting, specifically for scenarios where text is pasted or loses focus.
import { getArabicFormattingRules } from 'pattern-matcha';
const formattingRules = getArabicFormattingRules();
Retrieves rules for sanitizing Arabic text.
import { getArabicSanitizingRules } from 'pattern-matcha';
const sanitizingRules = getArabicSanitizingRules();
Retrieves rules for English text formatting typically on onBlur
events.
import { getEnglishOnBlurRules } from 'pattern-matcha';
const onBlurRules = getEnglishOnBlurRules();
Retrieves rules for Arabic text formatting on onBlur
events.
import { getArabicOnBlurRules } from 'pattern-matcha';
const arabicOnBlurRules = getArabicOnBlurRules();
Retrieves rules for Arabic text formatting on onPaste
events.
import { getArabicOnPasteRules } from 'pattern-matcha';
const arabicOnPasteRules = getArabicOnPasteRules();
Retrieves rules for English text formatting on onPaste
events.
import { getEnglishOnPasteRules } from 'pattern-matcha';
const englishOnPasteRules = getEnglishOnPasteRules();
Retrieves rules for Arabic text formatting on onSanitize
events.
import { getArabicOnSanitizeRules } from 'pattern-matcha';
const arabicOnSanitizeRules = getArabicOnSanitizeRules();
Returns a new Rules
object containing rules specified by their names.
import { getRulesByName } from 'pattern-matcha';
const specificRules = getRulesByName('applySmartQuotes', 'cleanSpacesBeforePeriod');
- Regex Complexity: Given the reliance on regular expressions, performance may vary based on the complexity and length of the patterns and the text being processed. Complex regex patterns can lead to increased time complexity, particularly for longer texts.
- Number of Rules: The runtime complexity increases with the number of rules being applied, as each rule implies a separate scan of the text.
- Text Length: Longer texts will naturally take more time to process as each rule needs to be applied across the entire text body.
Pattern Matcha is MIT licensed.