-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce length restrictions properly
Also, add more doctests.
- Loading branch information
Showing
4 changed files
with
301 additions
and
244 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
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 |
---|---|---|
|
@@ -24,12 +24,20 @@ import Text.Email.Parser | |
, toByteString | ||
, unsafeEmailAddress) | ||
|
||
-- $setup | ||
-- This is required for all examples: | ||
-- >>> :set -XOverloadedStrings | ||
|
||
-- | Smart constructor for an email address | ||
emailAddress :: ByteString -> Maybe EmailAddress | ||
emailAddress = either (const Nothing) Just . validate | ||
|
||
-- | Checks that an email is valid and returns a version of it | ||
-- where comments and whitespace have been removed. | ||
-- | ||
-- Example: | ||
-- >>> canonicalizeEmail "spaces. are. [email protected]" | ||
-- Just "[email protected]" | ||
canonicalizeEmail :: ByteString -> Maybe ByteString | ||
canonicalizeEmail = fmap toByteString . emailAddress | ||
|
||
|
@@ -40,6 +48,12 @@ isValid = either (const False) (const True) . validate | |
|
||
-- | If you want to find out *why* a particular string is not | ||
-- an email address, use this. | ||
-- | ||
-- Examples: | ||
-- >>> validate "[email protected]" | ||
-- Right "[email protected]" | ||
-- >>> validate "not.good" | ||
-- Left "at sign > @: not enough input" | ||
validate :: ByteString -> Either String EmailAddress | ||
validate = parseOnly (addrSpec >>= \r -> endOfInput >> return r) | ||
|
Oops, something went wrong.