Skip to content

Commit

Permalink
refactor: rework normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Nov 21, 2023
1 parent ef140d5 commit fc91d75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/utils/__tests__/normalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ test('should handle numbers with +46', () => {
expect(normalize('+46701234567')).toEqual('0701234567')
})

test('should handle numbers with 0046', () => {
expect(normalize('0046701234567')).toEqual('0701234567')
})

test('should handle numbers with 460', () => {
expect(normalize('460701234567')).toEqual('0701234567')
})

test('should handle numbers with optional zero (0)', () => {
expect(normalize('+46(0)701234567')).toEqual('0701234567')
})
Expand Down
17 changes: 9 additions & 8 deletions lib/utils/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ export const normalize = (phoneNumber: string): string => {
.replace('(0)', '')
.replace(/\D+/gi, '')

if (normalizedNumber.substring(0, 4) === '0046') {
return `0${normalizedNumber.substring(4)}`
} else if (normalizedNumber.substring(0, 3) === '460') {
return `0${normalizedNumber.substring(3)}`
} else if (normalizedNumber.substring(0, 2) === '46') {
return `0${normalizedNumber.substring(2)}`
switch (true) {
case normalizedNumber.startsWith('0046'):
return `0${normalizedNumber.substring(4)}`
case normalizedNumber.startsWith('460'):
return `0${normalizedNumber.substring(3)}`
case normalizedNumber.startsWith('46'):
return `0${normalizedNumber.substring(2)}`
default:
return normalizedNumber
}

return normalizedNumber
}

0 comments on commit fc91d75

Please sign in to comment.