Skip to content

Commit

Permalink
feat(regexp): 新增 phoneNumberRegExpBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 27, 2023
1 parent aa549f8 commit 4a09230
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/regexp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export * from './blankCharsRegExpBuilder'
export * from './dataUrlRegExpBuilder'
export * from './emailRegExpBuilder'
export * from './emojiRegExpBuilder'
export * from './phoneNumberRegExpBuilder'
// @endindex
11 changes: 11 additions & 0 deletions src/regexp/phoneNumberRegExpBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { phoneNumberRegExpBuilder } from './phoneNumberRegExpBuilder'

describe('chineseMobilePhoneNumberRegExpBuilder', () => {
test('test', () => {
expect(phoneNumberRegExpBuilder.build().test('18088040088')).toBeTrue()
expect(phoneNumberRegExpBuilder.build().test('180880400889')).toBeTrue()
expect(
phoneNumberRegExpBuilder.build({ exact: true }).test('180880400889'),
).toBeFalse()
})
})
7 changes: 7 additions & 0 deletions src/regexp/phoneNumberRegExpBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RegExpBuilder } from './RegExpBuilder'

const baseRegExp = /1[3-9][0-9]{9}/

export const phoneNumberRegExpBuilder = new RegExpBuilder({
baseRegExp: baseRegExp,
})
2 changes: 1 addition & 1 deletion src/utils/isPossibleChineseMobilePhoneNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPossibleChineseMobilePhoneNumber } from './isPossibleChineseMobilePhoneNumber'

describe(isPossibleChineseMobilePhoneNumber.name, () => {
describe('isPossibleChineseMobilePhoneNumber', () => {
test('不可能是中国的手机号码', () => {
for (const value of [
'',
Expand Down
6 changes: 4 additions & 2 deletions src/utils/isPossibleChineseMobilePhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const re = /^1[3-9][0-9]{9}$/
import { phoneNumberRegExpBuilder } from '../regexp'

const regExp = phoneNumberRegExpBuilder.build({ exact: true })

/**
* 检测传入的值是否可能是中国的手机号码。
Expand All @@ -13,5 +15,5 @@ const re = /^1[3-9][0-9]{9}$/
* ```
*/
export function isPossibleChineseMobilePhoneNumber(value: string | number) {
return re.test(String(value))
return regExp.test(String(value))
}

0 comments on commit 4a09230

Please sign in to comment.