First version of the interface

parent 501e7fea
/bun.lockb
/generated-docs/
/node_modules/
/output/
/.spago/
\ No newline at end of file
This diff is collapsed.
# purescript-address-rfc2821
A lightweight interface to validating email addresses with https://www.npmjs.com/package/address-rfc2821
\ No newline at end of file
A lightweight interface to validating email addresses with
https://www.npmjs.com/package/address-rfc2821.
To build:
```sh
nix develop
bun install
bun spago build
bun spago test
```
{
"nodes": {
"easy-ps": {
"inputs": {
"flake-utils": "flake-utils"
},
"locked": {
"lastModified": 1710161569,
"narHash": "sha256-lcIRIOFCdIWEGyKyG/tB4KvxM9zoWuBRDxW+T+mvIb0=",
"owner": "justinwoo",
"repo": "easy-purescript-nix",
"rev": "117fd96acb69d7d1727df95b6fde9d8715e031fc",
"type": "github"
},
"original": {
"owner": "justinwoo",
"repo": "easy-purescript-nix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1685518550,
"narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1713145326,
"narHash": "sha256-m7+IWM6mkWOg22EC5kRUFCycXsXLSU7hWmHdmBfmC3s=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "53a2c32bc66f5ae41a28d7a9a49d321172af621e",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"easy-ps": "easy-ps",
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
{
description = "purescript-address-rfc2821 development environment";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
inputs.easy-ps = {
url = "github:justinwoo/easy-purescript-nix";
};
outputs =
{ self
, flake-utils
, nixpkgs
, easy-ps
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
easy-ps-pkgs = easy-ps.packages.${system};
in
{
devShell = pkgs.mkShell {
packages = with pkgs; [
# See https://github.com/NixOS/nixpkgs/issues/59209.
bashInteractive
];
buildInputs = with pkgs; [
bun
nodejs
easy-ps-pkgs.purs-0_15_15
];
};
}
);
}
{
"dependencies": { "address-rfc2821": "^2.1.2" },
"devDependencies": {
"purescript": "0.15.15",
"purs": "^0.3.0",
"spago": "next"
}
}
\ No newline at end of file
This diff is collapsed.
workspace:
packageSet:
registry: 50.10.0
extraPackages: {}
package:
name: address-rfc2821
dependencies:
- ffi-simple
- prelude: ">=6.0.1 <7.0.0"
test:
main: Test.Main
dependencies:
- debug
- spec
- spec-mocha
# Optional section for configuring the `spago publish` command.
# If you intend to publish your package, this section becomes mandatory.
publish:
# The version of your package. This follows semver rules, but with no
# prereleases - so only major.minor.patch.
version: 0.1.0
# The license for your source, in SPDX format: https://spdx.dev/
license: AGPL-3.0-or-later
# Optional list of globs to include in the published archive, in
# addition to the list of files that the Registry includes by default:
# https://github.com/purescript/registry-dev/blob/master/SPEC.md#always-included-files
#include:
#- "test/**/*.purs"
# Optional list of globs to exclude from the published archive, in
# addition to the list of files that the Registry includes by default:
# https://github.com/purescript/registry-dev/blob/master/SPEC.md#always-excluded-files
# Note that the Registry will first apply the `include` list, then
# the `exclude` one, as detailed in the specification:
# https://github.com/purescript/registry-dev/blob/master/SPEC.md#33-manifest
exclude:
- "test/graphs/**/*"
# The place where the Registry will fetch the source from.
# This is optional since you might want to release the code without
# publishing to the Registry, which is what this is needed for.
#location:
# url: https://gitlab.iscpif.fr/gargantext/purescript-address-rfc2821.git
-- | This is a lightweight interface to
-- | [address-rfc2821](https://www.npmjs.com/package/address-rfc2821).
-- | Use this module to parse email envelope strings. (See
-- | [here](https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression)
-- | where it is recommended against using regular expressions to
-- | parse emails).
module Data.RFC2821
(
module Data.RFC2821.Internal
) where
import Data.RFC2821.Internal
import { Address } from 'address-rfc2821';
const makeAddressImpl = Left => Right => email => {
try {
return new Right(new Address(email));
}
catch(e) { return Left(e); }
}
export { makeAddressImpl };
module Data.RFC2821.Internal
(
Address
, makeAddress
, host
, original
, originalHost
, user
, address
, toString
)
where
import Data.Either (Either(..))
import FFI.Simple ((..), (...))
import Prelude
foreign import data Address :: Type
foreign import makeAddressImpl :: forall a b. (String -> Either String a)
-> (Address -> Either b Address)
-> String
-> Either String Address
-- | Attempt to parse given string as an email adddress
makeAddress :: String -> Either String Address
makeAddress email = makeAddressImpl Left Right email
-- | Return the (parsed) host part `Address`
host :: Address -> String
host a = a .. "host"
-- | Return the original string from `Address`
original :: Address -> String
original a = a .. "original"
-- | Return the original host from `Address`
originalHost :: Address -> String
originalHost a = a .. "original_host"
-- | Return the (parsed) user parf of `Address`
user :: Address -> String
user a = a .. "user"
-- | Format `Address` as a string
address :: Address -> String
address a = a ... "address" $ []
-- | Format `Address` as an envelope
toString :: Address -> String
toString a = a ... "toString" $ []
module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Aff (launchAff_)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (runSpec)
import Test.Data.RFC2821.Spec (spec)
main :: Effect Unit
main = launchAff_ do
runSpec [consoleReporter] spec
module Test.Data.RFC2821.Spec where
import Data.Either (Either(..), isLeft, isRight)
import Data.RFC2821 as RFC2821
import Debug (spy)
import Prelude
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
spec :: Spec Unit
spec =
describe "RFC2821" do
it "works for simple email" do
let address = RFC2821.makeAddress "admin@localhost.localdomain"
let original = (\a -> RFC2821.original a) <$> address
let host = (\a -> RFC2821.host a) <$> address
let user = (\a -> RFC2821.user a) <$> address
original `shouldEqual` (Right "admin@localhost.localdomain")
host `shouldEqual` (Right "localhost.localdomain")
user `shouldEqual` (Right "admin")
it "works for incorrect email" do
let address = RFC2821.makeAddress "admin"
-- shouldSatisfy requires a Show instance for the underlying type
isLeft address `shouldEqual` true
it "shouldn't parse email with symbols surrounding it" $ do
let address = RFC2821.makeAddress "'admin@localhost.localdomain'"
isLeft address `shouldEqual` true
it "parses email with <>" $ do
let address = RFC2821.makeAddress "<admin@localhost.localdomain>"
isRight address `shouldEqual` true
it "reformatting address as string works" $ do
let address = RFC2821.makeAddress "<admin@localhost.localdomain>"
let addressStr = (\a -> RFC2821.address a) <$> address
addressStr `shouldEqual` (Right "admin@localhost.localdomain")
it "reformatting address as envelope works" $ do
let address = RFC2821.makeAddress "admin@localhost.localdomain"
let envelopeStr = (\a -> RFC2821.toString a) <$> address
envelopeStr `shouldEqual` (Right "<admin@localhost.localdomain>")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment