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
39 changes: 38 additions & 1 deletion src/server/plugins/engine/components/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { OsGridRefField } from '~/src/server/plugins/engine/components/OsGridRef
import { createComponent } from '~/src/server/plugins/engine/components/helpers/components.js'
import {
createLowerFirstExpression,
lowerFirstExpressionOptions
lowerFirstExpressionOptions,
lowerFirstPreserveProperNouns
} from '~/src/server/plugins/engine/components/helpers/index.js'
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
import definition from '~/test/form/definitions/basic.js'
Expand Down Expand Up @@ -145,6 +146,42 @@ describe('ComponentBase tests', () => {
})
})

describe('lowerFirstPreserveProperNouns', () => {
test('should preserve "National Grid" capitalisation', () => {
expect(lowerFirstPreserveProperNouns('National Grid field number')).toBe(
'National Grid field number'
)
})

test('should preserve "Ordnance Survey" capitalisation', () => {
expect(
lowerFirstPreserveProperNouns('Ordnance Survey (OS) grid reference')
).toBe('Ordnance Survey (OS) grid reference')
})

test('should preserve "OS" capitalisation', () => {
expect(lowerFirstPreserveProperNouns('OS grid reference')).toBe(
'OS grid reference'
)
})

test('should lowercase first character for regular text', () => {
expect(lowerFirstPreserveProperNouns('Enter your name')).toBe(
'enter your name'
)
})

test('should handle text without special terms', () => {
expect(lowerFirstPreserveProperNouns('Latitude and longitude')).toBe(
'latitude and longitude'
)
})

test('should handle empty string', () => {
expect(lowerFirstPreserveProperNouns('')).toBe('')
})
})

describe('lowerFirst expression helpers', () => {
test('lowerFirstExpressionOptions should have lowerFirst function', () => {
expect(lowerFirstExpressionOptions).toHaveProperty('functions')
Expand Down
14 changes: 13 additions & 1 deletion src/server/plugins/engine/components/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ export const addClassOptionIfNone = (
options.classes ??= className
}

/**
* Applies lowerFirst but preserves capitalisation of proper nouns
* like "National Grid", "Ordnance Survey" and "OS".
*/
export function lowerFirstPreserveProperNouns(text: string): string {
const result = lowerFirst(text)
return result
.replace(/\bnational [Gg]rid\b/g, 'National Grid')
.replace(/\bordnance [Ss]urvey\b/g, 'Ordnance Survey')
.replace(/\b[oO][sS]\b/g, 'OS')
}

/**
* Configuration for Joi expressions that use lowerFirst function
*/
export const lowerFirstExpressionOptions = {
functions: {
lowerFirst
lowerFirst: lowerFirstPreserveProperNouns
}
} as ReferenceOptions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import joi, {
type ReferenceOptions,
type ValidationOptions
} from 'joi'
import lowerFirst from 'lodash/lowerFirst.js'

import { lowerFirstPreserveProperNouns } from '~/src/server/plugins/engine/components/helpers/index.js'

const opts = {
functions: {
lowerFirst
lowerFirst: lowerFirstPreserveProperNouns
}
} as ReferenceOptions

Expand Down
Loading