Skip to content

Commit

Permalink
Use ES2020 ?? nullish coalescing
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Oct 24, 2023
1 parent e9cdaee commit f7f9901
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function initAll(config) {

// Allow the user to initialise GOV.UK Frontend in only certain sections of the page
// Defaults to the entire document if nothing is set.
const $scope = config.scope || document
const $scope = config.scope ?? document

components.forEach(([Component, config]) => {
const $elements = $scope.querySelectorAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class CharacterCount extends GOVUKFrontendComponent {
})

// Determine the limit attribute (characters or words)
this.maxLength = this.config.maxwords || this.config.maxlength || Infinity
this.maxLength = this.config.maxwords ?? this.config.maxlength ?? Infinity

this.$module = $module
this.$textarea = $textarea
Expand Down Expand Up @@ -344,7 +344,7 @@ export class CharacterCount extends GOVUKFrontendComponent {
*/
count(text) {
if (this.config.maxwords) {
const tokens = text.match(/\S+/g) || [] // Matches consecutive non-whitespace chars
const tokens = text.match(/\S+/g) ?? [] // Matches consecutive non-whitespace chars
return tokens.length
} else {
return text.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class ErrorSummary extends GOVUKFrontendComponent {
}

return (
document.querySelector(`label[for='${$input.getAttribute('id')}']`) ||
document.querySelector(`label[for='${$input.getAttribute('id')}']`) ??
$input.closest('label')
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/components/tabs/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Tabs extends GOVUKFrontendComponent {
})

// Show either the active tab according to the URL's hash or the first tab
const $activeTab = this.getTab(window.location.hash) || this.$tabs[0]
const $activeTab = this.getTab(window.location.hash) ?? this.$tabs[0]

this.showTab($activeTab)

Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/errors/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class ElementError extends GOVUKFrontendError {

// Append reason
message += element
? ` is not of type ${expectedType || 'HTMLElement'}`
? ` is not of type ${expectedType ?? 'HTMLElement'}`
: ' not found'
}

Expand Down
2 changes: 1 addition & 1 deletion packages/govuk-frontend/src/govuk/i18n.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class I18n {
this.translations = translations

// The locale to use for PluralRules and NumberFormat
this.locale = config.locale || document.documentElement.lang || 'en'
this.locale = config.locale ?? (document.documentElement.lang || 'en')
}

/**
Expand Down

0 comments on commit f7f9901

Please sign in to comment.