-
Notifications
You must be signed in to change notification settings - Fork 1
fix(utils): correct bank number validation #917
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,29 @@ export function isValidBankname( | |
export function isValidBankNumber( | ||
bankNumber: string | ||
): FieldError.InvalidBankAccountNumber | true { | ||
if (isNaN(Number(bankNumber))) return FieldError.InvalidBankAccountNumber; | ||
if (isNaN(Number(bankNumber))) { | ||
return FieldError.InvalidBankAccountNumber; | ||
} | ||
|
||
const isLuhnValid = | ||
bankNumber | ||
.split("") | ||
.reverse() | ||
.map((char, index) => { | ||
const digit = Number(char); | ||
return index % 2 === 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer writing declarative code; use |
||
? digit * 2 > 9 | ||
? digit * 2 - 9 | ||
: digit * 2 | ||
: digit; | ||
Comment on lines
+102
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unreadable at all. It's logic is correct 100%, but it's hard to read. Try using if statements instead.
Remember, you will never learn by allotting everything to the AI.
|
||
}) | ||
.reduce((sum, digit) => sum + digit, 0) % | ||
10 === | ||
0; | ||
Comment on lines
+107
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks so ugly. |
||
|
||
if (!isLuhnValid) { | ||
return FieldError.InvalidBankAccountNumber; | ||
} | ||
|
||
return true; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This's not how we write unit tests. Use AI as a tool only. You are the coder; read other unit tests files and follow its pattern or so to speak our implicit convention of writing tests.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the note🫡 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isValidBankNumber } from "@/validation/invoice"; | ||
import { | ||
generateValidLuhnNumber, | ||
generateInvalidLuhnNumber, | ||
} from "./luhnGenerator"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relative imports are not recommended. |
||
import { FieldError } from "@litespace/types"; | ||
|
||
describe("isValidBankNumber - Generated numbers", () => { | ||
const validNumbers = Array.from({ length: 100 }, () => | ||
generateValidLuhnNumber() | ||
); | ||
|
||
const invalidNumbers = Array.from({ length: 100 }, () => | ||
generateInvalidLuhnNumber() | ||
); | ||
|
||
test.each(validNumbers)("✅ Valid: %s", (number) => { | ||
expect(isValidBankNumber(number)).toBe(true); | ||
}); | ||
|
||
test.each(invalidNumbers)("❌ Invalid: %s", (number) => { | ||
expect(isValidBankNumber(number)).toBe(FieldError.InvalidBankAccountNumber); | ||
}); | ||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First of all, only test files, like We may use the ones being used by |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Generate Valid Luhn Number Function | ||
export function generateValidLuhnNumber(length = 16): string { | ||
const digits = Array.from({ length: length - 1 }, () => | ||
Math.floor(Math.random() * 10) | ||
); | ||
|
||
const sum = digits | ||
.slice() | ||
.reverse() | ||
.map((digit, idx) => | ||
idx % 2 === 0 ? (digit * 2 > 9 ? digit * 2 - 9 : digit * 2) : digit | ||
) | ||
.reduce((acc, val) => acc + val, 0); | ||
|
||
const checkDigit = (10 - (sum % 10)) % 10; | ||
digits.push(checkDigit); | ||
|
||
return digits.join(""); | ||
} | ||
|
||
// Generate Invalid Luhn Number Function | ||
export function generateInvalidLuhnNumber(length = 16): string { | ||
const valid = generateValidLuhnNumber(length); | ||
const index = Math.floor(Math.random() * (length - 1)); | ||
const wrongDigit = (Number(valid[index]) + 1) % 10; | ||
|
||
return valid.slice(0, index) + wrongDigit + valid.slice(index + 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length of bank numbers varies between 9 digits to 12. We may use that as well before commencing on the luhn algorithm.