UNPKG

music-codes

Version:
69 lines 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ISRC = void 0; exports.parseISRC = parseISRC; const errors_js_1 = require("./errors.js"); class ISRC { constructor(prefix, year, code) { if (!/^([a-zA-Z]{2}[a-zA-Z0-9]{3})$/.test(prefix)) { throw new errors_js_1.ParseError(`Invalid prefix: ${prefix}`); } if (year < 0 || year > 99) { throw new errors_js_1.ParseError(`Invalid year: ${year}`); } if (code < 0 || code > 99999) { throw new errors_js_1.ParseError(`Invalid code: ${code}`); } this.prefix = prefix.toUpperCase(); this.year = year; this.code = code; } first() { return new ISRC(this.prefix, this.year, 0); } last() { return new ISRC(this.prefix, this.year, 99999); } previous() { if (this.code === 0) { throw new errors_js_1.BoundaryError(`ISRC boundary reached: ${this.toString()}`); } return new ISRC(this.prefix, this.year, this.code - 1); } next() { if (this.code === 99999) { throw new errors_js_1.BoundaryError(`ISRC boundary reached: ${this.toString()}`); } return new ISRC(this.prefix, this.year, this.code + 1); } *iterate(up = true) { let isrc = this; while (true) { yield isrc; try { isrc = up ? isrc.next() : isrc.previous(); } catch { break; } } } toString() { return `${this.prefix}${this.year.toString().padStart(2, '0')}${this.code.toString().padStart(5, '0')}`; } } exports.ISRC = ISRC; function parseISRC(isrc, prefix = undefined) { const parts = isrc.match(/^([a-zA-Z]{2}[a-zA-Z0-9]{3})([\d]{2})([\d]{5})$/i); if (!parts) { throw new errors_js_1.ParseError(`Invalid ISRC: ${isrc}`); } if (prefix && parts[1] !== prefix) { throw new errors_js_1.ParseError(`Invalid prefix '${prefix}' for '${isrc}'`); } const thePrefix = parts[1] ?? ''; const year = Number.parseInt(parts[2] ?? '', 10); const code = Number.parseInt(parts[3] ?? '', 10); return new ISRC(thePrefix, year, code); } //# sourceMappingURL=isrc.js.map