mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
31 lines
819 B
JavaScript
31 lines
819 B
JavaScript
const {slugify} = require('@tryghost/string');
|
|
const ValueObject = require('./shared/ValueObject');
|
|
|
|
const InvalidOfferCode = require('../errors').InvalidOfferCode;
|
|
|
|
/** @extends ValueObject<string> */
|
|
class OfferCode extends ValueObject {
|
|
/** @param {unknown} code */
|
|
static create(code) {
|
|
if (!code || typeof code !== 'string') {
|
|
throw new InvalidOfferCode({
|
|
message: 'Offer `code` must be a string.'
|
|
});
|
|
}
|
|
|
|
const slugged = slugify(code);
|
|
|
|
if (slugged.length > 191) {
|
|
throw new InvalidOfferCode({
|
|
message: 'Offer `code` can be a maximum of 191 characters.'
|
|
});
|
|
}
|
|
|
|
return new OfferCode(slugged);
|
|
}
|
|
|
|
static InvalidOfferCode = InvalidOfferCode;
|
|
}
|
|
|
|
module.exports = OfferCode;
|