Skip to content

Commit

Permalink
Forgot to commit this...
Browse files Browse the repository at this point in the history
  • Loading branch information
hkwu committed Oct 14, 2016
1 parent feb6d43 commit 85414a4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- 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))
([#593](https://github.com/chriso/validator.js/pull/593))

#### 6.0.0

Expand Down
56 changes: 56 additions & 0 deletions lib/isISSN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isISSN;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

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

(0, _assertString2.default)(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;
}
module.exports = exports['default'];

0 comments on commit 85414a4

Please sign in to comment.