generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* CDPS-92: adjudications update * Add pluralise function Add hyperlink Change awards to punishments Update tests
- Loading branch information
1 parent
eca85e4
commit c37046d
Showing
8 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { pluralise } from './pluralise' | ||
|
||
describe('pluralise a word', () => { | ||
it.each([ | ||
['Standard word, no options - singular count', 1, 'cat', undefined, '1 cat'], | ||
['Standard word, no options - plural count', 2, 'cat', undefined, '2 cats'], | ||
['Standard word, no options - zero count', 0, 'cat', undefined, '0 cats'], | ||
['Standard word, no options - negative singular count', -1, 'cat', undefined, '-1 cat'], | ||
['Standard word, no options - negative plural count', -7, 'cat', undefined, '-7 cats'], | ||
['Standard word, forced plural - plural count', 3, 'cat', { plural: 'kittens' }, '3 kittens'], | ||
['Standard word, no includeCount - plural count', 4, 'cat', { includeCount: false }, 'cats'], | ||
['Standard word, empty message - zero count', 0, 'cat', { emptyMessage: 'No cats' }, 'No cats'], | ||
['Standard word, empty message - non-zero count', 2, 'cat', { emptyMessage: 'No cats' }, '2 cats'], | ||
['Non-standard word, no options - plural count', 2, 'person', undefined, '2 people'], | ||
['Non-standard word, forced plural - plural count', 4, 'person', { plural: 'humans' }, '4 humans'], | ||
['Non-standard word, no includeCount - plural count', 4, 'person', { includeCount: false }, 'people'], | ||
[ | ||
'Non-standard word, forced plural, no includeCount - plural count', | ||
4, | ||
'person', | ||
{ plural: 'humans', includeCount: false }, | ||
'humans', | ||
], | ||
['Non-standard word, empty message - plural count', 0, 'person', { emptyMessage: 'Nobody here' }, 'Nobody here'], | ||
['Standard word, multiple - singular count', 1, 'active punishment', undefined, '1 active punishment'], | ||
['Standard word, multiple - plural count', 2, 'active punishment', undefined, '2 active punishments'], | ||
])( | ||
'%s pluralise(%s, %s, %s)', | ||
(_: string, count: number, word: string, options: { [key: string]: string | boolean }, expected: string) => { | ||
expect(pluralise(count, word, options)).toEqual(expected) | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const notStandardPlurals = { | ||
person: 'people', | ||
man: 'men', | ||
woman: 'women', | ||
child: 'children', | ||
knife: 'knives', | ||
life: 'lives', | ||
wife: 'wives', | ||
foot: 'feet', | ||
tooth: 'teeth', | ||
} | ||
|
||
/** | ||
* Pluralises a given word based on a count provided. | ||
* | ||
* Uses `nonStandardPlurals` if the word is defined, else just adds an 's' | ||
* | ||
* Both can be overridden by using the `plural` option | ||
* | ||
* Added as Nunjucks filter for use in templates, e.g. | ||
* - {{ prisonersArray.length | pluralise('person') }} // 'people' | ||
* - {{ countOfChildren | pluralise('child') }} // 'children' | ||
* - {{ countOfChildren | pluralise('child', 'kids') }} // 'kids' | ||
* | ||
* Limitations: entries in `nonStandardPlurals` are case-sensitive, so 'Person' will return 'Persons' | ||
* | ||
* @param count - denotes if `word` should be pluralised or not | ||
* @param word - the singular form of the word to be pluralised | ||
* @param options - plural, includeCount, emptyMessage | ||
* @param options.plural - return this specific plural if count !== [1,-1] | ||
* @param options.includeCount - prefix the return string with `count` (e.g. '3 people') - defaults to true | ||
* @param options.emptyMessage - return this string if `count` is Falsy | ||
* @return pluralised form of word if count !== [1,-1] else just word | ||
*/ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const pluralise = ( | ||
count: number, | ||
word: string, | ||
options?: { plural?: string; includeCount?: boolean; emptyMessage?: string }, | ||
): string => { | ||
const includeCount = options?.includeCount ?? true | ||
if (!count && options?.emptyMessage) { | ||
return options.emptyMessage | ||
} | ||
const pluralised = [1, -1].includes(count) ? word : options?.plural || notStandardPlurals[word] || `${word}s` | ||
return includeCount ? `${count} ${pluralised}` : pluralised | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters