isISSN.js 708 B

1234567891011121314151617181920212223
  1. import assertString from './util/assertString';
  2. var issn = '^\\d{4}-?\\d{3}[\\dX]$';
  3. export default function isISSN(str) {
  4. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5. assertString(str);
  6. var testIssn = issn;
  7. testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
  8. testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
  9. if (!testIssn.test(str)) {
  10. return false;
  11. }
  12. var digits = str.replace('-', '').toUpperCase();
  13. var checksum = 0;
  14. for (var i = 0; i < digits.length; i++) {
  15. var digit = digits[i];
  16. checksum += (digit === 'X' ? 10 : +digit) * (8 - i);
  17. }
  18. return checksum % 11 === 0;
  19. }