From 666a4621ac53bc971e63cae28e81b02febbf081d Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Mon, 20 Apr 2015 02:10:48 -0300 Subject: [PATCH 1/2] mailcheck --- mailcheck/mailcheck-tests.ts | 54 ++++++++++++++++++++++++++ mailcheck/mailcheck.d.ts | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 mailcheck/mailcheck-tests.ts create mode 100644 mailcheck/mailcheck.d.ts diff --git a/mailcheck/mailcheck-tests.ts b/mailcheck/mailcheck-tests.ts new file mode 100644 index 000000000..85a7b2fad --- /dev/null +++ b/mailcheck/mailcheck-tests.ts @@ -0,0 +1,54 @@ +/// +/// + +import MC = require('mailcheck'); + +var domains = ['gmail.com', 'aol.com']; +var secondLevelDomains = ['hotmail'] +var topLevelDomains = ["com", "net", "org"]; + +var superStringDistance = function(string1: string, string2: string): void { + // a string distance algorithm of your choosing +}; + +$('#email').on('blur', function() { + $(this).mailcheck({ + domains: domains, // optional + secondLevelDomains: secondLevelDomains, // optional + topLevelDomains: topLevelDomains, // optional + distanceFunction: superStringDistance, // optional + suggested: function(element, suggestion) { + // callback code + }, + empty: function(element) { + // callback code + } + }); +}); + +Mailcheck.run({ + domains: domains, // optional + secondLevelDomains: secondLevelDomains, // optional + topLevelDomains: topLevelDomains, // optional + distanceFunction: superStringDistance, // optional + suggested: function(suggestion) { + // callback code + }, + empty: function() { + // callback code + } +}); + +MC.run({ + domains: domains, // optional + secondLevelDomains: secondLevelDomains, // optional + topLevelDomains: topLevelDomains, // optional + distanceFunction: superStringDistance, // optional + suggested: function(suggested) { + // callback code + suggested.address === '' && suggested.full === '' && suggested.domain === ''; + }, + empty: function() { + // callback code + } +}); \ No newline at end of file diff --git a/mailcheck/mailcheck.d.ts b/mailcheck/mailcheck.d.ts new file mode 100644 index 000000000..b2811148a --- /dev/null +++ b/mailcheck/mailcheck.d.ts @@ -0,0 +1,75 @@ +// Type definitions for Mailcheck 1.1+ +// Project: https://github.com/mailcheck/mailcheck +// Definitions by: Paulo Cesar +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare var Mailcheck: MailcheckModule.Static; + +declare module MailcheckModule { + + export interface IDistanceFunction { + (s1: string, s2: string): number; + } + + export interface ISuggestFunction { + (email: string, domains?: string[], topLevelDomains?: string[], distanceFunction?: IDistanceFunction):void; + } + + export interface IJQuerySuggested { + (element: JQuery, suggested: ISuggestion): void; + } + + export interface ISuggested { + (suggested: ISuggestion): void; + } + + export interface ISplitEmail { + topLevelDomain?: string; + domain?: string; + address?: string; + } + + export interface ISuggestion { + address: string; + domain: string; + full: string; + } + + export interface IOptions { + domains?: string[]; + topLevelDomains?: string[]; + distanceFunction?: IDistanceFunction; + suggested?: ISuggested; + empty?: () => void; + } + + export interface IJQueryOptions extends IOptions { + suggested?: IJQuerySuggested; + empty?: (element: JQuery) => void; + } + + export interface Static { + defaultDomains: string[]; + defaultSecondLevelDomains: string[]; + defaultTopLevelDomains: string[]; + domainThreshold: number; + topLevelThreshold: number; + run(opts: IOptions):void; + suggest: ISuggestFunction; + encodeEmail(email: string): string; + splitEmail(email: string): ISplitEmail; + sift3Distance(s1: string, s2: string): number; + findClosestDomain(domain: string, domains: string[], distanceFunction?: IDistanceFunction, threshold?: number): boolean|string; + } + +} + +interface JQuery { + mailcheck(opts: MailcheckModule.IJQueryOptions): void; +} + +declare module 'mailcheck' { + export = Mailcheck; +} \ No newline at end of file From c1252aef0269ad7e4b401013a841b5bf06ef32e8 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Mon, 20 Apr 2015 02:16:35 -0300 Subject: [PATCH 2/2] finish test --- mailcheck/mailcheck-tests.ts | 11 ++++++----- mailcheck/mailcheck.d.ts | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mailcheck/mailcheck-tests.ts b/mailcheck/mailcheck-tests.ts index 85a7b2fad..4a5caa12a 100644 --- a/mailcheck/mailcheck-tests.ts +++ b/mailcheck/mailcheck-tests.ts @@ -7,8 +7,9 @@ var domains = ['gmail.com', 'aol.com']; var secondLevelDomains = ['hotmail'] var topLevelDomains = ["com", "net", "org"]; -var superStringDistance = function(string1: string, string2: string): void { +var superStringDistance = function(string1: string, string2: string): number { // a string distance algorithm of your choosing + return 0; }; $('#email').on('blur', function() { @@ -17,10 +18,10 @@ $('#email').on('blur', function() { secondLevelDomains: secondLevelDomains, // optional topLevelDomains: topLevelDomains, // optional distanceFunction: superStringDistance, // optional - suggested: function(element, suggestion) { + suggested: function(element: JQuery, suggestion: MailcheckModule.ISuggestion) { // callback code }, - empty: function(element) { + empty: function(element: JQuery) { // callback code } }); @@ -31,7 +32,7 @@ Mailcheck.run({ secondLevelDomains: secondLevelDomains, // optional topLevelDomains: topLevelDomains, // optional distanceFunction: superStringDistance, // optional - suggested: function(suggestion) { + suggested: function(suggestion: MailcheckModule.ISuggestion) { // callback code }, empty: function() { @@ -44,7 +45,7 @@ MC.run({ secondLevelDomains: secondLevelDomains, // optional topLevelDomains: topLevelDomains, // optional distanceFunction: superStringDistance, // optional - suggested: function(suggested) { + suggested: function(suggested: MailcheckModule.ISuggestion) { // callback code suggested.address === '' && suggested.full === '' && suggested.domain === ''; }, diff --git a/mailcheck/mailcheck.d.ts b/mailcheck/mailcheck.d.ts index b2811148a..c6f28d0db 100644 --- a/mailcheck/mailcheck.d.ts +++ b/mailcheck/mailcheck.d.ts @@ -21,6 +21,14 @@ declare module MailcheckModule { (element: JQuery, suggested: ISuggestion): void; } + export interface IJQueryEmpty { + (element: JQuery): void; + } + + export interface IEmpty { + (): void; + } + export interface ISuggested { (suggested: ISuggestion): void; } @@ -41,13 +49,8 @@ declare module MailcheckModule { domains?: string[]; topLevelDomains?: string[]; distanceFunction?: IDistanceFunction; - suggested?: ISuggested; - empty?: () => void; - } - - export interface IJQueryOptions extends IOptions { - suggested?: IJQuerySuggested; - empty?: (element: JQuery) => void; + suggested?: ISuggested | IJQuerySuggested; + empty?: IEmpty | IJQueryEmpty; } export interface Static { @@ -67,7 +70,7 @@ declare module MailcheckModule { } interface JQuery { - mailcheck(opts: MailcheckModule.IJQueryOptions): void; + mailcheck(opts: MailcheckModule.IOptions): void; } declare module 'mailcheck' {