diff --git a/API_DOCS.md b/API_DOCS.md index b4e3581..01fa49c 100644 --- a/API_DOCS.md +++ b/API_DOCS.md @@ -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 @@ -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 @@ -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. diff --git a/hmpo-countries-lib-7.1.2.tgz b/hmpo-countries-lib-7.1.2.tgz new file mode 100644 index 0000000..aef2fab Binary files /dev/null and b/hmpo-countries-lib-7.1.2.tgz differ diff --git a/lib/index.js b/lib/index.js index 0ffff67..7c5b03b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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, @@ -428,8 +428,8 @@ class CountriesCachedModel { * @returns {Array} - 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); } /** diff --git a/test/lib/spec.index.js b/test/lib/spec.index.js index 2f7b7a8..cc0f263 100644 --- a/test/lib/spec.index.js +++ b/test/lib/spec.index.js @@ -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); @@ -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);