Skip to content

Commit

Permalink
Add isISSN().
Browse files Browse the repository at this point in the history
  • Loading branch information
hkwu committed Oct 14, 2016
1 parent 57af262 commit feb6d43
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
([#585](https://github.com/chriso/validator.js/pull/585))
- Added support for greater or less than in `isFloat()`
([#544](https://github.com/chriso/validator.js/issues/544))
- Added support for ISSN validation via `isISSN()`
([#589](https://github.com/chriso/validator.js/pull/589))

#### 6.0.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Passing anything other than a string is an error.
- **isHexadecimal(str)** - check if the string is a hexadecimal number.
- **isIP(str [, version])** - check if the string is an IP (version 4 or 6).
- **isISBN(str [, version])** - check if the string is an ISBN (version 10 or 13).
- **isISSN(str [, options])** - check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number). `options` is an object which defaults to `{ case_sensitive: false }`.
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
- **isIn(str, values)** - check if the string is in a array of allowed values.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ var _isISBN = require('./lib/isISBN');

var _isISBN2 = _interopRequireDefault(_isISBN);

var _isISSN = require('./lib/isISSN');

var _isISSN2 = _interopRequireDefault(_isISSN);

var _isMobilePhone = require('./lib/isMobilePhone');

var _isMobilePhone2 = _interopRequireDefault(_isMobilePhone);
Expand Down Expand Up @@ -269,7 +273,7 @@ var validator = {
isDate: _isDate2.default, isAfter: _isAfter2.default, isBefore: _isBefore2.default,
isIn: _isIn2.default,
isCreditCard: _isCreditCard2.default,
isISIN: _isISIN2.default, isISBN: _isISBN2.default,
isISIN: _isISIN2.default, isISBN: _isISBN2.default, isISSN: _isISSN2.default,
isMobilePhone: _isMobilePhone2.default,
isCurrency: _isCurrency2.default,
isISO8601: _isISO2.default,
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import isCreditCard from './lib/isCreditCard';

import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';

import isMobilePhone from './lib/isMobilePhone';

Expand Down Expand Up @@ -103,7 +104,7 @@ const validator = {
isDate, isAfter, isBefore,
isIn,
isCreditCard,
isISIN, isISBN,
isISIN, isISBN, isISSN,
isMobilePhone,
isCurrency,
isISO8601,
Expand Down
20 changes: 20 additions & 0 deletions src/lib/isISSN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assertString from './util/assertString';

const issn = /^\d{4}-\d{3}[\dX]$/;

export default function isISSN(str, options = {}) {
assertString(str);
const testIssn = options.case_sensitive ? issn : new RegExp(issn, 'i');
if (!testIssn.test(str)) {
return false;
}
const issnDigits = str.replace('-', '');
let position = 8;
let checksum = 0;
for (const digit of issnDigits) {
const digitValue = digit.toUpperCase() === 'X' ? 10 : +digit;
checksum += digitValue * position;
--position;
}
return checksum % 11 === 0;
}
29 changes: 29 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,35 @@ describe('Validators', function () {
});
});

it('should validate ISSNs', function () {
test({
validator: 'isISSN',
valid: [
'0378-5955',
'0000-0000',
'2434-561X',
'2434-561x',
],
invalid: [
'0378-5954',
'0000-0001',
'0378-123',
'037-1234',
'0',
],
});
test({
validator: 'isISSN',
args: [{ case_sensitive: true }],
valid: [
'2434-561X',
],
invalid: [
'2434-561x',
],
});
});

it('should validate JSON', function () {
test({
validator: 'isJSON',
Expand Down
45 changes: 44 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,49 @@
return false;
}

var issn = /^\d{4}-\d{3}[\dX]$/;

function isISSN(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

assertString(str);
var testIssn = options.case_sensitive ? issn : new RegExp(issn, 'i');
if (!testIssn.test(str)) {
return false;
}
var issnDigits = str.replace('-', '');
var position = 8;
var checksum = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = issnDigits[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var digit = _step.value;

var digitValue = digit.toUpperCase() === 'X' ? 10 : +digit;
checksum += digitValue * position;
--position;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

return checksum % 11 === 0;
}

/* eslint-disable max-len */
var phones = {
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
Expand Down Expand Up @@ -1367,7 +1410,7 @@
isDate: isDate, isAfter: isAfter, isBefore: isBefore,
isIn: isIn,
isCreditCard: isCreditCard,
isISIN: isISIN, isISBN: isISBN,
isISIN: isISIN, isISBN: isISBN, isISSN: isISSN,
isMobilePhone: isMobilePhone,
isCurrency: isCurrency,
isISO8601: isISO8601,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit feb6d43

Please sign in to comment.