isbn3 1.2.17 → 1.2.19

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.
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/lib/get_group.js CHANGED
@@ -1,32 +1,23 @@
1
1
  const groups = require('./groups')
2
2
 
3
- const groupsMap = {}
4
- Object.keys(groups).forEach(groupPrefix => {
5
- const [ prefix, group ] = groupPrefix.split('-')
6
- const groupFirstDigit = group[0]
7
- groupsMap[prefix] = groupsMap[prefix] || {}
8
- groupsMap[prefix][groupFirstDigit] = groupsMap[prefix][groupFirstDigit] || {}
9
- groupsMap[prefix][groupFirstDigit][group] = groups[groupPrefix]
10
- })
11
-
12
3
  module.exports = isbn13 => {
13
4
  const prefix = isbn13.substring(0, 3)
14
- if (!groupsMap[prefix]) return null
15
5
  const restAfterPrefix = isbn13.substring(3)
16
- const groupFirstDigit = restAfterPrefix[0]
17
- const prefixMap = groupsMap[prefix]
18
- if (!prefixMap) return null
19
- const possibleGroups = prefixMap[groupFirstDigit]
20
-
21
- for (let groupPrefix in possibleGroups) {
22
- if (restAfterPrefix.startsWith(groupPrefix)) {
23
- return {
24
- group: groupPrefix,
25
- ranges: possibleGroups[groupPrefix].ranges,
26
- restAfterGroup: restAfterPrefix.slice(groupPrefix.length)
27
- }
28
- }
6
+ const foundGroup = findGroup(prefix, restAfterPrefix, 5)
7
+ if (!foundGroup) return null
8
+ return {
9
+ group: foundGroup.groupId,
10
+ ranges: foundGroup.groupData.ranges,
11
+ restAfterGroup: restAfterPrefix.slice(foundGroup.groupId.length)
29
12
  }
13
+ }
30
14
 
31
- return null
15
+ function findGroup (prefix, restAfterPrefix, maxGroupIdLength) {
16
+ let length = 0
17
+ while (length <= maxGroupIdLength) {
18
+ const groupId = restAfterPrefix.slice(0, length)
19
+ const groupData = groups[`${prefix}-${groupId}`]
20
+ if (groupData) return { groupId, groupData }
21
+ else length++
22
+ }
32
23
  }
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.19",
5
5
  "authors": [
6
6
  "Hetappi <hetappi.pm@gmail.com>",
7
7
  "Tadas S. <tadas@yoyo.lt>",
@@ -40,8 +40,8 @@
40
40
  "engines": {
41
41
  "node": ">= 6.4.0"
42
42
  },
43
- "dependencies": {},
44
43
  "devDependencies": {
44
+ "@types/mocha": "^10.0.10",
45
45
  "babel-cli": "^6.26.0",
46
46
  "babel-preset-es2015": "^6.24.1",
47
47
  "babelify": "^8.0.0",