-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(intl-phone-input): format input value (#703)
Co-authored-by: Александр Серов <ARserov@alfabank.ru> Co-authored-by: Alexander Yatsenko <reme3d2y@gmail.com>
- Loading branch information
1 parent
f74cdc7
commit b5e91ed
Showing
6 changed files
with
122 additions
and
31 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
32 changes: 32 additions & 0 deletions
32
packages/intl-phone-input/src/utils/format-phone-with-unclearable-country-code.test.ts
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,32 @@ | ||
import { Country } from '@alfalab/utils'; | ||
import { formatPhoneWithUnclearableCountryCode } from './format-phone-with-unclearable-country-code'; | ||
|
||
describe('formatPhoneWithUnclearableCountryCode', () => { | ||
it('should work', () => { | ||
const ru = { | ||
dialCode: '7', | ||
} as Country; | ||
|
||
const az = { | ||
dialCode: '994', | ||
} as Country; | ||
expect(formatPhoneWithUnclearableCountryCode('', ru)).toEqual('+7'); | ||
expect(formatPhoneWithUnclearableCountryCode('+', ru)).toEqual('+7'); | ||
expect(formatPhoneWithUnclearableCountryCode('7', ru)).toEqual('+7'); | ||
expect(formatPhoneWithUnclearableCountryCode('+7', ru)).toEqual('+7'); | ||
expect(formatPhoneWithUnclearableCountryCode('+17', ru)).toEqual('+71'); | ||
expect(formatPhoneWithUnclearableCountryCode('+71', ru)).toEqual('+71'); | ||
expect(formatPhoneWithUnclearableCountryCode('1+7', ru)).toEqual('+71'); | ||
|
||
expect(formatPhoneWithUnclearableCountryCode('', az)).toEqual('+994'); | ||
expect(formatPhoneWithUnclearableCountryCode('+', az)).toEqual('+994'); | ||
expect(formatPhoneWithUnclearableCountryCode('+9', az)).toEqual('+994'); | ||
expect(formatPhoneWithUnclearableCountryCode('+99', az)).toEqual('+994'); | ||
expect(formatPhoneWithUnclearableCountryCode('+994', az)).toEqual('+994'); | ||
expect(formatPhoneWithUnclearableCountryCode('1+994', az)).toEqual('+9941'); | ||
expect(formatPhoneWithUnclearableCountryCode('+1994', az)).toEqual('+9941'); | ||
expect(formatPhoneWithUnclearableCountryCode('+9194', az)).toEqual('+9941'); | ||
expect(formatPhoneWithUnclearableCountryCode('+9914', az)).toEqual('+9941'); | ||
expect(formatPhoneWithUnclearableCountryCode('+9941', az)).toEqual('+9941'); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/intl-phone-input/src/utils/format-phone-with-unclearable-country-code.ts
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,32 @@ | ||
import { Country } from '@alfalab/utils'; | ||
import { getPhoneDiff } from './get-phone-diff'; | ||
|
||
/** | ||
* Форматирует телефон с неудаляемым кодом страны | ||
*/ | ||
export const formatPhoneWithUnclearableCountryCode = (phone: string, country: Country) => { | ||
const defaultValue = `+${country.dialCode}`; | ||
// При попытке стереть код страны возвращаем дефолтное значение | ||
if (phone.length < defaultValue.length) { | ||
return defaultValue; | ||
} | ||
|
||
// Если код страны совпадает, даем вводить значение | ||
if (phone.substr(1, country.dialCode.length) === country.dialCode) { | ||
return phone; | ||
} | ||
|
||
const lengthDiff = phone.substr(1).length - country.dialCode.length; | ||
// Если разница длины нового значения и длины кода страны равна 1, то определяем отличающийся символ и ставим его после кода | ||
if (lengthDiff === 1) { | ||
const diff = getPhoneDiff(phone, country); | ||
// Если не смогли вычислить отличающийся символ, то возвращаем дефолтное значение | ||
if (!diff) { | ||
return defaultValue; | ||
} | ||
|
||
return `+${country.dialCode}${diff}`; | ||
} | ||
|
||
return phone; | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/intl-phone-input/src/utils/get-phone-diff.test.ts
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,30 @@ | ||
import { Country } from '@alfalab/utils'; | ||
import { getPhoneDiff } from './get-phone-diff'; | ||
|
||
describe('getPhoneDiff', () => { | ||
it('should work', () => { | ||
const ru = { | ||
dialCode: '7', | ||
} as Country; | ||
|
||
const az = { | ||
dialCode: '994', | ||
} as Country; | ||
expect(getPhoneDiff('', ru)).toEqual(null); | ||
expect(getPhoneDiff('+', ru)).toEqual(null); | ||
expect(getPhoneDiff('7', ru)).toEqual(null); | ||
expect(getPhoneDiff('+7', ru)).toEqual(null); | ||
expect(getPhoneDiff('+17', ru)).toEqual('1'); | ||
expect(getPhoneDiff('1+7', ru)).toEqual('1'); | ||
|
||
expect(getPhoneDiff('', az)).toEqual(null); | ||
expect(getPhoneDiff('+', az)).toEqual(null); | ||
expect(getPhoneDiff('+9', az)).toEqual(null); | ||
expect(getPhoneDiff('+99', az)).toEqual(null); | ||
expect(getPhoneDiff('+994', az)).toEqual(null); | ||
expect(getPhoneDiff('1+994', az)).toEqual('1'); | ||
expect(getPhoneDiff('+1994', az)).toEqual('1'); | ||
expect(getPhoneDiff('+9194', az)).toEqual('1'); | ||
expect(getPhoneDiff('+9914', az)).toEqual('1'); | ||
}); | ||
}); |
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,23 @@ | ||
import { Country } from '@alfalab/utils'; | ||
|
||
/** | ||
* Возвращает разницу между значением и кодом страны | ||
*/ | ||
export const getPhoneDiff = (value: string, country: Country) => { | ||
if (!value || value === `+${country.dialCode}`) return null; | ||
|
||
const dialRegex = country.dialCode.split('').reduce((acc, item, index) => { | ||
return `${acc}(?<dial${index}>\\d+)*${item}`; | ||
}, ''); | ||
const regex = new RegExp(`(?<beforePlusSign>\\d+)*\\+${dialRegex}`); | ||
|
||
const result = value.match(regex); | ||
|
||
if (!result) return null; | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (!result.hasOwnProperty('groups')) return null; | ||
const { groups } = result; | ||
if (!groups) return null; | ||
|
||
return Object.values(groups).join(''); | ||
}; |