Skip to content

Instantly share code, notes, and snippets.

@balanza
Last active February 1, 2019 09:52
Show Gist options
  • Save balanza/3a9a50e0c59d497d6a118556ce68ec0e to your computer and use it in GitHub Desktop.
Save balanza/3a9a50e0c59d497d6a118556ce68ec0e to your computer and use it in GitHub Desktop.
// email.ts
import { Email } from './mobile';
export type Email = string & { __phantom_1549009918142: never }
export const createEmail = (input: string): Email | Error => {
 const pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0–9]{1,3}\.[0–9]{1,3}\.[0–9]{1,3}\.[0–9]{1,3}])|(([a-zA-Z\-0–9]+\.)+[a-zA-Z]{2,}))$/
 if(pattern.test(input)) return createOf<Email, string>(input)
 else return new Error(`invalid email format: ${input}`)
}
// signup.ts
import { Email, createEmail } from './email'
type Account = {
email: Email,
password: string,
createdAt: Date
}
function signup(inputForm): Account {
try {
const email: Email = createEmail(inputForm.email)
return {
email,
password: inputForm.password,
createdAt: new Date()
}
} catch (ex) {
console.error(`There has been an error during signup`, ex)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment