Skip to content

Commit

Permalink
- supporing underscore in domain name.
Browse files Browse the repository at this point in the history
- URL is valid only if has at least 1 digit/alpha
- added loop optimizations
  • Loading branch information
omerbn committed Nov 30, 2019
1 parent b758887 commit 872dd7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/clean-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function checkTrimmingNeeded(value) {
* @param {string} value
*/
function checkLowerCaseNeeded(value) {
for (var i = 0; i < value.length; i += 1) {
let i = 0;
const m = value.length;
for (; i < m; i++) {
var code = value.charCodeAt(i);
if (code >= 65 && code <= 90) { // [A-Z]
return true;
Expand Down
17 changes: 13 additions & 4 deletions lib/is-valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ module.exports = function isValid(hostname) {
var code;
var len = hostname.length;

for (var i = 0; i < len; i += 1) {
let i = 0;
let hasAlphaOrDigit = false;
for (; i < len; i++) {
code = hostname.charCodeAt(i);
const isAlphaOrDigit = isAlpha(code) || isDigit(code);
hasAlphaOrDigit |= isAlphaOrDigit;


if (code === 46) { // '.'
if (
Expand All @@ -72,21 +77,25 @@ module.exports = function isValid(hostname) {
// Check that previous character was not already a '.'
lastCharCode === 46 ||
// Check that the previous label does not end with a '-'
lastCharCode === 45
lastCharCode === 45 ||
// check that prvious label does not end with a '_'
lastCharCode === 95
) {
return false;
}

lastDotIndex = i;
} else if (!(isAlpha(code) || isDigit(code) || code === 45)) {
// Check if there is a forbidden character in the label: [^a-zA-Z0-9-]
} else if (!isAlphaOrDigit && !code === 45 && !code === 95) {
// Check if there is a forbidden character in the label: [^a-zA-Z0-9-_]
return false;
}

lastCharCode = code;
}

return (
// has atleast 1 digit or alpha character
hasAlphaOrDigit &&
// Check that last label is shorter than 63 chars
(len - lastDotIndex - 1) <= 63 &&
// Check that the last character is an allowed trailing label character.
Expand Down

0 comments on commit 872dd7a

Please sign in to comment.