UNPKG

isbn3

Version:

ISBN utils: parse, validate, format, audit

26 lines (23 loc) 790 B
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++ } }