forked from tadas-s/isbnjs
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathget_group.js
More file actions
25 lines (23 loc) · 790 Bytes
/
get_group.js
File metadata and controls
25 lines (23 loc) · 790 Bytes
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
const groups = require('./groups')
module.exports = isbn13 => {
const prefix = isbn13.substring(0, 3)
const restAfterPrefix = isbn13.substring(3)
const foundGroup = findGroup(prefix, restAfterPrefix, 5)
if (!foundGroup) return null
return {
group: foundGroup.groupId,
groupPrefix: foundGroup.groupPrefix,
ranges: foundGroup.groupData.ranges,
restAfterGroup: restAfterPrefix.slice(foundGroup.groupId.length)
}
}
function findGroup (prefix, restAfterPrefix, maxGroupIdLength) {
let length = 0
while (length <= maxGroupIdLength) {
const groupId = restAfterPrefix.slice(0, length)
const groupPrefix = `${prefix}-${groupId}`
const groupData = groups[groupPrefix]
if (groupData) return { groupId, groupPrefix, groupData }
else length++
}
}