Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing countries, new input #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ To install this component to an external project, follow the procedure:
### @Input() onlyNumbers
Allows only numeric values (default: true)

### @Input() firstlySelectedCountries
An ISO 639-1 country codes array can be provided to define which country flag should
be displayed in same dial code situation. Array is empty by default

## License
* License: MIT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor {
@Input()
defaultCountry: string;

@Input()
firstlySelectedCountries: string[] = [];

@Input()
maxLength = 15;

Expand All @@ -49,6 +52,7 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor {
onModelChange: Function;

countries: Country[];
prioritizedCountries: Country[];
locales: CountryCode;
selectedCountry: Country;
countryFilter: string;
Expand All @@ -72,6 +76,7 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor {

ngOnInit(): void {
this.countries = this.service.getCountries();
this.prioritizedCountries = this.getPrioritizedCountries();
this.locales = this.localeService.getLocales(this.locale);
this.translateCountryNames();
}
Expand Down Expand Up @@ -156,9 +161,9 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor {
}

private findPrefix(prefix: string) {
let foundPrefixes: Country[] = this.countries.filter((country: Country) => prefix.startsWith(country.dialCode));
let foundPrefixes: Country[] = _.filter(this.countries, (country: Country) => prefix.startsWith(country.dialCode));
this.selectedCountry = !_.isEmpty(foundPrefixes)
? IntPhonePrefixComponent.reducePrefixes(foundPrefixes)
? IntPhonePrefixComponent.reducePrefixes(foundPrefixes, this.prioritizedCountries)
: null;
}

Expand All @@ -168,16 +173,35 @@ export class IntPhonePrefixComponent implements OnInit, ControlValueAccessor {
this.onTouch();
}

private getPrioritizedCountries() {
const selected: Country[] = [];
_.map(this.firstlySelectedCountries, (countryCode: string) => {
selected.push(_.find(this.countries, (country: Country) => {
return country.countryCode === countryCode;
}));
});
return _.compact(selected);
}

private static startsWithPlus(text: string): boolean {
return text.startsWith(PLUS);
}

private static reducePrefixes(foundPrefixes: Country[]) {
private static reducePrefixes(foundPrefixes: Country[], prioritizedCountries: Country[]) {
return foundPrefixes.reduce(
(first: Country, second: Country) =>
first.dialCode.length > second.dialCode.length
? first
: second
);
(first: Country, second: Country) => this.checkPriorities(first, second, prioritizedCountries));
}

private static checkPriorities(first: Country, second: Country, prioritizedCountries: Country[]) {
const checkFirst = _.find(prioritizedCountries, (item: Country) => item.countryCode === first.countryCode);
const checkSecond = _.find(prioritizedCountries, (item: Country) => item.countryCode === second.countryCode);

if (first.dialCode.length === second.dialCode.length) {
return checkFirst || checkSecond || second;
}

return first.dialCode.length > second.dialCode.length
? first
: second;
}
}
6 changes: 3 additions & 3 deletions src/lib/src/interface/country.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Country {
name: string,
dialCode: string
countryCode: string,
name: string;
dialCode: string;
countryCode: string;
}
19 changes: 16 additions & 3 deletions src/lib/src/service/country.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable} from '@angular/core';
import {Country} from "../interface/country.interface";
import {Country} from '../..';

@Injectable()
export class CountryService {
Expand All @@ -13,6 +13,16 @@ export class CountryService {
dialCode: '994',
countryCode: 'az'
},
{
name: '',
dialCode: '54',
countryCode: 'ar'
},
{
name: '',
dialCode: '61',
countryCode: 'au'
},
{
name: '',
dialCode: '1242',
Expand Down Expand Up @@ -168,6 +178,11 @@ export class CountryService {
dialCode: '61',
countryCode: 'cx'
},
{
name: '',
dialCode: '61',
countryCode: 'cc'
},
{
name: '',
dialCode: '57',
Expand Down Expand Up @@ -1154,8 +1169,6 @@ export class CountryService {
getCountries(): Country[] {
return this.countries;
}


}