Skip to content

Commit

Permalink
JavaScript #4 & NodeJS version #2
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 4, 2013
1 parent 93b80c3 commit e4ee4be
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 24 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

Multi-language email validation.

* Validate the format of your email
* Validate the format of your email (via [node-validator](https://github.com/chriso/node-validator/blob/master/lib/validators.js#L27) email regex)
* Validate if the email is not a temporary mail (like yopmail or others...)

This will be very helpful when you have to contact your users and you want to avoid errors causing lack of communication or want to block "spamboxes".

MailChecker currently supports:

* [NodeJS(CommonJS)](./mailchecker/tree/master/platform/node)
* [JavaScript (client-side)](./mailchecker/tree/master/platform/javascript).
* [PHP]((./mailchecker/tree/master/platform/php))
* [NodeJS](./mailchecker/tree/master/platform/node) (CommonJS)
* [JavaScript](./mailchecker/tree/master/platform/javascript) (Client-Side)
* [PHP](./mailchecker/tree/master/platform/php)
* **Easily add support for your own language with MailChecker template system and [send us a pull-request!](https://github.com/FGRibreau/mailchecker/fork_select)**

## Installation
Expand All @@ -24,7 +24,7 @@ npm install mailchecker

We accept pull-requests for enabling package manager.

## Sources
## Our current sources

[TorVPN](http://torvpn.com/temporaryemail.html)

Expand All @@ -43,7 +43,7 @@ We accept pull-requests for enabling package manager.
Regenerate libraries from list.json
-------------------------------

Just run: (requires NodeJS)
Just run (requires NodeJS):
```
./gen.js
```
11 changes: 10 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var fs = require("fs");
var p = require("path");
var mu = require('mu2');
var _ = require('lodash');
var validators = require('validator/lib/validators');
var EventEmitter2 = require('eventemitter2').EventEmitter2;

var loader = require("./loader");
Expand All @@ -28,8 +29,16 @@ Generator.parsePath = function(fullpath){
};
};

Generator.getEmailRegexp = function(){
var s = 'str.match(';
var e = ');';
var fn = validators.isEmail.toString();
return fn.substring(fn.indexOf(s)+s.length, fn.indexOf(e));
};

Generator.compile = function(fn){
var templates = this.getTemplatesSync();
var regexp = this.getEmailRegexp();
var list = loader.getList();
var listJSON = JSON.stringify(list);

Expand All @@ -39,7 +48,7 @@ Generator.compile = function(fn){
this.emit('compiling:template', template);
var d = '';
mu
.compileAndRender(template.fullpath, {list:list, listJSON:listJSON})
.compileAndRender(template.fullpath, {regexp:regexp, list:list, listJSON:listJSON})
.on('data', function(data){
d += data;
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "platform/node/",
"scripts": {
"test": "./node_modules/.bin/mocha -r should -R spec -G -u tdd",
"watch":"./node_modules/.bin/mocha -r should -R spec -G -u tdd -w"
"watch": "./node_modules/.bin/mocha -r should -R spec -G -u tdd -w"
},
"repository": {
"type": "git",
Expand All @@ -24,7 +24,8 @@
"glob": "~3.1.21",
"mu2": "~0.5.17",
"async": "~0.2.6",
"eventemitter2": "~0.4.11"
"eventemitter2": "~0.4.11",
"validator": "~0.4.25"
},
"devDependencies": {
"nodeunit": "~0.7.4",
Expand Down
22 changes: 22 additions & 0 deletions platform/javascript/mailchecker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions platform/javascript/mailchecker.tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* MailChecker(String email);
* @return {Boolean} true is the specified email is valid, false otherwise
*
* Usage
*
* <script type="text/javascript" src="mailchecker/platform/javascript/mailchecker.js"></script>
* <script type="text/javascript">
* alert(MailChecker("plop@plop.33mail.com"));
* </script>
*/

(function(global){
var isValidEmail = {{& regexp }};
var isThrowableEmail = new RegExp({{& listJSON }}.join('|'));

global.mailChecker = function(email){
if(!isValidEmail.test(email)){return false;}
return !isThrowableEmail.test(email);
};
})(window);

13 changes: 9 additions & 4 deletions platform/node/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e4ee4be

Please sign in to comment.