Permalink
Browse files

Add Jshint and code fixes to pass check

Add jshint task to grunt.  Update src to pass jshint checks.  Replace
email.trim() with regex so that it still works in IE8.
  • Loading branch information...
henrythewasp committed Nov 22, 2016
1 parent a3ce3c0 commit 06fd2502fb530d312ca39bf6db1f16c689b96c62
Showing with 40 additions and 21 deletions.
  1. +8 −2 Gruntfile.coffee
  2. +9 −0 jshint.json
  3. +3 −2 package.json
  4. +19 −16 src/mailcheck.js
  5. +1 −1 src/mailcheck.min.js
View
@@ -2,6 +2,11 @@ module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON('package.json'),
jshint:
files: 'src/mailcheck.js'
options:
jshintrc: 'jshint.json'
uglify:
options:
banner: '/*! <%= pkg.name %> v<%= pkg.version %> @licence MIT */'
@@ -21,6 +26,7 @@ module.exports = (grunt) ->
grunt.loadNpmTasks 'grunt-contrib-uglify'
grunt.loadNpmTasks 'grunt-jasmine-node'
grunt.loadNpmTasks 'grunt-contrib-jshint'
grunt.registerTask 'default', ['jasmine_node', 'uglify']
grunt.registerTask 'test', ['jasmine_node']
grunt.registerTask 'default', ['jshint', 'jasmine_node', 'uglify']
grunt.registerTask 'test', ['jshint', 'jasmine_node']
View
@@ -0,0 +1,9 @@
{
"undef": true,
"unused": "vars",
"browser": true,
"devel": true,
"esversion": 3,
"curly": true,
"forin": true
}
View
@@ -31,11 +31,12 @@
"docs": "https://github.com/mailcheck/mailcheck/blob/master/README.md",
"dependencies": {},
"devDependencies": {
"jasmine-node": "1.x",
"grunt": "~0.4",
"grunt-cli": "~0.1.9",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-uglify": "~0.2",
"grunt-jasmine-node": "~0.1.0",
"grunt-cli": "~0.1.9"
"jasmine-node": "1.x"
},
"main": "src/mailcheck.js",
"repository": {
View
@@ -1,3 +1,5 @@
/* globals define, module, jQuery */
/*
* Mailcheck https://github.com/mailcheck/mailcheck
* Author
@@ -37,13 +39,13 @@ var Mailcheck = {
opts.topLevelDomains = opts.topLevelDomains || Mailcheck.defaultTopLevelDomains;
opts.distanceFunction = opts.distanceFunction || Mailcheck.sift4Distance;
var defaultCallback = function(result){ return result };
var defaultCallback = function(result){ return result; };
var suggestedCallback = opts.suggested || defaultCallback;
var emptyCallback = opts.empty || defaultCallback;
var result = Mailcheck.suggest(Mailcheck.encodeEmail(opts.email), opts.domains, opts.secondLevelDomains, opts.topLevelDomains, opts.distanceFunction);
return result ? suggestedCallback(result) : emptyCallback()
return result ? suggestedCallback(result) : emptyCallback();
},
suggest: function(email, domains, secondLevelDomains, topLevelDomains, distanceFunction) {
@@ -75,7 +77,7 @@ var Mailcheck = {
var closestTopLevelDomain = this.findClosestDomain(emailParts.topLevelDomain, topLevelDomains, distanceFunction, this.topLevelThreshold);
if (emailParts.domain) {
var closestDomain = emailParts.domain;
closestDomain = emailParts.domain;
var rtrn = false;
if(closestSecondLevelDomain && closestSecondLevelDomain != emailParts.secondLevelDomain) {
@@ -90,7 +92,7 @@ var Mailcheck = {
rtrn = true;
}
if (rtrn == true) {
if (rtrn) {
return { address: emailParts.address, domain: closestDomain, full: emailParts.address + "@" + closestDomain };
}
}
@@ -136,7 +138,7 @@ var Mailcheck = {
sift4Distance: function(s1, s2, maxOffset) {
// sift4: https://siderite.blogspot.com/2014/11/super-fast-and-accurate-string-distance.html
if (maxOffset === undefined) {
maxOffset = 5 //default
maxOffset = 5; //default
}
if (!s1||!s1.length) {
@@ -203,15 +205,15 @@ var Mailcheck = {
}
//if matching characters are found, remove 1 from both cursors (they get incremented at the end of the loop)
//so that we can have only one code block handling matches
for (var i = 0; i < maxOffset && (c1+i<l1 || c2+i<l2); i++) {
if ((c1 + i < l1) && (s1.charAt(c1 + i) == s2.charAt(c2))) {
c1+= i-1;
for (var j = 0; j < maxOffset && (c1+j<l1 || c2+j<l2); j++) {
if ((c1 + j < l1) && (s1.charAt(c1 + j) == s2.charAt(c2))) {
c1+= j-1;
c2--;
break;
}
if ((c2 + i < l2) && (s1.charAt(c1) == s2.charAt(c2 + i))) {
if ((c2 + j < l2) && (s1.charAt(c1) == s2.charAt(c2 + j))) {
c1--;
c2+= i-1;
c2+= j-1;
break;
}
}
@@ -230,7 +232,8 @@ var Mailcheck = {
},
splitEmail: function(email) {
var parts = email.trim().split('@');
email = email !== null ? (email.replace(/^\s*/, '').replace(/\s*$/, '')) : null; // trim() not exist in old IE!

This comment has been minimized.

Show comment
Hide comment
@pke

pke Feb 23, 2017

If email === null then the next line email.split('@') will throw an exception.
The function should return false if !!email and could use feature detection for the trimming:
email = (typeof email.trim === "function") ? email().trim() : email.replace(/^\s*/, '').replace(/\s*$/, '');

Are u still processing PR for this package?

@pke

pke Feb 23, 2017

If email === null then the next line email.split('@') will throw an exception.
The function should return false if !!email and could use feature detection for the trimming:
email = (typeof email.trim === "function") ? email().trim() : email.replace(/^\s*/, '').replace(/\s*$/, '');

Are u still processing PR for this package?

var parts = email.split('@');
if (parts.length < 2) {
return false;
@@ -247,7 +250,7 @@ var Mailcheck = {
var sld = '';
var tld = '';
if (domainParts.length == 0) {
if (domainParts.length === 0) {
// The address does not have a top-level domain
return false;
} else if (domainParts.length == 1) {
@@ -256,8 +259,8 @@ var Mailcheck = {
} else {
// The address has a domain and a top-level domain
sld = domainParts[0];
for (var i = 1; i < domainParts.length; i++) {
tld += domainParts[i] + '.';
for (var j = 1; j < domainParts.length; j++) {
tld += domainParts[j] + '.';
}
tld = tld.substring(0, tld.length - 1);
}
@@ -267,7 +270,7 @@ var Mailcheck = {
secondLevelDomain: sld,
domain: domain,
address: parts.join('@')
}
};
},
// Encode the email address to prevent XSS but leave in valid
@@ -316,6 +319,6 @@ if (typeof window !== 'undefined' && window.jQuery) {
opts.email = this.val();
Mailcheck.run(opts);
}
};
})(jQuery);
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 06fd250

Please sign in to comment.