-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Added isFreightContainerID validator #2144
Changes from 2 commits
5e711b6
638c89b
a81dfe1
a48637c
b76305e
ffe6c7c
147f15a
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import assertString from './util/assertString'; | ||
|
||
// https://en.wikipedia.org/wiki/ISO_6346 | ||
const isContainerIdReg = new RegExp('^[A-Z]{3}U[0-9]{7}$'); | ||
const isDigit = new RegExp('^[0-9]{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. I feel like this could be rewritten to Also maybe just a matter of taste in general, but this project seems to be generally prefering the use of regular expression literals. as opposed to a constructor function, like you used here.
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. Hi Panagiotis, Thank you very much for pointing it out! I have updated the corresponding lines. |
||
|
||
export default function isContainerID(str) { | ||
assertString(str); | ||
str = str.trim(); | ||
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. I have the same concerns about this, as I have mentioned in your other PR: what do you think? 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. |
||
|
||
if (!isContainerIdReg.test(str)) return false; | ||
|
||
let sum = 0; | ||
for (let i = 0; i < str.length - 1; i++) { | ||
if (!isDigit.test(str[i])) { | ||
let convertedCode; | ||
const letterCode = str.charCodeAt(i) - 55; | ||
if (letterCode < 11) convertedCode = letterCode; | ||
else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); | ||
else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); | ||
else convertedCode = 34 + (letterCode % 31); | ||
sum += convertedCode * (2 ** i); | ||
} else sum += str[i] * (2 ** i); | ||
} | ||
|
||
const checkSumDigit = sum % 11; | ||
return Number(str[str.length - 1]) === checkSumDigit; | ||
} |
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.
I think the name might be a bit too unspecific, as "container ID" feels a bit too generic, e.g. just google it, brought up several hits, like e.g. "docker container ID", "Windows Drivers Container ID", etc.
So I would probably say it makes sense to rename this?
Options could be maybe:
What do you think?
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.
We already have some ISO standards so a simple
isISO6346
could also sufficeThere 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.
I have changed it to
isISO6346
, for consistency with other validators.