Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add checkdigit validation for aba routing numbers #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/__tests__/routing-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("routingNumber", () => {
expect(routingNumber(value)).toEqual({
isValid: false,
isPotentiallyValid: false,
isCheckDigitValid: false,
});
});
});
Expand All @@ -37,6 +38,7 @@ describe("routingNumber", () => {
expect(routingNumber(value)).toEqual({
isValid: false,
isPotentiallyValid: true,
isCheckDigitValid: false,
});
});
});
Expand All @@ -46,6 +48,7 @@ describe("routingNumber", () => {
expect(routingNumber(value)).toEqual({
isValid: true,
isPotentiallyValid: true,
isCheckDigitValid: true,
});
});
});
Expand All @@ -55,6 +58,7 @@ describe("routingNumber", () => {
expect(routingNumber(value)).toEqual({
isValid: false,
isPotentiallyValid: false,
isCheckDigitValid: false,
});
});
});
Expand All @@ -70,6 +74,7 @@ describe("routingNumber", () => {
expect(routingNumber(value)).toEqual({
isValid: false,
isPotentiallyValid: false,
isCheckDigitValid: false,
});
});
});
Expand Down
34 changes: 32 additions & 2 deletions src/routing-number.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
import routingNumberList from "./routing-number-list";
import type { BankValidity } from "./types";
import type { RoutingNumberValidity } from "./types";

const allowedRoutingNumbers = routingNumberList.reduce((result, number) => {
result[number] = true;

return result;
}, Object.create(null));

export default function (value: string | unknown): BankValidity {
export default function (value: string | unknown): RoutingNumberValidity {
if (typeof value !== "string") {
return {
isValid: false,
isPotentiallyValid: false,
isCheckDigitValid: false,
};
}

const isValid = value in allowedRoutingNumbers;
const isPotentiallyValid = /^\d{0,8}$/.test(value);
const isCheckDigitValid = getIsCheckDigitValid(value);

return {
isValid,
isPotentiallyValid: isValid || isPotentiallyValid,
isCheckDigitValid,
};
}

/**
* reference: https://en.wikipedia.org/wiki/ABA_routing_transit_number#Check_digit
* @param abaRoutingNumber an ABA routing number to be checked
* @returns whether check digit of the ABA routing number is valid
*/
const getIsCheckDigitValid = (abaRoutingNumber: string) => {
try {
if (!/^\d+$/.test(abaRoutingNumber)) return false;
if (abaRoutingNumber.length !== 9) return false;

const d = abaRoutingNumber.split("").map((digit) => parseInt(digit, 10));

const res =
// prettier-ignore
(
(3 * (d[0] + d[3] + d[6])) +
(7 * (d[1] + d[4] + d[7])) +
(1 * (d[2] + d[5] + d[8]))
)
% 10 === 0;

return res;
} catch (e) {
return false;
}
};
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export type BankValidity = {
isValid: boolean;
isPotentiallyValid: boolean;
};

export type RoutingNumberValidity = BankValidity & {
isCheckDigitValid: boolean;
};