diff --git a/README.md b/README.md index 37fbd5e3d..25a523781 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ Validator | Description **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that is divisible by another. **isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number]. +**isUPC(str)** | check if the string is a [UPC-A][UPC-A] code with a valid checksum. **isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. **isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace: false }`. **isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums. @@ -246,6 +247,7 @@ This project is licensed under the [MIT](LICENSE). See the [LICENSE](LICENSE) fi [Base64 URL Safe]: https://base64.guru/standards/base64url [Data URI Format]: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs [European Article Number]: https://en.wikipedia.org/wiki/International_Article_Number +[UPC-A]: https://en.wikipedia.org/wiki/Universal_Product_Code [Ethereum]: https://ethereum.org/ [CSS Colors Level 4 Specification]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value [IMEI]: https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity diff --git a/src/index.js b/src/index.js index b69c43649..f0351bd5b 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,7 @@ import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; import isEAN from './lib/isEAN'; +import isUPC from './lib/isUPC'; import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; @@ -198,6 +199,7 @@ const validator = { isCreditCard, isIdentityCard, isEAN, + isUPC, isISIN, isISBN, isISSN, diff --git a/src/lib/isUPC.js b/src/lib/isUPC.js new file mode 100644 index 000000000..24d51fbc0 --- /dev/null +++ b/src/lib/isUPC.js @@ -0,0 +1,36 @@ +import assertString from './util/assertString'; + +const UPCA_REGEX = /^\d{12}$/; + +function calculateCheckDigit(upc) { + const digits = upc + .slice(0, -1) + .split('') + .map(Number); + + const sumOddPositions = digits + .filter((_, index) => index % 2 === 0) + .reduce((acc, digit) => acc + digit, 0); + + const sumEvenPositions = digits + .filter((_, index) => index % 2 === 1) + .reduce((acc, digit) => acc + digit, 0); + + const total = (sumOddPositions * 3) + sumEvenPositions; + const remainder = total % 10; + + return remainder === 0 ? 0 : 10 - remainder; +} + +export default function isUPC(str) { + assertString(str); + + if (!UPCA_REGEX.test(str)) { + return false; + } + + const actualCheckDigit = Number(str.slice(-1)); + + return actualCheckDigit === calculateCheckDigit(str); +} + diff --git a/test/validators.test.js b/test/validators.test.js index c5ea4dc99..449e1bb84 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -7072,6 +7072,27 @@ describe('Validators', () => { }); }); + it('should validate UPCs', () => { + test({ + validator: 'isUPC', + valid: [ + '036000291452', + '012345678905', + '042100005264', + '725272730706', + '013800150738', + '015000000394', + ], + invalid: [ + '036000291453', + '725272730700', + '01234567890', + '0123456789057', + '12345678A905', + ], + }); + }); + it('should validate ISSNs', () => { test({ validator: 'isISSN',