-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.ts
53 lines (48 loc) · 1.57 KB
/
utilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { CPACode, CPACodeCategory, CPACodeString } from './types.js'
/**
* Checks if a string is meets the CPA code format.
* @param cpaCode - A CPA code.
* @returns - True when the string validates to the CPA code format.
*/
export function _validateCPACodeStringFormat(cpaCode: string): cpaCode is CPACodeString {
return /^\d{3}$/.test(cpaCode)
}
/**
* Retrieves a category object from a given list.
* @param categoryList - A list of CPA code category objects.
* @param cpaCode - A CPA code.
* @returns - A CPA code category object.
*/
export function _getCodeCategory(
categoryList: CPACodeCategory[],
cpaCode: string
): CPACodeCategory | undefined {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined
}
return categoryList.find((possibleCategory) => {
return (
cpaCode >= possibleCategory.cpaCodeMin &&
cpaCode <= possibleCategory.cpaCodeMax
)
})
}
/**
* Retrieves a list of CPA code objects that correspond to a given abbreviation.
* @param codeList - A list of CPA code objects.
* @param cpaCodeAbbreviation - A CPA code abbreviation, English or French.
* @returns - A filtered list of CPA code objects.
*/
export function _getCodesByAbbreviation(
codeList: CPACode[],
cpaCodeAbbreviation: string
): CPACode[] {
const upperCaseCpaCodeAbbreviation = cpaCodeAbbreviation.toUpperCase()
return codeList.filter((possibleCode) => {
return (
upperCaseCpaCodeAbbreviation ===
possibleCode.cpaCodeAbbreviationEnglish ||
upperCaseCpaCodeAbbreviation === possibleCode.cpaCodeAbbreviationFrench
)
})
}