Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): implements the every-at cron syntax #45

Merged
merged 16 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/fields/every.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function strToArray(str, {min, max}){
if(!re.test(str)){
return null
}

let [, everyStr] = str.split('/')
let every = parseInt(everyStr)

Expand Down
59 changes: 59 additions & 0 deletions core/src/fields/everyAt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// x/y
import types from '../types'
const { EveryAtColumn } = types

let re = /^\d+\/\d+$/

function buildArray(min, max, at, every) {
let res = []
for(let i = at; i <= max; i+=every){
if(i >= min){
res.push(i)
}
}
return res.length > 0 ? res : null
}

function strToArray(str, {min, max}){
if(!re.test(str)){
return null
}

let [atStr, everyStr] = str.split('/')
let at = parseInt(atStr)
let every = parseInt(everyStr)

return buildArray(min, max, at, every)
}

function arrayToStr(arr, field){
let {min, max} = field

if(arr.length < 3){
return null
}

let at = arr[0]
let step = arr[1] - arr[0]

if(step <= 1){
return null
}

let computedArray = buildArray(min, max, at, step)
if (arr.length !== computedArray.length) {
return null
}

let missing = arr.filter((i) => !computedArray.includes(i))
if (missing.length > 0) {
return null
}

return new EveryAtColumn(field, step, at)
}

export default {
strToArray,
arrayToStr
}
3 changes: 2 additions & 1 deletion core/src/fields/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import any from './any'
import every from './every'
import everyAt from './everyAt'
import range from './range'
import value from './value'

let fieldTypes = [any, every, range, value]
let fieldTypes = [any, every, everyAt, range, value]

function strToArray(str, field){
let fields = str.split(',')
Expand Down
13 changes: 9 additions & 4 deletions core/src/locale/en.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
export default {
eachPeriod: {
eachPeriod: {
eachField: {
empty: 'every {{field.id}}',
value: '{{value.text}}',
range: '{{start.text}}-{{end.text}}',
everyX: 'every {{every.value}}'
everyX: 'every {{every.value}} {{field.id}}(s)',
everyXatY: 'every {{every.value}} {{field.id}}(s), starting at {{at.value}} {{field.id}}(s)'
},
monthField: {
prefix: 'in',
value: '{{value.alt}}',
range: '{{start.alt}}-{{end.alt}}',
everyXatY: 'every {{every.value}} {{field.id}}(s), starting in {{at.alt}}'
},
dayField: {
prefix: 'on'
prefix: 'on',
everyXatY: 'every {{every.value}} {{field.id}}(s), starting on the {{at.alt}} of the month'
},
dayOfWeekField: {
prefix: 'on',
empty: 'every day of the week',
value: '{{value.alt}}',
range: '{{start.alt}}-{{end.alt}}',
everyX: 'every {{every.value}} day(s) of the week',
everyXatY: 'every {{every.value}} day(s) of the week, starting on {{at.alt}}',
},
hourField: {
prefix: 'at'
Expand Down Expand Up @@ -46,4 +51,4 @@ export default {
},
periodPrefix: 'Every',
periodSuffix: ''
}
}
36 changes: 28 additions & 8 deletions core/src/locale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const locales = {
}

/**
*
* @param {string} locale=en
* @returns {object} object with all strings in the requested language
*
* @param {string} locale=en
* @returns {object} object with all strings in the requested language
*/
function getLocale(locale){
if(locales.hasOwnProperty(locale)){
Expand All @@ -20,16 +20,36 @@ function getLocale(locale){
}
}

/** Solution provided by https://stackoverflow.com/a/57518703
*
*
*/
function getLocaleOrdinalSuffix(locale, value) {
const ordRules = new Intl.PluralRules(locale, {type: "ordinal"});
const suffixes = {
one: "st",
two: "nd",
few: "rd",
other: "th"
};
const category = ordRules.select(value);
const suffix = suffixes[category];
return (value + suffix);
}

/**
*
* @param {string} locale
*
* @param {string} locale
* @returns {object} items for minute, hour, day, month and day of week
*/
function defaultItems(locale){
return {
minuteItems: genItems(0, 59, (value) => pad(value, 2)),
hourItems: genItems(0, 23, (value) => pad(value, 2)),
dayItems: genItems(1, 31),
dayItems: genItems(1, 31,
(value) => { return value+''},
(value) => getLocaleOrdinalSuffix(locale, value)
),
monthItems: genItems(1, 12, (value) => {
return new Date(2021, value-1, 1).toLocaleDateString(locale, {month: 'long'})
}, (value) => {
Expand Down Expand Up @@ -57,7 +77,7 @@ export default {
getSuffix: (locale, periodId, fieldId) => {
return traverse(locale, [periodId+'Period', 'eachPeriod'], [fieldId+'Field', 'eachField'], ['suffix']) || ''
},

defaultItems,
getLocale
}
}
46 changes: 36 additions & 10 deletions core/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const {getLocaleStr} = locale
class Field {

/**
*
* @param {String} name
* @param {Array} items
*
* @param {String} name
* @param {Array} items
*/
constructor(id, items){
this.id = id
Expand Down Expand Up @@ -37,8 +37,8 @@ class Field {
class CronColumn{

/**
*
* @param {Field} field
*
* @param {Field} field
*/
constructor(field){
this.field = field
Expand Down Expand Up @@ -76,19 +76,19 @@ class CronColumn{
}

class AnyColumn extends CronColumn {

get localeKey(){
return 'empty'
}

get value(){
return '*'
}

}

class RangeColumn extends CronColumn {

constructor(field, start, end){
super(field)
this.start = start
Expand All @@ -105,7 +105,7 @@ class RangeColumn extends CronColumn {
end: this.end
}
}

get value(){
return `${this.start}-${this.end}`
}
Expand Down Expand Up @@ -135,6 +135,31 @@ class EveryColumn extends CronColumn {

}

class EveryAtColumn extends CronColumn {

constructor(field, every, at){
super(field)
this.every = every
this.at = at
}

get localeKey(){
return 'everyXatY'
}

get localeParams(){
return {
every: this.every,
at: this.at,
}
}

get value(){
return `${this.at}/${this.every}`
}

}

class ValueColumn extends CronColumn {

constructor(field, value){
Expand Down Expand Up @@ -185,5 +210,6 @@ export default {
RangeColumn,
ValueColumn,
EveryColumn,
EveryAtColumn,
CombinedColumn,
}
}
10 changes: 9 additions & 1 deletion core/test/multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ test('test strToArray', () => {
expect(strToArray('100', r(0, 59))).toBe(null);
expect(strToArray('0', r(1, 10))).toBe(null);
expect(strToArray('*/90', r(1, 10))).toBe(null);

expect(strToArray('5/10', r(0, 59))).toEqual([5, 15, 25, 35, 45, 55]);
expect(strToArray('5/10', r(8, 59))).toEqual([15, 25, 35, 45, 55]);
expect(strToArray('3/8', r(7, 59))).toEqual([11, 19, 27, 35, 43, 51, 59]);

});

test('test arrayToStr', () => {
Expand All @@ -36,4 +41,7 @@ test('test arrayToStr', () => {
expect(arrayToStr([7,14,21,28], r(5, 30)).value).toEqual('*/7');
expect(arrayToStr([0,5,10], r(0, 20)).value).toEqual('0,5,10');
expect(arrayToStr([1,2,5,8,9,10], r(1, 10)).value).toEqual('1-2,5,8-10');
});

expect(arrayToStr([5, 15, 25, 35, 45, 55], r(0, 59)).value).toEqual('5/10');
expect(arrayToStr([15, 25, 35, 45, 55], r(10, 59)).value).toEqual('15/10');
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"babel-jest": "^26.6.3",
"jest": "^26.6.3",
"multi-semantic-release": "^2.11.0",
"node-gyp": "^10.0.1",
abichinger marked this conversation as resolved.
Show resolved Hide resolved
"rollup": "^2.38.5",
"rollup-jest": "^1.1.1",
"rollup-plugin-vue": "^5.1.9",
Expand Down
Loading
Loading