-
Notifications
You must be signed in to change notification settings - Fork 87
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
refactor: use modern JS constructs to validate NRIC #2785
Conversation
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.
a few typo/casing suggestions first, will review the logic soon!
Co-authored-by: Antariksh Mahajan <[email protected]>
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.
lgtm!
On my 2 questions from the PR description:
Support matrix for named character groups
String array-like character accessed was introduced in EcmaScript 5 (see doc here). which has this support matrix: I guess the biggest question I have now is whether we support IE11? I just checked the transpiled (tsc+babel) result to check if by chance the code would be transformed to use older constructs like function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.isNricValid=void 0;var r=/^(?<prefix>[STFG])(?<digits>\d{7})(?<checksum>[A-Z])$/;t.isNricValid=function(e){var t=null==e?void 0:e.toUpperCase().match(r);if(!t)return!1;var n=t.groups,i=n.prefix,o=n.digits;return n.checksum===("S"===i||"T"===i?"JZIHGFEDCBA":"XWUTRQPNMLK")[[2,7,6,5,4,3,2].reduce((function(e,t,n){return e+t*parseInt(o[n])}),"S"===i||"F"===i?0:4)%11]}} I will defer merging this for now, and maybe schedule a short discussion for our next sync. |
Confirmed with Sonjia we do not support IE11, so I'm just going to merge this now. |
…)" This reverts commit aaa8072.
Context
While following code from the NIRC component, I ended up on the nirc validation where I thought several minor improvements could be made. Below are some (very minor) issues I thought about when seeing the code
{1}
reduce()
function does an unnecessary assignment to a local variable, which looks confusing because of the shadow variable namesum
.The code was working perfectly fine, so the refactor is unnecessary. I initially only wanted to remove the
{1}
and add ai
flag to the regex, but I ended up refactoring the whole file 😑I hesitated to open the PR since it is unplanned and unnecessary, and will consume precious reviewer time, but in the end, since I did the changes, I thought I'd open it up anyway for some discussions, like:
Approach
I've made changes that addresses all the points above. The code reduces significantly. I am obviously really biased, but I think it is more readable now.
Testing