Skip to content

Commit ecb2856

Browse files
authored
fix(validation): preserve capitalisation of National Grid, OS, and Ordnance Survey (#291)
1 parent a9ad7fd commit ecb2856

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

src/server/plugins/engine/components/helpers/helpers.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { OsGridRefField } from '~/src/server/plugins/engine/components/OsGridRef
99
import { createComponent } from '~/src/server/plugins/engine/components/helpers/components.js'
1010
import {
1111
createLowerFirstExpression,
12-
lowerFirstExpressionOptions
12+
lowerFirstExpressionOptions,
13+
lowerFirstPreserveProperNouns
1314
} from '~/src/server/plugins/engine/components/helpers/index.js'
1415
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
1516
import definition from '~/test/form/definitions/basic.js'
@@ -145,6 +146,42 @@ describe('ComponentBase tests', () => {
145146
})
146147
})
147148

149+
describe('lowerFirstPreserveProperNouns', () => {
150+
test('should preserve "National Grid" capitalisation', () => {
151+
expect(lowerFirstPreserveProperNouns('National Grid field number')).toBe(
152+
'National Grid field number'
153+
)
154+
})
155+
156+
test('should preserve "Ordnance Survey" capitalisation', () => {
157+
expect(
158+
lowerFirstPreserveProperNouns('Ordnance Survey (OS) grid reference')
159+
).toBe('Ordnance Survey (OS) grid reference')
160+
})
161+
162+
test('should preserve "OS" capitalisation', () => {
163+
expect(lowerFirstPreserveProperNouns('OS grid reference')).toBe(
164+
'OS grid reference'
165+
)
166+
})
167+
168+
test('should lowercase first character for regular text', () => {
169+
expect(lowerFirstPreserveProperNouns('Enter your name')).toBe(
170+
'enter your name'
171+
)
172+
})
173+
174+
test('should handle text without special terms', () => {
175+
expect(lowerFirstPreserveProperNouns('Latitude and longitude')).toBe(
176+
'latitude and longitude'
177+
)
178+
})
179+
180+
test('should handle empty string', () => {
181+
expect(lowerFirstPreserveProperNouns('')).toBe('')
182+
})
183+
})
184+
148185
describe('lowerFirst expression helpers', () => {
149186
test('lowerFirstExpressionOptions should have lowerFirst function', () => {
150187
expect(lowerFirstExpressionOptions).toHaveProperty('functions')

src/server/plugins/engine/components/helpers/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,24 @@ export const addClassOptionIfNone = (
3939
options.classes ??= className
4040
}
4141

42+
/**
43+
* Applies lowerFirst but preserves capitalisation of proper nouns
44+
* like "National Grid", "Ordnance Survey" and "OS".
45+
*/
46+
export function lowerFirstPreserveProperNouns(text: string): string {
47+
const result = lowerFirst(text)
48+
return result
49+
.replace(/\bnational [Gg]rid\b/g, 'National Grid')
50+
.replace(/\bordnance [Ss]urvey\b/g, 'Ordnance Survey')
51+
.replace(/\b[oO][sS]\b/g, 'OS')
52+
}
53+
4254
/**
4355
* Configuration for Joi expressions that use lowerFirst function
4456
*/
4557
export const lowerFirstExpressionOptions = {
4658
functions: {
47-
lowerFirst
59+
lowerFirst: lowerFirstPreserveProperNouns
4860
}
4961
} as ReferenceOptions
5062

src/server/plugins/engine/pageControllers/validationOptions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import joi, {
88
type ReferenceOptions,
99
type ValidationOptions
1010
} from 'joi'
11-
import lowerFirst from 'lodash/lowerFirst.js'
11+
12+
import { lowerFirstPreserveProperNouns } from '~/src/server/plugins/engine/components/helpers/index.js'
1213

1314
const opts = {
1415
functions: {
15-
lowerFirst
16+
lowerFirst: lowerFirstPreserveProperNouns
1617
}
1718
} as ReferenceOptions
1819

0 commit comments

Comments
 (0)