Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
00e87e6
Make sure unsupported config property types are dropped
colinrotherham Apr 28, 2026
1906816
Add support for functions in component configs
colinrotherham Apr 28, 2026
d1ab61a
Update `initAll()` config types to use `ComponentConfig` generic
colinrotherham May 13, 2026
6080d7f
Update character count config override types to show they’re partial …
colinrotherham May 13, 2026
0e28909
Fix missing `this.config` in config override function
colinrotherham May 13, 2026
75ce0ef
Fix typo
colinrotherham May 11, 2026
06ce5bd
Default to `characters` translation key prefix
colinrotherham May 11, 2026
7fcbbfb
Deprecate character count `maxwords` and add `countType` option
colinrotherham May 11, 2026
3f9a2c0
Update GOV.UK Frontend support error to accept custom message
colinrotherham May 11, 2026
d014d6d
Add character count support for `countType: "characters"` using Intl.…
colinrotherham May 13, 2026
4848496
Expose segmenter for `countType: "words"`
colinrotherham May 11, 2026
b18ddd1
Add character count support for `countType: "words"` using Intl.Segme…
colinrotherham May 14, 2026
ad6bf20
Expose character count `countFunctions`
colinrotherham May 14, 2026
1692a88
Add character count support for `countFunction` option
colinrotherham May 14, 2026
20db8e0
Expose segmenter for `countFunction` option
colinrotherham May 21, 2026
647f7a1
Set up 2nd parameter for count function context
colinrotherham May 21, 2026
4e3da12
Determine the count function to use in constructor
colinrotherham May 21, 2026
c24ac3d
Cache the count function context in constructor
colinrotherham May 21, 2026
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
21 changes: 17 additions & 4 deletions packages/govuk-frontend/src/govuk/common/configuration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ export class ConfigurableComponent extends Component {
normaliseDataset(childConstructor, this._$root.dataset)
)

// Override defaults with JavaScript config
this._config = /** @type {ConfigurationType} */ (
mergeConfigs(childConstructor.defaults, config ?? {})
)

// Override merged config with dataset config
this._config = /** @type {ConfigurationType} */ (
mergeConfigs(
childConstructor.defaults,
config ?? {},
this._config,
this[configOverride](datasetConfig),
datasetConfig
)
Expand All @@ -107,6 +112,13 @@ export class ConfigurableComponent extends Component {
* @returns {string | boolean | number | undefined} Normalised data
*/
export function normaliseString(value, property) {
if (
property?.type &&
!['string', 'number', 'boolean'].includes(property.type)
) {
return
}

const trimmedValue = value ? value.trim() : ''

let output
Expand Down Expand Up @@ -374,7 +386,8 @@ export function extractConfigByNamespace(schema, dataset, namespace) {
/**
* @internal
* @typedef {keyof ObjectNested} NestedKey
* @typedef {{ [key: string]: string | boolean | number | ObjectNested | undefined }} ObjectNested
* @typedef {string | boolean | number} NestedValue
* @typedef {{ [key: string]: NestedValue | ((...args: any[]) => NestedValue) | ObjectNested | undefined }} ObjectNested
*/

/**
Expand All @@ -390,7 +403,7 @@ export function extractConfigByNamespace(schema, dataset, namespace) {
* Schema property for component config
*
* @typedef {object} SchemaProperty
* @property {'string' | 'boolean' | 'number' | 'object'} type - Property type
* @property {'string' | 'boolean' | 'number' | 'object' | 'function'} type - Property type
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('normaliseDataset', () => {
aStringBoolean: { type: 'string' },
aStringNumber: { type: 'string' },
anOptionalString: { type: 'string' },
anObject: { type: 'object' }
anObject: { type: 'object' },
aFunction: { type: 'function' }
}
}
},
Expand All @@ -39,7 +40,8 @@ describe('normaliseDataset', () => {
anOptionalString: '',
'anObject.one': '111',
'anObject.two': '222',
'anObject.three': '333'
'anObject.three': '333',
aFunction: '() => "albatross"'
}
)
).toEqual({
Expand All @@ -54,7 +56,8 @@ describe('normaliseDataset', () => {
one: 111,
two: 222,
three: 333
}
},
aFunction: undefined // Functions are not normalised from datasets
Comment thread
romaricpascal marked this conversation as resolved.
})
})
})
Expand All @@ -69,6 +72,7 @@ describe('normaliseDataset', () => {
* @property {string} aStringNumber - A string number
* @property {string} [anOptionalString] - An optional string
* @property {{ one: string, two: string, three: string }} anObject - An object
* @property {(name: string) => string} aFunction - A function
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,25 @@ describe('normaliseString', () => {
it('does not normalise whitespace only strings', () => {
expect(normaliseString(' ')).toBe(' ')
})

it('handles missing property schema', () => {
// @ts-expect-error Property 'type' is missing
expect(normaliseString('true', {})).toBe(true)

// @ts-expect-error Property 'type' is missing
expect(normaliseString('1337', {})).toBe(1337)
})

it('skips unhandled property schema', () => {
const inputFunction = '() => "albatross"'
const inputObject = '{ not: "allowed" }'

// Functions in strings are ignored even with schema property type
expect(normaliseString(inputFunction)).toBe(inputFunction)
expect(normaliseString(inputFunction, { type: 'function' })).toBeUndefined()

// Objects in strings are ignored even with schema property type
expect(normaliseString(inputObject)).toBe(inputObject)
expect(normaliseString(inputObject, { type: 'object' })).toBeUndefined()
})
})
Loading