music-codes
Version:
Small library for music codes (ISRC, ISWC, UPC)
33 lines • 1.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseEAN = parseEAN;
exports.getEanFromPrefix = getEanFromPrefix;
const errors_js_1 = require("./errors.js");
const gtin_js_1 = require("./gtin.js");
const utils_js_1 = require("./utils.js");
/**
* EAN is an alias for GTIN-13
* @returns GTIN instance
*/
function parseEAN(ean, prefix) {
if (!/^[\d]{13}$/.test(ean)) {
throw new errors_js_1.ParseError(`Invalid EAN: ${ean}`);
}
return new gtin_js_1.GTIN(ean, prefix, true);
}
/**
* EAN is an alias for GTIN-13
* @returns GTIN instance
*/
function getEanFromPrefix(prefix) {
if (prefix.length < 1) {
throw new errors_js_1.ParseError(`Prefix is too short: ${prefix}`);
}
if (prefix.length > 12) {
throw new errors_js_1.ParseError(`Prefix is too long: ${prefix}`);
}
const code = prefix.padEnd(12, '0');
const checkDigit = (0, utils_js_1.getLuhnCheckDigit)(code);
return new gtin_js_1.GTIN(`${code}${checkDigit}`, prefix, false);
}
//# sourceMappingURL=ean.js.map