Ott 227 add phone code (#45)
parent
4053387aaa
commit
a66e647106
@ -0,0 +1,12 @@ |
|||||||
|
import { formatPhoneCode } from '..' |
||||||
|
|
||||||
|
it('returns empty code', () => { |
||||||
|
expect(formatPhoneCode('')).toBe('') |
||||||
|
expect(formatPhoneCode('+')).toBe('') |
||||||
|
expect(formatPhoneCode('abcd')).toBe('') |
||||||
|
}) |
||||||
|
|
||||||
|
it('returns formatted code', () => { |
||||||
|
expect(formatPhoneCode('+1')).toBe('+(1)') |
||||||
|
expect(formatPhoneCode('+12345')).toBe('+(12345)') |
||||||
|
}) |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
const numberGroup = /(\d+)/ |
||||||
|
|
||||||
|
export const formatPhoneCode = (phoneCode: string) => { |
||||||
|
const group = phoneCode.match(numberGroup) |
||||||
|
const code = group?.[0] |
||||||
|
return code ? `+(${code})` : '' |
||||||
|
} |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
import { isValidPhone } from '..' |
||||||
|
|
||||||
|
it('returns false for invalid phones', () => { |
||||||
|
expect(isValidPhone('')).toBeFalsy() |
||||||
|
expect(isValidPhone('a')).toBeFalsy() |
||||||
|
expect(isValidPhone('!@#!$%')).toBeFalsy() |
||||||
|
expect(isValidPhone('+(1)123a')).toBeFalsy() |
||||||
|
expect(isValidPhone('+(1)-234-5678$')).toBeFalsy() |
||||||
|
}) |
||||||
|
|
||||||
|
it('returns true for valid phones', () => { |
||||||
|
expect(isValidPhone('+(1)')).toBeTruthy() |
||||||
|
expect(isValidPhone('+(1)12345')).toBeTruthy() |
||||||
|
expect(isValidPhone('+(1) 234 5678')).toBeTruthy() |
||||||
|
expect(isValidPhone('+(1)-234-5678')).toBeTruthy() |
||||||
|
}) |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
const phoneRegex = /^[0-9+\- ()]{1,500}$/ |
||||||
|
|
||||||
|
export const isValidPhone = (phone: string) => ( |
||||||
|
phoneRegex.test(phone.toLowerCase()) |
||||||
|
) |
||||||
Loading…
Reference in new issue