Skip to content

Instantly share code, notes, and snippets.

@modernlabrat
Created June 20, 2021 18:41
Show Gist options
  • Save modernlabrat/537c84f029cdb83d065bb7b6db866ca0 to your computer and use it in GitHub Desktop.
Save modernlabrat/537c84f029cdb83d065bb7b6db866ca0 to your computer and use it in GitHub Desktop.
Cron Decoder
import { ref } from 'vue'
const useCronDecoder = () => {
const decodeMinutes = (stringDate, time) => {
let date = new Date(`${stringDate} ${time}`)
let minutes = date.getMinutes();
console.log('minutes: ', minutes)
return minutes
}
const decodeHours = (stringDate, time) => {
let date = new Date(`${stringDate} ${time}`)
console.log(date)
let hours = date.getHours();
console.log('hours: ', hours)
return hours
}
const decodeDays = (days) => {
let tempDays = ''
for (const day in days) {
tempDays = tempDays.concat(`${days[day].toUpperCase()},`)
}
const validDays = tempDays.slice(0, -1)
console.log(tempDays, ' => ', validDays)
return validDays
}
const decode = (data) => {
const repeatOption = data.repeatOption
const time = data.time
const stringDate = data.date
console.log(repeatOption)
console.log(time)
console.log(stringDate)
const hours = decodeHours(stringDate, time)
const minutes = decodeMinutes(stringDate, time)
const expression = ref('')
if (repeatOption == 'Daily') {
expression.value = `${minutes} ${hours} * * *`
} else if ( repeatOption == 'Custom') {
const prdOption = data.prdOption
const everyNth = data.everyNth
if ( prdOption == 'Daily') {
expression.value = `${minutes} ${hours} * * */${everyNth}`
} else if ( prdOption == 'Weekly') {
const days = decodeDays(data.days)
expression.value = `${minutes} ${hours} * * ${days}`
}
} else if (repeatOption == 'Never') {
var scheduledDate = new Date(`${stringDate} ${time}`)
expression.value = scheduledDate
}
return expression
}
return { decode }
}
export default useCronDecoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment