special-character-transliteration
v1.0.0
Published
This package sanitizes user input for creating slugs, URLs, or standard English text by converting special characters from 31 languages into English equivalents and removing emojis.
Downloads
67
Maintainers
Keywords
Readme
SpecialCharacterTransliteration
Package Name: special-character-transliteration
Version: 1.0.0
Author: Saif ur Rahman
Functionality
The special-character-transliteration
package provides utility functions to:
- Normalize special characters: Convert special characters such as
Æ
,Ŧ
, etc., to their simple English transliterations. - Remove emojis: Automatically detect and remove emojis from any string input😎❌.
This package is useful for sanitizing user input, especially for creating slugs, URLs, or handling input that requires standard English text.
How to Install
You can install the package via npm by running:
npm install special-character-transliteration
Step-by-Step Guide to Use the Package
Step 1: Install the Package
Copy the following command in your terminal to install the special-character-transliteration package in your project:
npm install special-character-transliteration
Step 2: Import the Package
In your .ts or .js file, import the SpecialCharTransliterator class from the package like this:
import { SpecialCharacterTransliterator } from 'special-character-transliteration';
Step 3: Initialize the Transliterator
Create an instance of SpecialCharacterTransliterator to access its methods.
const transliterator = new SpecialCharacterTransliterator();
Step 4: Normalize Special Characters
To replace special characters with their English transliterations, use the normalizeString method.
const input = "Æ Ŧ special characters";
const result = transliterator.normalizeString(input);
console.log(result); // Output: "AE T special characters"
Step 5: Remove Emojis from Text
To remove emojis from a string, use the removeEmojis method.
const emojiText = "Hello 😊 world 🌍";
const noEmojis = transliterator.removeEmojis(emojiText);
console.log(noEmojis); // Output: "Hello world"
Step 6: Use It in a Real Scenario (Example: Slug Sanitization)
If you're working with form fields or user input that needs to be sanitized, such as a URL slug, here’s how you can integrate it:
setSlug(event: Event) {
const slug = (event.target as HTMLInputElement).value;
// Normalize the slug to remove special characters
const normalizedSlug = transliterator.normalizeString(slug);
// Remove emojis from the slug
const sanitizedSlug = transliterator.removeEmojis(normalizedSlug);
// Set the sanitized value in your form control
this.form?.controls['url'].setValue(sanitizedSlug.trim().toLowerCase());
}