isbn3 1.2.17 → 1.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/audit.js +24 -4
  2. package/package.json +1 -1
package/lib/audit.js CHANGED
@@ -10,6 +10,8 @@ module.exports = source => {
10
10
  const clues = []
11
11
  const normalizedIsbn = normalize(source)
12
12
 
13
+ if (!(normalizedIsbn.length === 13 || normalizedIsbn.length === 10)) throw new Error('invalid input length')
14
+
13
15
  if (data) {
14
16
  result.groupname = data.groupname
15
17
  if (normalizedIsbn.length === 13) {
@@ -24,6 +26,9 @@ module.exports = source => {
24
26
  } else if (normalizedIsbn.startsWith('979')) {
25
27
  guessPrefixFromChecksum(normalizedIsbn, '978', clues)
26
28
  }
29
+ if (clues.length === 0) {
30
+ suggestCorrectChecksum(normalizedIsbn, clues)
31
+ }
27
32
  } else if (normalizedIsbn.length === 10) {
28
33
  guessMissingPrefixFromChecksum(normalizedIsbn, '978', clues)
29
34
  guessMissingPrefixFromChecksum(normalizedIsbn, '979', clues)
@@ -41,7 +46,7 @@ const considerAltPrefix = (normalizedIsbn, altPrefix, clues) => {
41
46
  const candidateData = parse(`${candidateBase}${checkDigit}`)
42
47
  if (candidateData != null) {
43
48
  const { isbn13h, isbn13, groupname } = candidateData
44
- clues.push({ message: 'possible prefix error', candidate: isbn13h, isbn13, groupname })
49
+ clues.push({ message: 'Possible prefix error', candidate: isbn13h, isbn13, groupname })
45
50
  }
46
51
  }
47
52
 
@@ -50,7 +55,7 @@ const guessPrefixFromChecksum = (normalizedIsbn, altPrefix, clues) => {
50
55
  const altPrefixData = parse(altPrefixIsbn)
51
56
  if (altPrefixData != null) {
52
57
  const { isbn13h, isbn13, groupname } = altPrefixData
53
- clues.push({ message: 'checksum hints different prefix', candidate: isbn13h, isbn13, groupname })
58
+ clues.push({ message: 'Checksum hints different prefix', candidate: isbn13h, isbn13, groupname })
54
59
  }
55
60
  }
56
61
 
@@ -59,7 +64,7 @@ const guessUnprefixedFromChecksum = (normalizedIsbn, clues) => {
59
64
  const unprefixData = parse(unprefixedIsbn)
60
65
  if (unprefixData != null) {
61
66
  const { isbn10h, isbn13, groupname } = unprefixData
62
- clues.push({ message: 'checksum hints that a 978 was added to an ISBN-10 without updating the checksum', candidate: isbn10h, isbn13, groupname })
67
+ clues.push({ message: 'Checksum hints that a 978 was added to an ISBN-10 without updating the checksum', candidate: isbn10h, isbn13, groupname })
63
68
  }
64
69
  }
65
70
 
@@ -68,8 +73,23 @@ const guessMissingPrefixFromChecksum = (normalizedIsbn, missingPrefix, clues) =>
68
73
  const prefixData = parse(prefixedIsbn)
69
74
  if (prefixData != null) {
70
75
  const { isbn13h, isbn13, groupname } = prefixData
71
- clues.push({ message: `checksum hints that it is an ISBN-13 without its ${missingPrefix} prefix`, candidate: isbn13h, isbn13, groupname })
76
+ clues.push({ message: `Checksum hints that it is an ISBN-13 without its ${missingPrefix} prefix`, candidate: isbn13h, isbn13, groupname })
72
77
  }
73
78
  }
74
79
 
75
80
  const normalize = input => input.replace(/[^\dX]/g, '')
81
+
82
+ const suggestCorrectChecksum = (normalizedIsbn, clues) => {
83
+ const candidateBase = normalizedIsbn.slice(0, -1)
84
+ const checkDigit = calculateCheckDigit(candidateBase)
85
+ const candidateData = parse(`${candidateBase}${checkDigit}`)
86
+ if (candidateData != null) {
87
+ const { isbn13h, isbn13, isbn10h, isbn10, groupname } = candidateData
88
+ const message = 'No alternative prefix or form could be found. Maybe the problem is the invalid checksum?'
89
+ if (normalizedIsbn.length === 13) {
90
+ clues.push({ message, candidate: isbn13h, isbn13, groupname })
91
+ } else {
92
+ clues.push({ message, candidate: isbn10h, isbn10, groupname })
93
+ }
94
+ }
95
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "isbn3",
3
3
  "description": "ISBN utils: parse, validate, format, audit",
4
- "version": "1.2.17",
4
+ "version": "1.2.18",
5
5
  "authors": [
6
6
  "Hetappi <hetappi.pm@gmail.com>",
7
7
  "Tadas S. <tadas@yoyo.lt>",