-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of Clojure implementation
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
(ns mailchecker) | ||
|
||
(require '[clojure.string :as str]) | ||
|
||
(def ^:const blacklist (set ["a", "b"])) | ||
|
||
; Copied from https://github.com/scstarkey/noir/blob/998e846dd44f42b8e01a6977e6d22a3eff5e4542/src/noir/validation.clj#L37-L40 | ||
(defn is-email? | ||
"Returns true if email is an email address" | ||
[email] | ||
(re-matches #"(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" email)) | ||
|
||
(defn at-split | ||
"Returns list from string splitted on @ char" | ||
[email] | ||
(str/split email #"@")) | ||
|
||
(defn last-element | ||
"Returns the last element of the arr" | ||
[arr] | ||
(first | ||
(take-last 1 arr))) | ||
|
||
(defn domain-part | ||
"Returns the domain part from email" | ||
[email] | ||
(last-element (at-split email))) | ||
|
||
(defn dot-join | ||
"Returns string from arr joined with dot char" | ||
[arr] | ||
(str/join "." arr)) | ||
|
||
(defn dot-split | ||
"Returns list from string splitted on dot char" | ||
[string] | ||
(str/split string #"\.")) | ||
|
||
(defn top-domain-part | ||
"Returns the top domain for email" | ||
[email] | ||
(dot-join | ||
(take-last 2 | ||
(dot-split (domain-part email))))) | ||
|
||
(defn in-blacklist? | ||
"Returns true if email domain is not in the blacklist" | ||
[email] | ||
(contains? blacklist (top-domain-part email))) | ||
|
||
(defn valid? | ||
"Returns true if the email is valid" | ||
[email] | ||
(and | ||
(is-email? email) | ||
(not | ||
(in-blacklist? email)))) | ||
|
||
; (println (valid? "[email protected]")) | ||
; (println (valid? "example.com")) | ||
; (println (top-domain-part "[email protected]")) | ||
|
||
; Valid | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
|
||
; Invalid | ||
(println (not (valid? "plopplop.com"))) | ||
(println (not (valid? "my+ok@ok=plop.com"))) | ||
(println (not (valid? "my,[email protected]"))) | ||
|
||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
(ns mailchecker) | ||
|
||
(require '[clojure.string :as str]) | ||
|
||
(def ^:const blacklist (set [{{& listSTR }}])) | ||
|
||
; Copied from https://github.com/scstarkey/noir/blob/998e846dd44f42b8e01a6977e6d22a3eff5e4542/src/noir/validation.clj#L37-L40 | ||
(defn is-email? | ||
"Returns true if email is an email address" | ||
[email] | ||
(re-matches #"(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" email)) | ||
|
||
(defn at-split | ||
"Returns list from string splitted on @ char" | ||
[email] | ||
(str/split email #"@")) | ||
|
||
(defn last-element | ||
"Returns the last element of the arr" | ||
[arr] | ||
(first | ||
(take-last 1 arr))) | ||
|
||
(defn domain-part | ||
"Returns the domain part from email" | ||
[email] | ||
(last-element (at-split email))) | ||
|
||
(defn dot-join | ||
"Returns string from arr joined with dot char" | ||
[arr] | ||
(str/join "." arr)) | ||
|
||
(defn dot-split | ||
"Returns list from string splitted on dot char" | ||
[string] | ||
(str/split string #"\.")) | ||
|
||
(defn top-domain-part | ||
"Returns the top domain for email" | ||
[email] | ||
(dot-join | ||
(take-last 2 | ||
(dot-split (domain-part email))))) | ||
|
||
(defn in-blacklist? | ||
"Returns true if email domain is not in the blacklist" | ||
[email] | ||
(contains? blacklist (top-domain-part email))) | ||
|
||
(defn valid? | ||
"Returns true if the email is valid" | ||
[email] | ||
(and | ||
(is-email? email) | ||
(not | ||
(in-blacklist? email)))) | ||
|
||
; (println (valid? "[email protected]")) | ||
; (println (valid? "example.com")) | ||
; (println (top-domain-part "[email protected]")) | ||
|
||
; Valid | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
(println (valid? "[email protected]")) | ||
|
||
; Invalid | ||
(println (not (valid? "plopplop.com"))) | ||
(println (not (valid? "my+ok@ok=plop.com"))) | ||
(println (not (valid? "my,[email protected]"))) | ||
|
||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) | ||
(println (not (valid? "[email protected]"))) |