-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add phone number formatting and validation
- Loading branch information
1 parent
78799c9
commit 059c529
Showing
6 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { normalizePhone, removeNonDigits } from './phone' | ||
|
||
describe('phone', () => { | ||
describe('removeNonDigits', () => { | ||
it('should return empty string for empty input', () => { | ||
expect(removeNonDigits('')).toBe('') | ||
}) | ||
|
||
it('should remove non-digits', () => { | ||
expect(removeNonDigits('a12-34/5 6b_78@90)')).toBe('1234567890') | ||
}) | ||
}) | ||
|
||
describe('normalizePhone', () => { | ||
it('should return empty string for empty input', () => { | ||
expect(normalizePhone('')).toBe('') | ||
}) | ||
|
||
it('should format first 3 digits with parenthesis', () => { | ||
expect(normalizePhone('123')).toBe('(123') | ||
}) | ||
|
||
it('should add space and format next 3 digits', () => { | ||
expect(normalizePhone('123456')).toBe('(123) 456') | ||
}) | ||
|
||
it('should add hyphen when exceeding 6 digits', () => { | ||
expect(normalizePhone('1234567')).toBe('(123) 456-7') | ||
}) | ||
|
||
it('should strip non-digits before formatting', () => { | ||
expect(normalizePhone('(123) abc 456-7890')).toBe('(123) 456-7890') | ||
}) | ||
|
||
it('should truncate input longer than 10 digits', () => { | ||
expect(normalizePhone('12345678901234')).toBe('(123) 456-7890') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const removeNonDigits = (value: string): string => { | ||
return value.replace(/\D/g, '') | ||
} | ||
|
||
export const normalizePhone = (value: string): string => { | ||
const digits = removeNonDigits(value) | ||
|
||
if (!digits.length) return '' | ||
|
||
// Format: (XXX) XXX-XXXX | ||
if (digits.length <= 3) { | ||
return `(${digits}` | ||
} | ||
|
||
if (digits.length <= 6) { | ||
return `(${digits.slice(0, 3)}) ${digits.slice(3)}` | ||
} | ||
|
||
return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6, 10)}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters