string-sanitizer
Advanced tools
Comparing version 1.1.1 to 2.0.0
141
index.js
@@ -1,55 +0,138 @@ | ||
"use strict"; | ||
'use strict'; | ||
exports.sanitize = function(str) { | ||
return str.replace(/[^a-zA-Z0-9]/g, ""); | ||
exports.sanitize = function (str) { | ||
return str.replace(/[^a-zA-Z0-9]/g, ''); | ||
}; | ||
exports.sanitize.keepUnicode = function(str) { | ||
return str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ""); | ||
exports.sanitize.keepUnicode = function (str) { | ||
return str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | ||
}; | ||
exports.sanitize.keepSpace = function(str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ""); | ||
return str2.replace(/ /g, " "); | ||
exports.sanitize.keepSpace = function (str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | ||
return str2.replace(/ /g, ' '); | ||
}; | ||
exports.sanitize.addFullstop = function(str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ""); | ||
return str2.replace(/ /g, "."); | ||
exports.sanitize.addFullstop = function (str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | ||
return str2.replace(/ /g, '.'); | ||
}; | ||
exports.sanitize.addUnderscore = function(str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ""); | ||
return str2.replace(/ /g, "_"); | ||
exports.sanitize.addUnderscore = function (str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | ||
return str2.replace(/ /g, '_'); | ||
}; | ||
exports.sanitize.addDash = function(str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ""); | ||
return str2.replace(/ /g, "-"); | ||
exports.sanitize.addDash = function (str) { | ||
var str2 = str.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | ||
return str2.replace(/ /g, '-'); | ||
}; | ||
exports.sanitize.removeNumber = function(str) { | ||
return str.replace(/[^a-zA-Z]/g, ""); | ||
exports.sanitize.removeNumber = function (str) { | ||
return str.replace(/[^a-zA-Z]/g, ''); | ||
}; | ||
exports.sanitize.keepNumber = function(str) { | ||
return str.replace(/[^a-zA-Z0-9]/g, ""); | ||
exports.sanitize.removeText = function (str) { | ||
return str.replace(/[^0-9]/g, ''); | ||
}; | ||
exports.sanitize.keepNumber = function (str) { | ||
return str.replace(/[^a-zA-Z0-9]/g, ''); | ||
}; | ||
// Add Fullstop, Underscore & Dash without sanitizing | ||
exports.addFullstop = function(str) { | ||
return str.replace(/ /g, "."); | ||
exports.addFullstop = function (str) { | ||
return str.replace(/ /g, '.'); | ||
}; | ||
exports.addUnderscore = function(str) { | ||
return str.replace(/ /g, "_"); | ||
exports.addUnderscore = function (str) { | ||
return str.replace(/ /g, '_'); | ||
}; | ||
exports.addDash = function(str) { | ||
return str.replace(/ /g, "-"); | ||
exports.addDash = function (str) { | ||
return str.replace(/ /g, '-'); | ||
}; | ||
// Remove Space without sanitizing | ||
exports.removeSpace = function(str) { | ||
return str.replace(/\s+/g, ""); | ||
exports.removeSpace = function (str) { | ||
return str.replace(/\s+/g, ''); | ||
}; | ||
exports.removeUnderscore = function (str) { | ||
return str.replace(/_+/g, ''); | ||
}; | ||
exports.validate = function (str) { | ||
console.log( | ||
`Use validate.isEmail or validate.isUsername for further validation` | ||
); | ||
return 'Use validate.isEmail or validate.isUsername for further validation'; | ||
}; | ||
//Username & Email | ||
exports.validate.isEmail = function (str) { | ||
const regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
exports.validate.isUsername = function (str) { | ||
const regex = /^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str.toLowerCase(); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
// Password Validation | ||
// To check a password between 6 to 15 characters which contain at least one numeric digit and a special character | ||
exports.validate.isPassword6to15 = function (str) { | ||
const regex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,15}$/; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
// 7 to 20 characters which contain only characters, numeric digits, underscore and first character must be a letter | ||
exports.validate.isPassword7to20 = function (str) { | ||
const regex = /^[A-Za-z]\w{7,20}$/; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
// 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter | ||
exports.validate.isPassword6to20 = function (str) { | ||
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
// 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 | ||
exports.validate.isPassword8to15 = function (str) { | ||
const regex = | ||
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/; | ||
// return regex.test(str); | ||
if (regex.test(str)) { | ||
return str; | ||
} else { | ||
return false; | ||
} | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "An intuitive & tiny string sanitizer to remove any special characters or convert strings to create filename or url 🎉🎉", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"main": "node.js", | ||
@@ -12,2 +12,3 @@ "repository": "github:fazlulkarimweb/string-sanitizer", | ||
"string sanitizer", | ||
"string validator", | ||
"string filename", | ||
@@ -14,0 +15,0 @@ "remove special characters", |
@@ -1,5 +0,8 @@ | ||
# String Sanitizer | ||
# 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 | ||
@@ -34,3 +37,3 @@ | ||
# Other Use Cases | ||
# Sanitization Use Cases | ||
@@ -52,10 +55,80 @@ ```js | ||
string.removeSpace("@abcd efgh"); // @abcdefgh | ||
string.removeUnderscore("@ab__cd ef_gh_"); // @abcd efgh | ||
``` | ||
## Show Me the Code | ||
## Santization Use Case | ||
![string-sanitizer](https://i.ibb.co/y44bXBb/Screenshot-275.png) | ||
## 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 | ||
```js | ||
var string = require("string-sanitizer"); | ||
string.validate.isEmail("jhon@gmail.com") // jhon@gmail.com | ||
string.validate.isEmail("jhongmail.com") // false | ||
string.validate.isEmail("jhon@gmailcom") // false | ||
string.validate.isEmail("jhon@@gmail.com") // false | ||
``` | ||
### 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. | ||
```js | ||
var string = require("string-sanitizer"); | ||
string.validate.isUsername("fazlulka") // fazlulka | ||
string.validate.isUsername("Fazlulka") // fazlulka (Automatically lowerstring method applied.) | ||
string.validate.isUsername("f") // false (Minimum 2 letters) | ||
string.validate.isUsername("123") // false (Only number is not acceptable) | ||
string.validate.isUsername("fazlulka@") // false (Special Character not accpeted) | ||
string.validate.isUsername("fazlulka_") // false (Special Character not accepted) | ||
``` | ||
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) | ||
```js | ||
string.validate.isPassword6to15("password1@") // password1@ | ||
string.validate.isPassword6to15("password1") // No special character | ||
``` | ||
#### 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) | ||
```js | ||
string.validate.isPassword8to15("password1Aa_"); // password1Aa_ | ||
string.validate.isPassword8to15("password1") // false ( Not matched) | ||
``` | ||
#### 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) | ||
```js | ||
string.validate.isPassword7to20("password1@_") // false (Because special character) | ||
string.validate.isPassword7to20("password1") // password1 | ||
``` | ||
#### To check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter | ||
```js | ||
string.validate.isPassword6to20("password1Aa"); // password1Aa | ||
string.validate.isPassword6to20("password1") // false ( Not matched) | ||
``` | ||
## Typescript compatitibility | ||
Thanks to @kewitz for typescript compatibility | ||
## Contributing | ||
@@ -62,0 +135,0 @@ |
132
test.js
@@ -1,2 +0,2 @@ | ||
var string = require("./index"); | ||
var string = require('./index'); | ||
@@ -18,3 +18,3 @@ // string.sanitize("a.bc@d efg#h") = abcdefgh | ||
`string.sanitize("a.bc@d efg#h") = `, | ||
string.sanitize("a.bc@d efg#h") | ||
string.sanitize('a.bc@d efg#h') | ||
); // abcdefgh | ||
@@ -24,3 +24,3 @@ | ||
`string.sanitize.keepSpace("a.bc@d efg#h") = `, | ||
string.sanitize.keepSpace("a.bc@d efg#h") | ||
string.sanitize.keepSpace('a.bc@d efg#h') | ||
); // abcd efgh | ||
@@ -30,3 +30,3 @@ | ||
`string.sanitize.keepUnicode("a.bc@d efg#hক") = `, | ||
string.sanitize.keepSpace("a.bc@d efg#hক") | ||
string.sanitize.keepSpace('a.bc@d efg#hক') | ||
); // abcd efghক | ||
@@ -36,3 +36,3 @@ | ||
`string.sanitize.addFullstop("a.bc@d efg#h") = `, | ||
string.sanitize.addFullstop("a.bc@d efg#h") | ||
string.sanitize.addFullstop('a.bc@d efg#h') | ||
); // abcd.efgh | ||
@@ -42,3 +42,3 @@ | ||
`string.sanitize.addUnderscore("a.bc@d efg#h") = `, | ||
string.sanitize.addUnderscore("a.bc@d efg#h") | ||
string.sanitize.addUnderscore('a.bc@d efg#h') | ||
); // abcd_efgh | ||
@@ -48,3 +48,3 @@ | ||
`string.sanitize.addDash("a.bc@d efg#h") = `, | ||
string.sanitize.addDash("a.bc@d efg#h") | ||
string.sanitize.addDash('a.bc@d efg#h') | ||
); // abcd-efgh | ||
@@ -54,7 +54,13 @@ | ||
`string.sanitize.removeNumber("@abcd efgh123") = `, | ||
string.sanitize.removeNumber("@abcd efgh123") | ||
string.sanitize.removeNumber('@abcd efgh123') | ||
); // abcdefgh | ||
console.log( | ||
`string.sanitize.removeText("@abcd efgh123") = `, | ||
string.sanitize.removeText('@abcd efgh123') | ||
); // 123 | ||
console.log( | ||
`string.sanitize.keepNumber("@abcd efgh123") = `, | ||
string.sanitize.keepNumber("@abcd efgh123") | ||
string.sanitize.keepNumber('@abcd efgh123') | ||
); // abcdefgh123 | ||
@@ -64,3 +70,3 @@ | ||
`string.addFullstop("abcd efgh") = `, | ||
string.addFullstop("abcd efgh") | ||
string.addFullstop('abcd efgh') | ||
); // abcd.efgh | ||
@@ -70,10 +76,110 @@ | ||
`string.addUnderscore("@abcd efgh") = `, | ||
string.addUnderscore("@abcd efgh") | ||
string.addUnderscore('@abcd efgh') | ||
); // @abcd_efgh | ||
console.log(`string.addDash("@abcd efgh") = `, string.addDash("@abcd efgh")); // @abcd-efgh | ||
console.log(`string.addDash("@abcd efgh") = `, string.addDash('@abcd efgh')); // @abcd-efgh | ||
console.log( | ||
`string.removeSpace("@abcd efgh") = `, | ||
string.removeSpace("@abcd efgh") | ||
string.removeSpace('@abcd efgh') | ||
); // @abcdefgh | ||
console.log( | ||
`string.removeUnderscore("@ab__cd ef_gh_") = `, | ||
string.removeUnderscore('@ab__cd ef_gh_') | ||
); // @abcd efgh | ||
console.log( | ||
`string.validate.isEmail("fazlulkarimrocky@gmail.com") = `, | ||
string.validate.isEmail('fazlulkarimrocky@gmail.com') | ||
); // true | ||
console.log( | ||
`string.validate.isEmail("fazlulkarimrockygmail.com") = `, | ||
string.validate.isEmail('fazlulkarimrockygmail.com') | ||
); // false | ||
console.log( | ||
`string.validate.isEmail("fazlulkarimrocky@gmailcom") = `, | ||
string.validate.isEmail('fazlulkarimrocky@gmailcom') | ||
); // false | ||
console.log( | ||
`string.validate.isEmail("fazlulkarimrocky@@gmail.com") = `, | ||
string.validate.isEmail('fazlulkarimrocky@@gmail.com') | ||
); // false | ||
console.log( | ||
`string.validate.isUsername("fazlulka") = `, | ||
string.validate.isUsername('fazlulka') | ||
); // True | ||
console.log( | ||
`string.validate.isUsername("f") = `, | ||
string.validate.isUsername('f') | ||
); // False | ||
console.log( | ||
`string.validate.isUsername("123") = `, | ||
string.validate.isUsername('123') | ||
); // False | ||
console.log( | ||
`string.validate.isUsername("Fazlulka") = `, | ||
string.validate.isUsername('Fazlulka') | ||
); // false | ||
console.log( | ||
`string.validate.isUsername("fazlulka@") = `, | ||
string.validate.isUsername('fazlulka@') | ||
); // false | ||
console.log( | ||
`string.validate.isUsername("fazlulka_") = `, | ||
string.validate.isUsername('fazlulka_') | ||
); // false | ||
//Password | ||
// 6 to 15 | ||
console.log( | ||
`string.validate.isPassword6to15("password1@_") = `, | ||
string.validate.isPassword6to15('password1@_') | ||
); // password1@ | ||
console.log( | ||
`string.validate.isPassword6to15("password1@") = `, | ||
string.validate.isPassword6to15('password1@') | ||
); // false | ||
// isPassword7to20 | ||
console.log( | ||
`string.validate.isPassword7to20("password1@_") = `, | ||
string.validate.isPassword7to20('password1@_') | ||
); // password1@ | ||
console.log( | ||
`string.validate.isPassword7to20("password1") = `, | ||
string.validate.isPassword7to20('password1') | ||
); // false | ||
// isPassword6to20 | ||
console.log( | ||
`string.validate.isPassword6to20("password1Aa") = `, | ||
string.validate.isPassword6to20('password1Aa') | ||
); // password1Aa_ | ||
console.log( | ||
`string.validate.isPassword6to20("password1") = `, | ||
string.validate.isPassword6to20('password1') | ||
); // false | ||
// isPassword8to20 | ||
console.log( | ||
`string.validate.isPassword8to15("password1Aa_") = `, | ||
string.validate.isPassword8to15('password1Aa_') | ||
); // password1Aa_ | ||
console.log( | ||
`string.validate.isPassword8to15("password1") = `, | ||
string.validate.isPassword8to15('password1') | ||
); // false |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16649
275
141
1