Skip to content

Commit

Permalink
fix typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Jul 31, 2024
1 parent 9482227 commit 0e91166
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
5 changes: 4 additions & 1 deletion cpaCodes/returns.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _getCodeCategory, _getCodesByAbbreviation } from '../utilities.js';
import { _getCodeCategory, _getCodesByAbbreviation, _validateCPACodeStringFormat } from '../utilities.js';
export const cpaReturnCodeCategories = [
{
cpaCodeMin: '900',
Expand Down Expand Up @@ -157,6 +157,9 @@ export function isCPAReturnCode(cpaCode) {
* @returns - The CPA Code object, when available.
*/
export function getCPAReturnCode(cpaCode) {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined;
}
// eslint-disable-next-line security/detect-object-injection
return cpaReturnCodes[cpaCode];
}
Expand Down
14 changes: 11 additions & 3 deletions cpaCodes/returns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { CPACode, CPACodeCategory, CPACodeString } from '../types.js'
import { _getCodeCategory, _getCodesByAbbreviation } from '../utilities.js'
import {
_getCodeCategory,
_getCodesByAbbreviation,
_validateCPACodeStringFormat
} from '../utilities.js'

export const cpaReturnCodeCategories: CPACodeCategory[] = [
{
Expand Down Expand Up @@ -141,13 +145,13 @@ export const cpaReturnCodesDefault: Record<CPACodeString, CPACode> = {
}
}

export const cpaReturnCodes: Record<CPACodeString, CPACode> = Object.assign(
export const cpaReturnCodes = Object.assign(
{},
cpaReturnCodesDishonoured,
cpaReturnCodesCustomerInitiated,
cpaReturnCodesCreditReturn,
cpaReturnCodesDefault
)
) as Record<CPACodeString, CPACode>

/**
* Retrieves the CPA return code category object.
Expand Down Expand Up @@ -175,6 +179,10 @@ export function isCPAReturnCode(cpaCode: string): boolean {
* @returns - The CPA Code object, when available.
*/
export function getCPAReturnCode(cpaCode: string): CPACode | undefined {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined
}

// eslint-disable-next-line security/detect-object-injection
return cpaReturnCodes[cpaCode]
}
Expand Down
5 changes: 4 additions & 1 deletion cpaCodes/transactions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _getCodeCategory, _getCodesByAbbreviation } from '../utilities.js';
import { _getCodeCategory, _getCodesByAbbreviation, _validateCPACodeStringFormat } from '../utilities.js';
import { cpaTransactionCodeCategoryCommerical, cpaTransactionCodesCommercial } from './transactions/commercial.js';
import { cpaTransactionCodeCategoryFederal, cpaTransactionCodesFederal } from './transactions/federal.js';
import { cpaTransactionCodeCategoryPreauthorized, cpaTransactionCodeCategoryPreauthorizedContinued, cpaTransactionCodesPreauthorized } from './transactions/preauthorized.js';
Expand Down Expand Up @@ -58,6 +58,9 @@ export function isCPATransactionCode(cpaCode) {
* @returns - The CPA code object, when available.
*/
export function getCPATransactionCode(cpaCode) {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined;
}
// eslint-disable-next-line security/detect-object-injection
return cpaTransactionCodes[cpaCode];
}
Expand Down
25 changes: 16 additions & 9 deletions cpaCodes/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { CPACode, CPACodeCategory, CPACodeString } from '../types.js'
import { _getCodeCategory, _getCodesByAbbreviation } from '../utilities.js'
import {
_getCodeCategory,
_getCodesByAbbreviation,
_validateCPACodeStringFormat
} from '../utilities.js'

import {
cpaTransactionCodeCategoryCommerical,
Expand Down Expand Up @@ -52,14 +56,13 @@ export const cpaTransactionCodeCategories: CPACodeCategory[] = [
}
]

export const cpaTransactionCodes: Record<CPACodeString, CPACode> =
Object.assign(
{},
cpaTransactionCodesPreauthorized,
cpaTransactionCodesFederal,
cpaTransactionCodesProvincialLocal,
cpaTransactionCodesCommercial
)
export const cpaTransactionCodes = Object.assign(
{},
cpaTransactionCodesPreauthorized,
cpaTransactionCodesFederal,
cpaTransactionCodesProvincialLocal,
cpaTransactionCodesCommercial
) as Record<CPACodeString, CPACode>

/**
* Retrieves the CPA transaction code category object.
Expand Down Expand Up @@ -87,6 +90,10 @@ export function isCPATransactionCode(cpaCode: string): boolean {
* @returns - The CPA code object, when available.
*/
export function getCPATransactionCode(cpaCode: string): CPACode | undefined {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined
}

// eslint-disable-next-line security/detect-object-injection
return cpaTransactionCodes[cpaCode]
}
Expand Down
8 changes: 7 additions & 1 deletion utilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { CPACode, CPACodeCategory } from './types.js';
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 declare function _validateCPACodeStringFormat(cpaCode: string): cpaCode is CPACodeString;
/**
* Retrieves a category object from a given list.
* @param categoryList - A list of CPA code category objects.
Expand Down
9 changes: 7 additions & 2 deletions utilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
function _validateCodeFormat(cpaCode) {
/**
* 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) {
return /^\d{3}$/.test(cpaCode);
}
/**
Expand All @@ -8,7 +13,7 @@ function _validateCodeFormat(cpaCode) {
* @returns - A CPA code category object.
*/
export function _getCodeCategory(categoryList, cpaCode) {
if (!_validateCodeFormat(cpaCode)) {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined;
}
return categoryList.find((possibleCategory) => {
Expand Down
11 changes: 8 additions & 3 deletions utilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { CPACode, CPACodeCategory } from './types.js'
import type { CPACode, CPACodeCategory, CPACodeString } from './types.js'

function _validateCodeFormat(cpaCode: string): boolean {
/**
* 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)
}

Expand All @@ -14,7 +19,7 @@ export function _getCodeCategory(
categoryList: CPACodeCategory[],
cpaCode: string
): CPACodeCategory | undefined {
if (!_validateCodeFormat(cpaCode)) {
if (!_validateCPACodeStringFormat(cpaCode)) {
return undefined
}

Expand Down

0 comments on commit 0e91166

Please sign in to comment.