refactor: use modern JS constructs to validate NRIC #2842
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
This is a reopening of #2785 . That PR was reverted because it was using named capture groups (NCG) in regular expressions, and that is not supported in IE11.
The reopening of the PR removes the NCGs for compatibility. It makes the code slightly less readable, but it's good enough.
I was hoping to use babel and/or core-js to have auto-correction with compatibility of the NCGs in the regular expression (with @babel/plugin-transform-named-capturing-groups-regex) or core-js 3.18 (see this core-js PR that added support), but somehow I can't get either to run 😭 , and since this PR is not critical, I need to time-box and move on.
Old PR text is below.
Context (old)
While following code from the NIRC component, I ended up on the nric 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