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

Added isFreightContainerID validator #2144

Merged
merged 7 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Validator | Description
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification.
**isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.
**isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date. <br/>`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid.
**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import isCurrency from './lib/isCurrency';

import isBtcAddress from './lib/isBtcAddress';

import isISO6346 from './lib/isISO6346';
import isISO6391 from './lib/isISO6391';
import isISO8601 from './lib/isISO8601';
import isRFC3339 from './lib/isRFC3339';
Expand Down Expand Up @@ -198,6 +199,7 @@ const validator = {
isEthereumAddress,
isCurrency,
isBtcAddress,
isISO6346,
isISO6391,
isISO8601,
isRFC3339,
Expand Down
35 changes: 35 additions & 0 deletions src/lib/isISO6346.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assertString from './util/assertString';

// https://en.wikipedia.org/wiki/ISO_6346
// according to ISO6346 standard, checksum digit is mandatory for freight container but recommended
// for other container types (J and Z)
const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/;
const isDigit = /^[0-9]$/;

export default function isISO6346(str) {
assertString(str);
str = str.trim();
str = str.toUpperCase();

if (!isISO6346Str.test(str)) return false;

if (str.length === 11) {
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;
}

return true;
}
26 changes: 26 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -11837,6 +11837,32 @@ describe('Validators', () => {
});
});

it('should validate ISO6346 shipping containerID', () => {
test({
validator: 'isISO6346',
valid: [
'HLXU2008419',
'TGHU7599330',
'ECMU4657496',
'MEDU6246078',
'YMLU2809976',
'MRKU0046221',
'EMCU3811879',
'OOLU8643084',
'HJCU1922713',
'QJRZ123456',
],
invalid: [
'OOLU1922713',
'HJCU1922413',
'FCUI985619',
'ECMJ4657496',
'TBJA7176445',
'AFFU5962593',
],
});
});

// EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas.
it('should validate taxID', () => {
test({
Expand Down