pattern-matcha
v1.4.1
Published
Performs formatting and sanitization on English and Arabic text based on regular expressions to produce consistent texts.
Downloads
28
Readme
Pattern Matcha
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.
Table of Contents
Demo
See it in action here.
Installation
To install Pattern Matcha, use npm:
npm install pattern-matcha
API Usage
getArabicRules
Returns a Rules
object with rules applicable for Arabic text formatting.
import { getArabicRules } from 'pattern-matcha';
const arabicRules = getArabicRules();
getEnglishRules
Returns a Rules
object with rules applicable for English text formatting.
import { getEnglishRules } from 'pattern-matcha';
const englishRules = getEnglishRules();
getArabicFormattingRules
Retrieves rules for Arabic text formatting, specifically for scenarios where text is pasted or loses focus.
import { getArabicFormattingRules } from 'pattern-matcha';
const formattingRules = getArabicFormattingRules();
getArabicSanitizingRules
Retrieves rules for sanitizing Arabic text.
import { getArabicSanitizingRules } from 'pattern-matcha';
const sanitizingRules = getArabicSanitizingRules();
getEnglishOnBlurRules
Retrieves rules for English text formatting typically on onBlur
events.
import { getEnglishOnBlurRules } from 'pattern-matcha';
const onBlurRules = getEnglishOnBlurRules();
getArabicOnBlurRules
Retrieves rules for Arabic text formatting on onBlur
events.
import { getArabicOnBlurRules } from 'pattern-matcha';
const arabicOnBlurRules = getArabicOnBlurRules();
getArabicOnPasteRules
Retrieves rules for Arabic text formatting on onPaste
events.
import { getArabicOnPasteRules } from 'pattern-matcha';
const arabicOnPasteRules = getArabicOnPasteRules();
getEnglishOnPasteRules
Retrieves rules for English text formatting on onPaste
events.
import { getEnglishOnPasteRules } from 'pattern-matcha';
const englishOnPasteRules = getEnglishOnPasteRules();
getArabicOnSanitizeRules
Retrieves rules for Arabic text formatting on onSanitize
events.
import { getArabicOnSanitizeRules } from 'pattern-matcha';
const arabicOnSanitizeRules = getArabicOnSanitizeRules();
getRulesByName
Returns a new Rules
object containing rules specified by their names.
import { getRulesByName } from 'pattern-matcha';
const specificRules = getRulesByName('applySmartQuotes', 'cleanSpacesBeforePeriod');
Performance Considerations
- 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.
License
Pattern Matcha is MIT licensed.