Skip to content

Commit

Permalink
feat(utils): 新增 isEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 26, 2023
1 parent 8f8e1ee commit a2fd62c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export * from './isBlobUrl'
export * from './isChineseIDCardNumber'
export * from './isDataUrl'
export * from './isElementVisible'
export * from './isEmail'
export * from './isNumeric'
export * from './isPossibleChineseMobilePhoneNumber'
export * from './isPromiseLike'
Expand Down Expand Up @@ -91,5 +92,4 @@ export * from './toHalfWidthString'
export * from './toSingleLineString'
export * from './traverse'
export * from './wait'

// @endindex
8 changes: 8 additions & 0 deletions src/utils/isEmail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { isEmail } from './isEmail'

describe('isEmail', () => {
test('ok', () => {
expect(isEmail('[email protected]')).toBe(true)
expect(isEmail('https://sss.cc')).toBe(false)
})
})
19 changes: 19 additions & 0 deletions src/utils/isEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// from yup
const re =
/^((([a-z]|\d|[!#$%&'*+\-/=?^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#$%&'*+\-/=?^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i

/**
* 检测传入值是否是邮箱地址。
*
* @public
* @param value 要检测的值
* @returns 返回检测结果
* @example
* ```typescript
* isEmail('[email protected]') // => true
* isEmail('http://foo.bar') // => false
* ```
*/
export function isEmail(value: string) {
return re.test(value)
}

0 comments on commit a2fd62c

Please sign in to comment.