isbn3 1.2.18 → 2.0.0
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/README.md +25 -9
- package/lib/audit.js +51 -29
- package/lib/get_group.js +17 -24
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -179,22 +179,38 @@ ISBN.audit('9781090648525')
|
|
|
179
179
|
// "groupname": "English language",
|
|
180
180
|
// "clues": [
|
|
181
181
|
// {
|
|
182
|
-
// "message": "
|
|
183
|
-
// "candidate":
|
|
184
|
-
//
|
|
182
|
+
// "message": "Possible prefix error",
|
|
183
|
+
// "candidate": {
|
|
184
|
+
// "isbn13h": "979-10-90648-52-4",
|
|
185
|
+
// "isbn13": "9791090648524",
|
|
186
|
+
// "groupname": "France"
|
|
187
|
+
// }
|
|
185
188
|
// }
|
|
186
189
|
// ]
|
|
187
190
|
// }
|
|
188
191
|
|
|
189
192
|
ISBN.audit('978-1-0906-4852-4')
|
|
190
193
|
// {
|
|
191
|
-
// "source":"978-1-0906-4852-4",
|
|
192
|
-
// "validIsbn":false,
|
|
193
|
-
// "clues":[
|
|
194
|
+
// "source": "978-1-0906-4852-4",
|
|
195
|
+
// "validIsbn": false,
|
|
196
|
+
// "clues": [
|
|
197
|
+
// {
|
|
198
|
+
// "message": "Found a matching group and publisher range, but the checksum didn't match. Could the checksum be wrong?",
|
|
199
|
+
// "candidate": {
|
|
200
|
+
// "isbn13h": "978-1-0906-4852-5",
|
|
201
|
+
// "isbn13": "9781090648525",
|
|
202
|
+
// "isbn10h": "1-0906-4852-9",
|
|
203
|
+
// "isbn10": "1090648529",
|
|
204
|
+
// "groupname": "English language"
|
|
205
|
+
// }
|
|
206
|
+
// },
|
|
194
207
|
// {
|
|
195
|
-
// "message":"
|
|
196
|
-
// "candidate":
|
|
197
|
-
//
|
|
208
|
+
// "message": "Checksum hints different prefix",
|
|
209
|
+
// "candidate": {
|
|
210
|
+
// "isbn13h": "979-10-90648-52-4",
|
|
211
|
+
// "isbn13": "9791090648524",
|
|
212
|
+
// "groupname": "France"
|
|
213
|
+
// }
|
|
198
214
|
// }
|
|
199
215
|
// ]
|
|
200
216
|
// }
|
package/lib/audit.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const parse = require('./parse')
|
|
2
2
|
const calculateCheckDigit = require('./calculate_check_digit')
|
|
3
|
+
const getGroup = require('./get_group.js')
|
|
4
|
+
const groups = require('./groups.js')
|
|
5
|
+
const splitIsbnParts = require('./split_isbn_parts.js')
|
|
3
6
|
|
|
4
7
|
module.exports = source => {
|
|
5
8
|
if (typeof source !== 'string' || source.length === 0) throw Error(`invalid input: ${source}`)
|
|
@@ -19,20 +22,7 @@ module.exports = source => {
|
|
|
19
22
|
else if (normalizedIsbn.startsWith('979')) considerAltPrefix(normalizedIsbn, '978', clues)
|
|
20
23
|
}
|
|
21
24
|
} else {
|
|
22
|
-
|
|
23
|
-
if (normalizedIsbn.startsWith('978')) {
|
|
24
|
-
guessPrefixFromChecksum(normalizedIsbn, '979', clues)
|
|
25
|
-
guessUnprefixedFromChecksum(normalizedIsbn, clues)
|
|
26
|
-
} else if (normalizedIsbn.startsWith('979')) {
|
|
27
|
-
guessPrefixFromChecksum(normalizedIsbn, '978', clues)
|
|
28
|
-
}
|
|
29
|
-
if (clues.length === 0) {
|
|
30
|
-
suggestCorrectChecksum(normalizedIsbn, clues)
|
|
31
|
-
}
|
|
32
|
-
} else if (normalizedIsbn.length === 10) {
|
|
33
|
-
guessMissingPrefixFromChecksum(normalizedIsbn, '978', clues)
|
|
34
|
-
guessMissingPrefixFromChecksum(normalizedIsbn, '979', clues)
|
|
35
|
-
}
|
|
25
|
+
lookForPossibleInvalityCauses(normalizedIsbn, clues)
|
|
36
26
|
}
|
|
37
27
|
|
|
38
28
|
result.clues = clues
|
|
@@ -40,13 +30,44 @@ module.exports = source => {
|
|
|
40
30
|
return result
|
|
41
31
|
}
|
|
42
32
|
|
|
33
|
+
function lookForPossibleInvalityCauses (normalizedIsbn, clues) {
|
|
34
|
+
const foundGroup = getGroup(normalizedIsbn)
|
|
35
|
+
if (foundGroup) {
|
|
36
|
+
const { groupPrefix } = foundGroup
|
|
37
|
+
const groupInfo = groups[groupPrefix]
|
|
38
|
+
const partsData = splitIsbnParts(normalizedIsbn)
|
|
39
|
+
if (partsData) {
|
|
40
|
+
const candidateBase = normalizedIsbn.slice(0, -1)
|
|
41
|
+
const checkDigit = calculateCheckDigit(candidateBase)
|
|
42
|
+
const candidateData = parse(`${candidateBase}${checkDigit}`)
|
|
43
|
+
const message = "Found a matching group and publisher range, but the checksum didn't match. Could the checksum be wrong?"
|
|
44
|
+
clues.push({ message, candidate: formatCandidate(candidateData) })
|
|
45
|
+
} else {
|
|
46
|
+
clues.push({ message: 'Found a matching group but no matching publisher range', group: { prefix: groupPrefix, name: groupInfo.name } })
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
clues.push({ message: 'Could not find a matching ISBN group' })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (normalizedIsbn.length === 13) {
|
|
53
|
+
if (normalizedIsbn.startsWith('978')) {
|
|
54
|
+
guessPrefixFromChecksum(normalizedIsbn, '979', clues)
|
|
55
|
+
guessUnprefixedFromChecksum(normalizedIsbn, clues)
|
|
56
|
+
} else if (normalizedIsbn.startsWith('979')) {
|
|
57
|
+
guessPrefixFromChecksum(normalizedIsbn, '978', clues)
|
|
58
|
+
}
|
|
59
|
+
} else if (normalizedIsbn.length === 10) {
|
|
60
|
+
guessMissingPrefixFromChecksum(normalizedIsbn, '978', clues)
|
|
61
|
+
guessMissingPrefixFromChecksum(normalizedIsbn, '979', clues)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
43
65
|
const considerAltPrefix = (normalizedIsbn, altPrefix, clues) => {
|
|
44
66
|
const candidateBase = `${altPrefix}${normalizedIsbn.substring(3, 12)}`
|
|
45
67
|
const checkDigit = calculateCheckDigit(candidateBase)
|
|
46
68
|
const candidateData = parse(`${candidateBase}${checkDigit}`)
|
|
47
69
|
if (candidateData != null) {
|
|
48
|
-
|
|
49
|
-
clues.push({ message: 'Possible prefix error', candidate: isbn13h, isbn13, groupname })
|
|
70
|
+
clues.push({ message: 'Possible prefix error', candidate: formatCandidate(candidateData) })
|
|
50
71
|
}
|
|
51
72
|
}
|
|
52
73
|
|
|
@@ -54,8 +75,7 @@ const guessPrefixFromChecksum = (normalizedIsbn, altPrefix, clues) => {
|
|
|
54
75
|
const altPrefixIsbn = `${altPrefix}${normalizedIsbn.substring(3)}`
|
|
55
76
|
const altPrefixData = parse(altPrefixIsbn)
|
|
56
77
|
if (altPrefixData != null) {
|
|
57
|
-
|
|
58
|
-
clues.push({ message: 'Checksum hints different prefix', candidate: isbn13h, isbn13, groupname })
|
|
78
|
+
clues.push({ message: 'Checksum hints different prefix', candidate: formatCandidate(altPrefixData) })
|
|
59
79
|
}
|
|
60
80
|
}
|
|
61
81
|
|
|
@@ -63,8 +83,7 @@ const guessUnprefixedFromChecksum = (normalizedIsbn, clues) => {
|
|
|
63
83
|
const unprefixedIsbn = normalizedIsbn.substring(3)
|
|
64
84
|
const unprefixData = parse(unprefixedIsbn)
|
|
65
85
|
if (unprefixData != null) {
|
|
66
|
-
|
|
67
|
-
clues.push({ message: 'Checksum hints that a 978 was added to an ISBN-10 without updating the checksum', candidate: isbn10h, isbn13, groupname })
|
|
86
|
+
clues.push({ message: 'Checksum hints that a 978 was added to an ISBN-10 without updating the checksum', candidate: formatCandidate(unprefixData) })
|
|
68
87
|
}
|
|
69
88
|
}
|
|
70
89
|
|
|
@@ -72,8 +91,7 @@ const guessMissingPrefixFromChecksum = (normalizedIsbn, missingPrefix, clues) =>
|
|
|
72
91
|
const prefixedIsbn = `${missingPrefix}${normalizedIsbn}`
|
|
73
92
|
const prefixData = parse(prefixedIsbn)
|
|
74
93
|
if (prefixData != null) {
|
|
75
|
-
|
|
76
|
-
clues.push({ message: `Checksum hints that it is an ISBN-13 without its ${missingPrefix} prefix`, candidate: isbn13h, isbn13, groupname })
|
|
94
|
+
clues.push({ message: `Checksum hints that it is an ISBN-13 without its ${missingPrefix} prefix`, candidate: formatCandidate(prefixData) })
|
|
77
95
|
}
|
|
78
96
|
}
|
|
79
97
|
|
|
@@ -84,12 +102,16 @@ const suggestCorrectChecksum = (normalizedIsbn, clues) => {
|
|
|
84
102
|
const checkDigit = calculateCheckDigit(candidateBase)
|
|
85
103
|
const candidateData = parse(`${candidateBase}${checkDigit}`)
|
|
86
104
|
if (candidateData != null) {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
105
|
+
const message = 'Maybe the problem is the invalid checksum?'
|
|
106
|
+
clues.push({ message, candidate: formatCandidate(candidateData) })
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function formatCandidate (candidateData) {
|
|
111
|
+
const { isbn13h, isbn13, isbn10h, isbn10, groupname } = candidateData
|
|
112
|
+
if (isbn10h) {
|
|
113
|
+
return { isbn13h, isbn13, isbn10h, isbn10, groupname }
|
|
114
|
+
} else {
|
|
115
|
+
return { isbn13h, isbn13, groupname }
|
|
94
116
|
}
|
|
95
117
|
}
|
package/lib/get_group.js
CHANGED
|
@@ -1,32 +1,25 @@
|
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
groupPrefix: foundGroup.groupPrefix,
|
|
11
|
+
ranges: foundGroup.groupData.ranges,
|
|
12
|
+
restAfterGroup: restAfterPrefix.slice(foundGroup.groupId.length)
|
|
29
13
|
}
|
|
14
|
+
}
|
|
30
15
|
|
|
31
|
-
|
|
16
|
+
function findGroup (prefix, restAfterPrefix, maxGroupIdLength) {
|
|
17
|
+
let length = 0
|
|
18
|
+
while (length <= maxGroupIdLength) {
|
|
19
|
+
const groupId = restAfterPrefix.slice(0, length)
|
|
20
|
+
const groupPrefix = `${prefix}-${groupId}`
|
|
21
|
+
const groupData = groups[groupPrefix]
|
|
22
|
+
if (groupData) return { groupId, groupPrefix, groupData }
|
|
23
|
+
else length++
|
|
24
|
+
}
|
|
32
25
|
}
|
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": "
|
|
4
|
+
"version": "2.0.0",
|
|
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",
|