Skip to content
Merged
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
9 changes: 6 additions & 3 deletions API_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,13 @@ Returns **([string][70] | [undefined][75])** The slug version of the country's n
## sortCountryList

Sorts a list of country objects by their `displayName` property.
Moves the country with `countryCode` 'GB' (United Kingdom) to the top of the list if present.
Conditional to move the country with `countryCode` 'GB' (United Kingdom) to the top of the list if present.
The GB move to top is optional as there are some instances where this is not required.

### Parameters

* `list` **[Array][74]<[Object][69]>** The array of country objects to sort.
Each object should have at least `displayName` and `countryCode` properties.
* `list` **[boolean][72]** Whether the GB country entry should move to top. Each object should have at least `displayName` and `countryCode` properties.
* `moveGbToTop` (optional, default `true`)

### Examples

Expand Down Expand Up @@ -389,6 +390,7 @@ Optionally uses Welsh display names based on the `isWelsh` flag.
* `list` **[Array][74]<[Object][69]>** The array of country objects to transform.
Each object should have `countryCode`, `displayName`, and `displayNameWelsh` properties.
* `isWelsh` **[boolean][72]** Whether to use the Welsh display name (`displayNameWelsh`) instead of the default (`displayName`).
* `moveGbToTop` (optional, default `true`)

### Examples

Expand Down Expand Up @@ -419,6 +421,7 @@ A wrapper function around `dropdownList(list, isWelsh)`.
### Parameters

* `isWelsh` **[boolean][72]** Whether to use the Welsh display name (`displayNameWelsh`) instead of the default (`displayName`).
* `moveGbToTop` (optional, default `true`)

Returns **[Array][74]<[Object][69]>** An array of objects formatted for use in dropdowns,
each containing `value`, `text`, and `label` properties.
Expand Down
Binary file added hmpo-countries-lib-7.1.2.tgz
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ class CountriesCachedModel {
* // { value: 'FR', text: 'France', label: 'France' }
* // ]
*/
dropdownList(list, isWelsh) {
list = this.sortCountryList(list);
dropdownList(list, isWelsh, moveGbToTop = true) {
list = this.sortCountryList(list, moveGbToTop);
return list.map(i => ({
value: i.countryCode,
text: isWelsh ? i.displayNameWelsh : i.displayName,
Expand All @@ -428,8 +428,8 @@ class CountriesCachedModel {
* @returns {Array<Object>} - An array of objects formatted for use in dropdowns,
* each containing `value`, `text`, and `label` properties.
*/
dropdownListBirthCountries(isWelsh) {
return this.dropdownList(this.getBirthCountries(), isWelsh);
dropdownListBirthCountries(isWelsh, moveGbToTop = true) {
return this.dropdownList(this.getBirthCountries(moveGbToTop), isWelsh, moveGbToTop);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/lib/spec.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ describe('CountriesCachedModel', () => {
});

describe('dropdownList', () => {
it('turns a list of countries into sorted drop down select box options', () => {
const items = instance.dropdownList(countries, false, false);

items.should.deep.equal([
{ value: 'BA', text: 'Bar', label: 'Bar' },
{ value: 'AA', text: 'Foo', label: 'Foo' },
{ value: 'NA', text: 'Narnia', label: 'Narnia' },
{ value: 'GB', text: 'United Kingdom', label: 'United Kingdom' },
]);
});

it('turns a list of countries into sorted drop down select box options with GB at the top', () => {
const items = instance.dropdownList(countries, false);

Expand Down Expand Up @@ -594,6 +605,12 @@ describe('CountriesCachedModel', () => {
items.should.deep.equal([unitedKingdom, bar, foo, narnia]);
});

it('turns a list of countries into sorted list with GB at the top by default when flag not provided', () => {
const items = instance.sortCountryList(countries);

items.should.deep.equal([unitedKingdom, bar, foo, narnia]);
});

it('turns a list of countries into sorted list', () => {
const items = instance.sortCountryList(countries, false);

Expand Down