├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /index.d.ts: -------------------------------------------------------------------------------- 1 | type TSanitizer = (s: string) => string; 2 | 3 | interface ISanitize extends TSanitizer { 4 | keepUnicode: TSanitizer; 5 | keepSpace: TSanitizer; 6 | addFullstop: TSanitizer; 7 | addUnderscore: TSanitizer; 8 | addDash: TSanitizer; 9 | removeNumber: TSanitizer; 10 | keepNumber: TSanitizer; 11 | } 12 | 13 | const sanitize: ISanitize; 14 | const addFullstop: TSanitizer; 15 | const addUnderscore: TSanitizer; 16 | const addDash: TSanitizer; 17 | const removeSpace: TSanitizer; 18 | 19 | export { sanitize, addFullstop, addUnderscore, addDash, removeSpace }; 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sanitize = function (str) { 4 | return str.replace(/[^a-zA-Z0-9]/g, ''); 5 | }; 6 | 7 | exports.sanitize.keepUnicode = function (str) { 8 | return str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); 9 | }; 10 | 11 | exports.sanitize.keepSpace = function (str) { 12 | var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); 13 | return str2.replace(/ /g, ' '); 14 | }; 15 | 16 | exports.sanitize.addFullstop = function (str) { 17 | var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); 18 | return str2.replace(/ /g, '.'); 19 | }; 20 | 21 | exports.sanitize.addUnderscore = function (str) { 22 | var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); 23 | return str2.replace(/ /g, '_'); 24 | }; 25 | 26 | exports.sanitize.addDash = function (str) { 27 | var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); 28 | return str2.replace(/ /g, '-'); 29 | }; 30 | 31 | exports.sanitize.removeNumber = function (str) { 32 | return str.replace(/[^a-zA-Z]/g, ''); 33 | }; 34 | 35 | exports.sanitize.removeText = function (str) { 36 | return str.replace(/[^0-9]/g, ''); 37 | }; 38 | 39 | exports.sanitize.keepNumber = function (str) { 40 | return str.replace(/[^a-zA-Z0-9]/g, ''); 41 | }; 42 | 43 | exports.addFullstop = function (str) { 44 | return str.replace(/ /g, '.'); 45 | }; 46 | exports.addUnderscore = function (str) { 47 | return str.replace(/ /g, '_'); 48 | }; 49 | 50 | exports.addDash = function (str) { 51 | return str.replace(/ /g, '-'); 52 | }; 53 | 54 | // Remove Space without sanitizing 55 | exports.removeSpace = function (str) { 56 | return str.replace(/\s+/g, ''); 57 | }; 58 | 59 | exports.removeUnderscore = function (str) { 60 | return str.replace(/_+/g, ''); 61 | }; 62 | 63 | exports.validate = function (str) { 64 | console.log( 65 | `Use validate.isEmail or validate.isUsername for further validation` 66 | ); 67 | return 'Use validate.isEmail or validate.isUsername for further validation'; 68 | }; 69 | 70 | //Username & Email 71 | exports.validate.isEmail = function (str) { 72 | const regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 73 | if (regex.test(str)) { 74 | return str; 75 | } else { 76 | return false; 77 | } 78 | }; 79 | 80 | exports.validate.isUsername = function (str) { 81 | const regex = /^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i; 82 | if (regex.test(str)) { 83 | return str.toLowerCase(); 84 | } else { 85 | return false; 86 | } 87 | }; 88 | 89 | // To check a password between 6 to 15 characters which contain at least one numeric digit and a special character 90 | exports.validate.isPassword6to15 = function (str) { 91 | const regex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,15}$/; 92 | if (regex.test(str)) { 93 | return str; 94 | } else { 95 | return false; 96 | } 97 | }; 98 | 99 | // 7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter 100 | exports.validate.isPassword7to20 = function (str) { 101 | const regex = /^[A-Za-z]\w{7,20}$/; 102 | if (regex.test(str)) { 103 | return str; 104 | } else { 105 | return false; 106 | } 107 | }; 108 | 109 | // 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter 110 | exports.validate.isPassword6to20 = function (str) { 111 | const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/; 112 | if (regex.test(str)) { 113 | return str; 114 | } else { 115 | return false; 116 | } 117 | }; 118 | 119 | // 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 120 | exports.validate.isPassword8to15 = function (str) { 121 | const regex = 122 | /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/; 123 | if (regex.test(str)) { 124 | return str; 125 | } else { 126 | return false; 127 | } 128 | }; 129 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Md. Fazlul Karim (http://twitter.com/fazlulkarimweb) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-sanitizer", 3 | "author": "Md Fazlul Karim (https://twitter.com/fazlulkarimweb)", 4 | "description": "An intuitive & tiny string sanitizer to remove any special characters or convert strings to create filename or url 🎉🎉", 5 | "version": "2.0.1", 6 | "main": "index.js", 7 | "repository": "github:fazlulkarimweb/string-sanitizer", 8 | "license": "MIT", 9 | "keywords": [ 10 | "string sanitizer", 11 | "string validator", 12 | "email validator", 13 | "username validator", 14 | "password validator", 15 | "string filename", 16 | "remove special characters", 17 | "convert string", 18 | "sanitize string", 19 | "string url", 20 | "string converter", 21 | "special characters removal", 22 | "custom string", 23 | "beautiful string" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # String Sanitizer 2 | 3 | An intuitive & tiny string sanitizer to remove any special characters or convert strings to create filename or url. Validate email, password & username too. 🎉🎉 4 | 5 | 6 | > **✅ Update:** 2.0.0 is launched with major updates. No breaking changes. Email, password and username validation is added. Everything is tested. 7 | 8 | ## Use Case 9 | 10 | 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. Besides you can validate email, passwords and username too. 11 | 🎉🎉 12 | 13 | ## Installation 14 | 15 | You can download this package from here - [string-sanitizer npm](https://www.npmjs.com/package/string-sanitizer) 16 | 17 | ```bash 18 | npm i string-sanitizer 19 | ``` 20 | 21 | Yarn installation 22 | 23 | ```bash 24 | yarn add string-sanitizer 25 | ``` 26 | 27 | ## Usage 👀👀👀 28 | 29 | Just pass your string as the argument. The method will return a sanitized or converted string instantly. 30 | 31 | ```js 32 | var string = require("string-sanitizer"); 33 | let someString = "@abcde$f$gh"; 34 | string.sanitize(someString); // abcdefgh 35 | ``` 36 | 37 | ## Sanitization use cases 38 | 39 | ```js 40 | var string = require("string-sanitizer"); 41 | 42 | string.sanitize("a.bc@d efg#h"); // abcdefgh 43 | string.sanitize.keepSpace("a.bc@d efg#h"); // abcd efgh 44 | string.sanitize.keepUnicode("a.bc@d efg#hক"); // abcd efghক 45 | string.sanitize.addFullstop("a.bc@d efg#h"); // abcd.efgh 46 | string.sanitize.addUnderscore("a.bc@d efg#h"); // abcd_efgh 47 | string.sanitize.addDash("a.bc@d efg#h"); // abcd-efgh 48 | string.sanitize.removeNumber("@abcd efgh123"); // abcdefgh 49 | string.sanitize.keepNumber("@abcd efgh123"); // abcdefgh123 50 | string.addFullstop("abcd efgh"); // abcd.efgh 51 | string.addUnderscore("@abcd efgh"); // @abcd_efgh 52 | string.addDash("@abcd efgh"); // @abcd-efgh 53 | string.removeSpace("@abcd efgh"); // @abcdefgh 54 | string.removeUnderscore("@ab__cd ef_gh_"); // @abcd efgh 55 | ``` 56 | 57 | **✅ Screenshot** 58 | 59 | ![string-sanitizer](https://i.ibb.co/y44bXBb/Screenshot-275.png) 60 | 61 | # Validation 👀👀👀 62 | **Added in version 2.0.0** 63 | 64 | Most of the time we have to validate email, password and username in our codebase. So string sanitizer starts offering validation along with sanitization. You pass your user generated email, username or password in this method. If it passes the filter, it will return the string as it is. If it doesn't pass the filter, it will return false. 65 | 66 | 67 | ## Email Validation ✅ 68 | 69 | ```js 70 | var string = require("string-sanitizer"); 71 | 72 | string.validate.isEmail("jhon@gmail.com") // jhon@gmail.com 73 | string.validate.isEmail("jhongmail.com") // false 74 | string.validate.isEmail("jhon@gmailcom") // false 75 | string.validate.isEmail("jhon@@gmail.com") // false 76 | ``` 77 | 78 | ## Username Validation ✅ 79 | 80 | 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. 81 | 82 | ```js 83 | var string = require("string-sanitizer"); 84 | 85 | string.validate.isUsername("fazlulka") // fazlulka 86 | string.validate.isUsername("Fazlulka") // fazlulka (Automatically lowerstring method applied.) 87 | string.validate.isUsername("f") // false (Minimum 2 letters) 88 | string.validate.isUsername("123") // false (Only number is not acceptable) 89 | string.validate.isUsername("fazlulka@") // false (Special Character not accpeted) 90 | string.validate.isUsername("fazlulka_") // false (Special Character not accepted) 91 | ``` 92 | 93 | Why minimum 2 letters not 3 letters? 94 | Because some of the username like (@ea) is still most popular. 95 | 96 | Why automatically lowerstring applied? 97 | 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. 98 | 99 | ## Password Validation ✅ 100 | 101 | > **1. Most popular:** To check a password **between 6 to 15 characters** which contain at least one numeric digit and a special character 102 | 103 | 104 | ```js 105 | string.validate.isPassword6to15("password1@") // password1@ 106 | string.validate.isPassword6to15("password1") // false 107 | ``` 108 |
109 | 110 | > **2. Most Secure:** 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 111 | 112 | ```js 113 | string.validate.isPassword8to15("password1Aa_"); // password1Aa_ 114 | string.validate.isPassword8to15("password1") // false 115 | ``` 116 |
117 | 118 | 119 | > **3. Simpler Password:** To check a password **between 6 to 20 characters** which contain at least one numeric digit, one uppercase and one lowercase letter 120 | 121 | 122 | ```js 123 | string.validate.isPassword6to20("password1Aa"); // password1Aa 124 | string.validate.isPassword6to20("password1") // false 125 | ``` 126 |
127 | 128 | > **4. Easy Password:** 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. 129 | > 130 | 131 | 132 | ```js 133 | string.validate.isPassword7to20("password1") // password1 134 | string.validate.isPassword7to20("password1@_") // false (No special character allowed) 135 | ``` 136 |
137 | 138 | ### Typescript compatitibility 139 | 140 | Thanks to @kewitz for typescript compatibility 141 | 142 | Thanks to @JohannesDev for removeUnderscore function 143 | 144 | 145 | ### Contributing 146 | 147 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 148 | 149 | Please make sure to update tests as appropriate. 🏃‍🏃‍ 150 | 151 | ### License 152 | 153 | [MIT](https://github.com/fazlulkarimweb/string-sanitizer/blob/master/license) 154 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var string = require('./index'); 2 | 3 | // string.sanitize("a.bc@d efg#h") = abcdefgh 4 | // string.sanitize.keepSpace("a.bc@d efg#h") = abcd efgh 5 | // string.sanitize.keepUnicode("a.bc@d efg#hক") = abcd efghক 6 | // string.sanitize.addFullstop("a.bc@d efg#h") = abcd.efgh 7 | // string.sanitize.addUnderscore("a.bc@d efg#h") = abcd_efgh 8 | // string.sanitize.addDash("a.bc@d efg#h") = abcd-efgh 9 | // string.sanitize.removeNumber("@abcd efgh123") = abcdefgh 10 | // string.sanitize.keepNumber("@abcd efgh123") = abcdefgh123 11 | // string.addFullstop("abcd efgh") = abcd.efgh 12 | // string.addUnderscore("@abcd efgh") = @abcd_efgh 13 | // string.addDash("@abcd efgh") = @abcd-efgh 14 | // string.removeSpace("@abcd efgh") = @abcdefgh 15 | 16 | console.log( 17 | `string.sanitize("a.bc@d efg#h") = `, 18 | string.sanitize('a.bc@d efg#h') 19 | ); // abcdefgh 20 | 21 | console.log( 22 | `string.sanitize.keepSpace("a.bc@d efg#h") = `, 23 | string.sanitize.keepSpace('a.bc@d efg#h') 24 | ); // abcd efgh 25 | 26 | console.log( 27 | `string.sanitize.keepUnicode("a.bc@d efg#hক") = `, 28 | string.sanitize.keepSpace('a.bc@d efg#hক') 29 | ); // abcd efghক 30 | 31 | console.log( 32 | `string.sanitize.addFullstop("a.bc@d efg#h") = `, 33 | string.sanitize.addFullstop('a.bc@d efg#h') 34 | ); // abcd.efgh 35 | 36 | console.log( 37 | `string.sanitize.addUnderscore("a.bc@d efg#h") = `, 38 | string.sanitize.addUnderscore('a.bc@d efg#h') 39 | ); // abcd_efgh 40 | 41 | console.log( 42 | `string.sanitize.addDash("a.bc@d efg#h") = `, 43 | string.sanitize.addDash('a.bc@d efg#h') 44 | ); // abcd-efgh 45 | 46 | console.log( 47 | `string.sanitize.removeNumber("@abcd efgh123") = `, 48 | string.sanitize.removeNumber('@abcd efgh123') 49 | ); // abcdefgh 50 | 51 | console.log( 52 | `string.sanitize.removeText("@abcd efgh123") = `, 53 | string.sanitize.removeText('@abcd efgh123') 54 | ); // 123 55 | 56 | console.log( 57 | `string.sanitize.keepNumber("@abcd efgh123") = `, 58 | string.sanitize.keepNumber('@abcd efgh123') 59 | ); // abcdefgh123 60 | 61 | console.log( 62 | `string.addFullstop("abcd efgh") = `, 63 | string.addFullstop('abcd efgh') 64 | ); // abcd.efgh 65 | 66 | console.log( 67 | `string.addUnderscore("@abcd efgh") = `, 68 | string.addUnderscore('@abcd efgh') 69 | ); // @abcd_efgh 70 | 71 | console.log(`string.addDash("@abcd efgh") = `, string.addDash('@abcd efgh')); // @abcd-efgh 72 | 73 | console.log( 74 | `string.removeSpace("@abcd efgh") = `, 75 | string.removeSpace('@abcd efgh') 76 | ); // @abcdefgh 77 | 78 | console.log( 79 | `string.removeUnderscore("@ab__cd ef_gh_") = `, 80 | string.removeUnderscore('@ab__cd ef_gh_') 81 | ); // @abcd efgh 82 | 83 | console.log( 84 | `string.validate.isEmail("fazlulkarimrocky@gmail.com") = `, 85 | string.validate.isEmail('fazlulkarimrocky@gmail.com') 86 | ); // true 87 | 88 | console.log( 89 | `string.validate.isEmail("fazlulkarimrockygmail.com") = `, 90 | string.validate.isEmail('fazlulkarimrockygmail.com') 91 | ); // false 92 | 93 | console.log( 94 | `string.validate.isEmail("fazlulkarimrocky@gmailcom") = `, 95 | string.validate.isEmail('fazlulkarimrocky@gmailcom') 96 | ); // false 97 | 98 | console.log( 99 | `string.validate.isEmail("fazlulkarimrocky@@gmail.com") = `, 100 | string.validate.isEmail('fazlulkarimrocky@@gmail.com') 101 | ); // false 102 | 103 | console.log( 104 | `string.validate.isUsername("fazlulka") = `, 105 | string.validate.isUsername('fazlulka') 106 | ); // True 107 | 108 | console.log( 109 | `string.validate.isUsername("f") = `, 110 | string.validate.isUsername('f') 111 | ); // False 112 | 113 | console.log( 114 | `string.validate.isUsername("123") = `, 115 | string.validate.isUsername('123') 116 | ); // False 117 | 118 | console.log( 119 | `string.validate.isUsername("Fazlulka") = `, 120 | string.validate.isUsername('Fazlulka') 121 | ); // false 122 | 123 | console.log( 124 | `string.validate.isUsername("fazlulka@") = `, 125 | string.validate.isUsername('fazlulka@') 126 | ); // false 127 | 128 | console.log( 129 | `string.validate.isUsername("fazlulka_") = `, 130 | string.validate.isUsername('fazlulka_') 131 | ); // false 132 | 133 | //Password 134 | // 6 to 15 135 | console.log( 136 | `string.validate.isPassword6to15("password1@_") = `, 137 | string.validate.isPassword6to15('password1@_') 138 | ); // password1@ 139 | 140 | console.log( 141 | `string.validate.isPassword6to15("password1@") = `, 142 | string.validate.isPassword6to15('password1@') 143 | ); // false 144 | 145 | // isPassword7to20 146 | console.log( 147 | `string.validate.isPassword7to20("password1@_") = `, 148 | string.validate.isPassword7to20('password1@_') 149 | ); // password1@ 150 | 151 | console.log( 152 | `string.validate.isPassword7to20("password1") = `, 153 | string.validate.isPassword7to20('password1') 154 | ); // false 155 | 156 | // isPassword6to20 157 | console.log( 158 | `string.validate.isPassword6to20("password1Aa") = `, 159 | string.validate.isPassword6to20('password1Aa') 160 | ); // password1Aa_ 161 | 162 | console.log( 163 | `string.validate.isPassword6to20("password1") = `, 164 | string.validate.isPassword6to20('password1') 165 | ); // false 166 | 167 | // isPassword8to20 168 | console.log( 169 | `string.validate.isPassword8to15("password1Aa_") = `, 170 | string.validate.isPassword8to15('password1Aa_') 171 | ); // password1Aa_ 172 | 173 | console.log( 174 | `string.validate.isPassword8to15("password1") = `, 175 | string.validate.isPassword8to15('password1') 176 | ); // false 177 | --------------------------------------------------------------------------------