-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
327 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
export * from './const/countries'; | ||
export * from './country-iso-code'; | ||
export * from './input-phone-international.component'; | ||
export * from './input-phone-international.module'; | ||
export * from './input-phone-international.options'; | ||
export * from './interfaces/country'; | ||
export * from './tokens/countries-masks'; | ||
export * from './utils/extract-value-from-event'; |
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
File renamed without changes.
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 @@ | ||
export const MASK_AFTER_CODE_REGEXP = /\([#]+\)|[#\- ]/g; |
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from '@taiga-ui/kit/pipes/field-error'; | ||
export * from '@taiga-ui/kit/pipes/filter-by-input'; | ||
export * from '@taiga-ui/kit/pipes/iso-to-country-code'; | ||
export * from '@taiga-ui/kit/pipes/stringify'; | ||
export * from '@taiga-ui/kit/pipes/stringify-content'; | ||
export * from '@taiga-ui/kit/pipes/to-country-code'; |
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,2 @@ | ||
export * from './iso-to-country-code.module'; | ||
export * from './iso-to-country-code.pipe'; |
9 changes: 9 additions & 0 deletions
9
projects/kit/pipes/iso-to-country-code/iso-to-country-code.module.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,9 @@ | ||
import {NgModule} from '@angular/core'; | ||
|
||
import {TuiIsoToCountryCodePipe} from './iso-to-country-code.pipe'; | ||
|
||
@NgModule({ | ||
declarations: [TuiIsoToCountryCodePipe], | ||
exports: [TuiIsoToCountryCodePipe], | ||
}) | ||
export class TuiIsoToCountryCodeModule {} |
18 changes: 18 additions & 0 deletions
18
projects/kit/pipes/iso-to-country-code/iso-to-country-code.pipe.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,18 @@ | ||
import {Inject, Pipe, PipeTransform} from '@angular/core'; | ||
import {TuiCountryIsoCode} from '@taiga-ui/i18n'; | ||
import {TUI_COUNTRIES_MASKS} from '@taiga-ui/kit/tokens'; | ||
import {tuiIsoToCountryCode} from '@taiga-ui/kit/utils'; | ||
|
||
@Pipe({ | ||
name: `tuiIsoToCountryCode`, | ||
}) | ||
export class TuiIsoToCountryCodePipe implements PipeTransform { | ||
constructor( | ||
@Inject(TUI_COUNTRIES_MASKS) | ||
private readonly countriesMasks: Record<TuiCountryIsoCode, string>, | ||
) {} | ||
|
||
transform(isoCode: TuiCountryIsoCode): string { | ||
return tuiIsoToCountryCode(this.countriesMasks, isoCode); | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
projects/kit/pipes/iso-to-country-code/test/iso-to-country-code.pipe.spec.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,34 @@ | ||
import {TestBed} from '@angular/core/testing'; | ||
import {TuiCountryIsoCode} from '@taiga-ui/i18n'; | ||
|
||
import {TuiIsoToCountryCodePipe} from '../iso-to-country-code.pipe'; | ||
|
||
describe(`TuiIsoToCountryCodePipe`, () => { | ||
let pipe: TuiIsoToCountryCodePipe; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [TuiIsoToCountryCodePipe], | ||
}); | ||
|
||
pipe = TestBed.inject(TuiIsoToCountryCodePipe); | ||
}); | ||
|
||
it(`should transform US iso code to country code`, () => { | ||
const transformed = pipe.transform(TuiCountryIsoCode.US); | ||
|
||
expect(transformed).toEqual(`+1`); | ||
}); | ||
|
||
it(`should transform GB iso code to country code`, () => { | ||
const transformed = pipe.transform(TuiCountryIsoCode.GB); | ||
|
||
expect(transformed).toEqual(`+44`); | ||
}); | ||
|
||
it(`should transform AU iso code to country code`, () => { | ||
const transformed = pipe.transform(TuiCountryIsoCode.AU); | ||
|
||
expect(transformed).toEqual(`+61`); | ||
}); | ||
}); |
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,2 @@ | ||
export * from './to-country-code.module'; | ||
export * from './to-country-code.pipe'; |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
projects/kit/pipes/to-country-code/test/to-country-code.pipe.spec.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,82 @@ | ||
import {TestBed} from '@angular/core/testing'; | ||
import {TuiCountryIsoCode} from '@taiga-ui/i18n'; | ||
import {TuiIsoToCountryCodePipe, TuiToCountryCodePipe} from '@taiga-ui/kit'; | ||
|
||
describe(`TuiToCountryCodePipe`, () => { | ||
let pipe: TuiToCountryCodePipe; | ||
|
||
const testCases: Array<{ | ||
input: string; | ||
countries: TuiCountryIsoCode[]; | ||
expected: TuiCountryIsoCode | undefined; | ||
}> = [ | ||
{ | ||
input: `79123456789`, | ||
countries: [TuiCountryIsoCode.US, TuiCountryIsoCode.RU, TuiCountryIsoCode.UA], | ||
expected: TuiCountryIsoCode.RU, | ||
}, | ||
{ | ||
input: `12024561111`, | ||
countries: [TuiCountryIsoCode.US, TuiCountryIsoCode.RU, TuiCountryIsoCode.UA], | ||
expected: TuiCountryIsoCode.US, | ||
}, | ||
{ | ||
input: `380442228888`, | ||
countries: [TuiCountryIsoCode.US, TuiCountryIsoCode.RU, TuiCountryIsoCode.UA], | ||
expected: TuiCountryIsoCode.UA, | ||
}, | ||
{ | ||
input: `0123456789`, | ||
countries: [TuiCountryIsoCode.DE], | ||
expected: undefined, | ||
}, | ||
|
||
// RU or KZ phones cases | ||
|
||
{ | ||
input: `79123456789`, | ||
countries: [TuiCountryIsoCode.KZ], | ||
expected: undefined, | ||
}, | ||
{ | ||
input: `76861234568`, | ||
countries: [TuiCountryIsoCode.RU], | ||
expected: undefined, | ||
}, | ||
|
||
{ | ||
input: `79123456789`, | ||
countries: [TuiCountryIsoCode.RU, TuiCountryIsoCode.KZ], | ||
expected: TuiCountryIsoCode.RU, | ||
}, | ||
{ | ||
input: `79123456789`, | ||
countries: [TuiCountryIsoCode.KZ, TuiCountryIsoCode.RU], | ||
expected: TuiCountryIsoCode.RU, | ||
}, | ||
{ | ||
input: `76861234568`, | ||
countries: [TuiCountryIsoCode.KZ, TuiCountryIsoCode.RU], | ||
expected: TuiCountryIsoCode.KZ, | ||
}, | ||
{ | ||
input: `76861234568`, | ||
countries: [TuiCountryIsoCode.RU, TuiCountryIsoCode.KZ], | ||
expected: TuiCountryIsoCode.KZ, | ||
}, | ||
]; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [TuiIsoToCountryCodePipe, TuiToCountryCodePipe], | ||
}); | ||
|
||
pipe = TestBed.inject(TuiToCountryCodePipe); | ||
}); | ||
|
||
testCases.forEach(({input, countries, expected}) => { | ||
it(`should return expected result for input: "${input}" and countries: ${countries}`, () => { | ||
expect(pipe.transform(input, countries)).toEqual(expected); | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
import {NgModule} from '@angular/core'; | ||
|
||
import {TuiToCountryCodePipe} from './to-country-code.pipe'; | ||
|
||
@NgModule({ | ||
declarations: [TuiToCountryCodePipe], | ||
exports: [TuiToCountryCodePipe], | ||
}) | ||
export class TuiExtractCountryCodeModule {} |
60 changes: 60 additions & 0 deletions
60
projects/kit/pipes/to-country-code/to-country-code.pipe.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,60 @@ | ||
import {Inject, Pipe, PipeTransform} from '@angular/core'; | ||
import {CHAR_PLUS} from '@taiga-ui/cdk'; | ||
import {TuiCountryIsoCode} from '@taiga-ui/i18n'; | ||
import {TUI_COUNTRIES_MASKS} from '@taiga-ui/kit/tokens'; | ||
import { | ||
tuiGetMaxAllowedPhoneLength, | ||
tuiIsoToCountryCode, | ||
tuiNotKzRegion, | ||
} from '@taiga-ui/kit/utils'; | ||
|
||
@Pipe({ | ||
name: `tuiToCountryCode`, | ||
}) | ||
export class TuiToCountryCodePipe implements PipeTransform { | ||
constructor( | ||
@Inject(TUI_COUNTRIES_MASKS) | ||
private readonly countriesMasks: Record<TuiCountryIsoCode, string>, | ||
) {} | ||
|
||
transform( | ||
value: string, | ||
countries: readonly TuiCountryIsoCode[], | ||
): TuiCountryIsoCode | undefined { | ||
return countries.find(countryIsoCode => { | ||
const ruCodeTest = | ||
countryIsoCode === TuiCountryIsoCode.RU && | ||
/^[78]/.test(value) && | ||
/^(?!880[1-9 ])/.test(value) && | ||
value.length + 1 === | ||
tuiGetMaxAllowedPhoneLength( | ||
this.countriesMasks, | ||
TuiCountryIsoCode.RU, | ||
); | ||
|
||
const matched = | ||
ruCodeTest || | ||
(value.startsWith( | ||
tuiIsoToCountryCode(this.countriesMasks, countryIsoCode).replace( | ||
CHAR_PLUS, | ||
``, | ||
), | ||
) && | ||
value.length + 1 === | ||
tuiGetMaxAllowedPhoneLength(this.countriesMasks, countryIsoCode)); | ||
|
||
if (matched) { | ||
switch (countryIsoCode) { | ||
case TuiCountryIsoCode.RU: | ||
return tuiNotKzRegion(value); | ||
case TuiCountryIsoCode.KZ: | ||
return !tuiNotKzRegion(value); | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
} |
Oops, something went wrong.