blob: b272b9a83a051a588ef7d12f2e52408e1b2fbf9a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isISBN;
var _assertString = _interopRequireDefault(require("./util/assertString"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var possibleIsbn10 = /^(?:[0-9]{9}X|[0-9]{10})$/;
var possibleIsbn13 = /^(?:[0-9]{13})$/;
var factor = [1, 3];
function isISBN(isbn, options) {
(0, _assertString.default)(isbn); // For backwards compatibility:
// isISBN(str [, version]), i.e. `options` could be used as argument for the legacy `version`
var version = String((options === null || options === void 0 ? void 0 : options.version) || options);
if (!(options !== null && options !== void 0 && options.version || options)) {
return isISBN(isbn, {
version: 10
}) || isISBN(isbn, {
version: 13
});
}
var sanitizedIsbn = isbn.replace(/[\s-]+/g, '');
var checksum = 0;
if (version === '10') {
if (!possibleIsbn10.test(sanitizedIsbn)) {
return false;
}
for (var i = 0; i < version - 1; i++) {
checksum += (i + 1) * sanitizedIsbn.charAt(i);
}
if (sanitizedIsbn.charAt(9) === 'X') {
checksum += 10 * 10;
} else {
checksum += 10 * sanitizedIsbn.charAt(9);
}
if (checksum % 11 === 0) {
return true;
}
} else if (version === '13') {
if (!possibleIsbn13.test(sanitizedIsbn)) {
return false;
}
for (var _i = 0; _i < 12; _i++) {
checksum += factor[_i % 2] * sanitizedIsbn.charAt(_i);
}
if (sanitizedIsbn.charAt(12) - (10 - checksum % 10) % 10 === 0) {
return true;
}
}
return false;
}
module.exports = exports.default;
module.exports.default = exports.default;
|