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