@skhemata/skhemata-form
Version:
Skhemata Form Web Component. This web component can be used as base web component when working with forms and inputs.
21 lines (18 loc) • 639 B
JavaScript
import assertString from './util/assertString';
const issn = '^\\d{4}-?\\d{3}[\\dX]$';
export default function isISSN(str, options = {}) {
assertString(str);
let testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
const digits = str.replace('-', '').toUpperCase();
let checksum = 0;
for (let i = 0; i < digits.length; i++) {
const digit = digits[i];
checksum += (digit === 'X' ? 10 : +digit) * (8 - i);
}
return checksum % 11 === 0;
}