UNPKG

cheetah-framework

Version:

Cheetah Framework JS used in all our applications

78 lines (62 loc) 1.76 kB
import { Model } from './Model' import Cronstrue from 'cronstrue/i18n' import config from '@cheetah/config' const validatorRegexp = _.reduce(_.keys(config.cronValidator), (accumulator, key) => { accumulator[key] = typeof config.cronValidator[key] === 'string' ? new RegExp(_.trim(config.cronValidator[key], '/')) : config.cronValidator[key] return accumulator }, {}) class Cron extends Model { constructor (data) { super(data) Cron.properties.forEach(prop => { _.set(this, prop, _.get(this, prop, '*') || '*') }) } static get properties () { return [ 'minute', 'hour', 'day_of_month', 'month', 'day_of_week', ] } toString () { return Cron.toString(this) } static toString (payload) { return Cron.properties.map(prop => { return _.get(payload, prop, '') }).join(' ') } get isValid () { return Cron.isValid(this) } static isValid (payload) { return _.every(Cron.properties, prop => { return !_.includes(['', null, undefined], payload[prop]) && Cron.validate(prop, payload[prop]) }) } validate (prop) { return Cron.validate(prop, this[prop]) } static validate (prop, value) { return validatorRegexp[prop] instanceof RegExp && validatorRegexp[prop].test(value) } get humanReadable () { return Cron.humanReadable(this) } static humanReadable (payload) { if (Cron.isValid(payload)) { return Cronstrue.toString(Cron.toString(payload), { locale: cheetahApp.locale }) } return cheetahApp.$t('cron.invalid') } get defaultValue () { return _.reduce(Cron.properties, (defaultValue, prop) => { defaultValue[prop] = '*' return defaultValue }, {}) } } export default Cron