Skip to content

Commit

Permalink
feat: SIMPLE_EMAIL_REGEX
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Mar 4, 2024
1 parent 8bb3e3e commit 8ec0a1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export * from './semver'
export * from './web'
export * from './abort'
export * from './polyfill'
export * from './string/regex'
export * from './zod/zod.util'
export * from './zod/zod.shared.schemas'
import { z, ZodSchema, ZodError, ZodIssue } from 'zod'
Expand Down
25 changes: 25 additions & 0 deletions src/string/regex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { zEmail } from '../zod/zod.shared.schemas'
import { zIsValid } from '../zod/zod.util'
import { SIMPLE_EMAIL_REGEX } from './regex'

test.each(['[email protected]', '[email protected]', '[email protected]'])(
'email valid %',
s => {
expect(s).toMatch(SIMPLE_EMAIL_REGEX)
// cross-check with Zod
expect(zIsValid(s, zEmail)).toBe(true)
},
)

test.each([
'kirill@@naturalcycles.com',
'[email protected]',
'kirill@naturalcyclescom',
'kirillnaturalcycles.com',
'@kirillnaturalcycles.com',
'[email protected]@',
])('email invalid %', s => {
expect(s).not.toMatch(SIMPLE_EMAIL_REGEX)
// cross-check with Zod
expect(zIsValid(s, zEmail)).toBe(false)
})
6 changes: 6 additions & 0 deletions src/string/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Simple, intentionally not exhaustive regex to match an email address.
*
* Source: https://regexr.com/3e48o
*/
export const SIMPLE_EMAIL_REGEX = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/

0 comments on commit 8ec0a1c

Please sign in to comment.