-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailAddress.ts
29 lines (22 loc) · 915 Bytes
/
EmailAddress.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { Branded, Option } from "../utils";
import { some, none } from "../utils";
const isEmailAddress = (s: string): s is T => /\S+@\S+\.\S+/.test(s);
export type T = Branded<string, 'EmailAddress'>;
// create with continuation
export const createWithCont = <A>(success: (a: T) => A) => (failure: (s: string) => A) => (email: string) => {
return isEmailAddress(email) ? success(email) : failure("Email address must contain an @ sign");
};
// create directly
export const create = (email: string): Option<T> => {
const success = <A>(e: A) => some(e)
const failure = (_: unknown) => none
return createWithCont(success)(failure)(email)
};
// unwrap with continuation
export const apply = <A>(f: (s: string) => A) => (e: T) => f(e);
// unwrap directly
export const value = (e: T): string => {
const id = <A>(a: A): A => a;
return apply(id)(e)
}
// export const value = (e: T): Unwrap<T> => e;