String Sanitizer
An intuitive & tiny string sanitizer to remove any special characters or convert strings to create filename or url 🎉🎉
Version 2 with no breaking changes
2.0.0 is launched with major updates. Pull requests are welcome. Everything is tested. Email, password and username validation is added.
Use Case
Converting or sanitizing string is easier than ever with the help of this package. You can use this utility package to sanitize even foreign languages other than English. Under the hood, regex is heavily used in this library. You can convert your string to url or filename frindly string.
🎉🎉
Installation
You can download this package from here - string-sanitizer npm
npm i string-sanitizer
Yarn installation
yarn add string-sanitizer
Usage 👀👀👀
Just pass your string as the argument. The method will return a sanitized or converted string instantly.
var string = require("string-sanitizer");
let someString = "@abcde$f$gh";
string.sanitize(someString);
Sanitization Use Cases
var string = require("string-sanitizer");
string.sanitize("a.bc@d efg#h");
string.sanitize.keepSpace("a.bc@d efg#h");
string.sanitize.keepUnicode("a.bc@d efg#hক");
string.sanitize.addFullstop("a.bc@d efg#h");
string.sanitize.addUnderscore("a.bc@d efg#h");
string.sanitize.addDash("a.bc@d efg#h");
string.sanitize.removeNumber("@abcd efgh123");
string.sanitize.keepNumber("@abcd efgh123");
string.addFullstop("abcd efgh");
string.addUnderscore("@abcd efgh");
string.addDash("@abcd efgh");
string.removeSpace("@abcd efgh");
string.removeUnderscore("@ab__cd ef_gh_");
Santization Use Case
Validation (Added in version 2.00)
Most of the time we have to validate email, password and username in our codebase. So string sanitizer starts offering validation. You pass your user generated email, username or password in this method. If it passed the checking, it will return the string as it is. If it doesn't pass the filtering, it will return false.
if you pass your email, username or password.
Email Validation
var string = require("string-sanitizer");
string.validate.isEmail("jhon@gmail.com")
string.validate.isEmail("jhongmail.com")
string.validate.isEmail("jhon@gmailcom")
string.validate.isEmail("jhon@@gmail.com")
Username Validation
Username must be free from any special characters. There will be no space and must be at least 2 characters long. Combination of numbers and letters is acceptable. Only numbers (i.e 123) are not acceptable. But only letters (i.e ea) wil be acceptable.
var string = require("string-sanitizer");
string.validate.isUsername("fazlulka")
string.validate.isUsername("Fazlulka")
string.validate.isUsername("f")
string.validate.isUsername("123")
string.validate.isUsername("fazlulka@")
string.validate.isUsername("fazlulka_")
Why minimum 2 letters not 3 letters?
Because some of the username like (@ea) is still most popular.
Why automatically lowerstring applied?
Because, most of the end user still don't understand the meaning of username. Sometimes they use upper letter. We just sanitized it. Nothing more.
Password Validation
To check a password between 6 to 15 characters which contain at least one numeric digit and a special character (Most popular)
string.validate.isPassword6to15("password1@")
string.validate.isPassword6to15("password1")
To check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character (Most Secure)
string.validate.isPassword8to15("password1Aa_");
string.validate.isPassword8to15("password1")
To check a password between 7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter. No special character accepted here. (Simple password)
string.validate.isPassword7to20("password1@_")
string.validate.isPassword7to20("password1")
To check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter
string.validate.isPassword6to20("password1Aa");
string.validate.isPassword6to20("password1")
Typescript compatitibility
Thanks to @kewitz for typescript compatibility
Thanks to @JohannesDev for remove undercore function
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate. 🏃🏃
License
MIT