cron-converter-u2q 0.1.15 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/__tests__/index.test.ts +74 -25
- package/src/converter.ts +23 -19
- package/src/describer.ts +58 -0
- package/src/helper.ts +19 -0
- package/src/index.ts +1 -0
- package/.github/workflows/integration.yml +0 -33
- package/.github/workflows/release.yml +0 -55
package/README.md
CHANGED
|
@@ -15,13 +15,13 @@ Easily convert cron expressions between Unix and Quartz formats with the `cron-c
|
|
|
15
15
|
Using npm:
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install cron-converter
|
|
18
|
+
npm install cron-converter-u2q
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Using yarn:
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
|
-
yarn add cron-converter
|
|
24
|
+
yarn add cron-converter-u2q
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Usage
|
package/package.json
CHANGED
|
@@ -1,41 +1,86 @@
|
|
|
1
|
-
import { CronConverterU2Q as
|
|
1
|
+
import { CronConverterU2Q as converter, CronDescriberU2Q as describer } from '../index';
|
|
2
2
|
|
|
3
3
|
//Basic suite of tests
|
|
4
4
|
describe('Unix2Quartz Conversion', () => {
|
|
5
5
|
|
|
6
|
+
test('Every minute', () => {
|
|
7
|
+
const result = converter.unixToQuartz('* * * * *');
|
|
8
|
+
expect(result).toBe("0 * * ? * *");
|
|
9
|
+
});
|
|
10
|
+
|
|
6
11
|
test('Every 5 minutes', () => {
|
|
7
|
-
const result =
|
|
12
|
+
const result = converter.unixToQuartz('*/5 * * * *');
|
|
8
13
|
expect(result).toBe("0 */5 * ? * *");
|
|
9
14
|
});
|
|
10
15
|
|
|
16
|
+
test('Every hour at minute 30', () => {
|
|
17
|
+
const result = converter.unixToQuartz('30 * * * *');
|
|
18
|
+
expect(result).toBe("0 30 * ? * *");
|
|
19
|
+
});
|
|
20
|
+
|
|
11
21
|
test('Everyday at 12pm', () => {
|
|
12
|
-
const result =
|
|
22
|
+
const result = converter.unixToQuartz('0 12 * * *');
|
|
13
23
|
expect(result).toBe("0 0 12 ? * *");
|
|
14
24
|
});
|
|
15
25
|
|
|
16
|
-
test('Every Monday at 12pm
|
|
17
|
-
const result =
|
|
26
|
+
test('Every Monday at 12pm', () => {
|
|
27
|
+
const result = converter.unixToQuartz('0 12 * * 1');
|
|
18
28
|
expect(result).toBe("0 0 12 ? * 1");
|
|
19
29
|
});
|
|
20
30
|
|
|
21
|
-
test('Every 10th day of the month
|
|
22
|
-
const result =
|
|
31
|
+
test('Every 10th day of the month', () => {
|
|
32
|
+
const result = converter.unixToQuartz('0 0 10 * *');
|
|
23
33
|
expect(result).toBe("0 0 0 10 * ?");
|
|
24
34
|
});
|
|
25
35
|
|
|
26
|
-
test('Every January 1st at 12am
|
|
27
|
-
const result =
|
|
36
|
+
test('Every January 1st at 12am', () => {
|
|
37
|
+
const result = converter.unixToQuartz('0 0 1 1 *');
|
|
28
38
|
expect(result).toBe("0 0 0 1 1 ?");
|
|
29
39
|
});
|
|
30
40
|
|
|
31
|
-
test('Every last day of the month
|
|
32
|
-
const result =
|
|
41
|
+
test('Every last day of the month', () => {
|
|
42
|
+
const result = converter.unixToQuartz('59 23 L * *');
|
|
33
43
|
expect(result).toBe("0 59 23 L * ?");
|
|
34
44
|
});
|
|
35
45
|
|
|
36
|
-
|
|
46
|
+
test('Every day at 2:30 AM', () => {
|
|
47
|
+
const result = converter.unixToQuartz('30 2 * * *');
|
|
48
|
+
expect(result).toBe('0 30 2 ? * *');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('Every Monday at 2:30 AM', () => {
|
|
52
|
+
const result = converter.unixToQuartz('30 2 * * 1');
|
|
53
|
+
expect(result).toBe('0 30 2 ? * 1');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('Every day at 1:00 AM and 1:00 PM', () => {
|
|
57
|
+
const result = converter.unixToQuartz('0 1,13 * * *');
|
|
58
|
+
expect(result).toBe('0 0 1,13 ? * *');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('Every 15 minutes', () => {
|
|
62
|
+
const result = converter.unixToQuartz('*/15 * * * *');
|
|
63
|
+
expect(result).toBe('0 */15 * ? * *');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
//Include
|
|
67
|
+
// test('Last day of every month at 4:45 PM conversion', () => {
|
|
68
|
+
// const result = converter.unixToQuartz('45 16 28-31 * *');
|
|
69
|
+
// expect(result).toBe('45 16 L * ?');
|
|
70
|
+
// });
|
|
71
|
+
|
|
72
|
+
// test('Last Sunday of every month at 4:45 PM conversion', () => {
|
|
73
|
+
// const result = converter.unixToQuartz('45 16 22-31 * 0');
|
|
74
|
+
// expect(result).toBe('45 16 ? * 7L');
|
|
75
|
+
// });
|
|
76
|
+
|
|
77
|
+
// test('Second Wednesday of every month at 1:00 AM conversion', () => {
|
|
78
|
+
// const result = converter.quartzToUnix('0 1 ? * 3#2');
|
|
79
|
+
// expect(result).toBe('Not directly possible without additional logic');
|
|
80
|
+
// });
|
|
81
|
+
|
|
37
82
|
// test('Every last Friday of the month conversion', () => {
|
|
38
|
-
// const result =
|
|
83
|
+
// const result = converter.unixToQuartz('0 0 * * 5'); //Find equivalent unix expression
|
|
39
84
|
// expect(result).toBe("0 0 0 ? * 5L");
|
|
40
85
|
// });
|
|
41
86
|
|
|
@@ -45,38 +90,42 @@ describe('Unix2Quartz Conversion', () => {
|
|
|
45
90
|
describe('Quartz2Unix Conversion', () => {
|
|
46
91
|
|
|
47
92
|
test('Every 5 minutes conversion', () => {
|
|
48
|
-
const result =
|
|
93
|
+
const result = converter.quartzToUnix('0 */5 * ? * *');
|
|
49
94
|
expect(result).toBe("*/5 * * * *");
|
|
50
95
|
});
|
|
51
96
|
|
|
52
97
|
test('Everyday at 12pm conversion', () => {
|
|
53
|
-
const result =
|
|
98
|
+
const result = converter.quartzToUnix('0 0 12 ? * *');
|
|
54
99
|
expect(result).toBe("0 12 * * *");
|
|
55
100
|
});
|
|
56
101
|
|
|
57
102
|
test('Every Monday at 12pm conversion', () => {
|
|
58
|
-
const result =
|
|
103
|
+
const result = converter.quartzToUnix('0 0 12 ? * 1');
|
|
59
104
|
expect(result).toBe("0 12 * * 1");
|
|
60
105
|
});
|
|
61
106
|
|
|
62
107
|
test('Every 10th day of the month conversion', () => {
|
|
63
|
-
const result =
|
|
108
|
+
const result = converter.quartzToUnix('0 0 0 10 * ?');
|
|
64
109
|
expect(result).toBe("0 0 10 * *");
|
|
65
110
|
});
|
|
66
111
|
|
|
67
112
|
test('Every January 1st at 12am conversion', () => {
|
|
68
|
-
const result =
|
|
113
|
+
const result = converter.quartzToUnix('0 0 0 1 1 ?');
|
|
69
114
|
expect(result).toBe("0 0 1 1 *");
|
|
70
115
|
});
|
|
71
116
|
|
|
72
117
|
test('Every last day of the month conversion', () => {
|
|
73
|
-
const result =
|
|
118
|
+
const result = converter.quartzToUnix('0 59 23 L * ?');
|
|
74
119
|
expect(result).toBe("59 23 L * *");
|
|
75
120
|
});
|
|
121
|
+
});
|
|
76
122
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
// describe('Unix Description', () => {
|
|
126
|
+
|
|
127
|
+
// test('Describe every 5 minutes', () => {
|
|
128
|
+
// const result = describer.describe('*/5 * * * *');
|
|
129
|
+
// expect(result).toBe("Every 5 minutes");
|
|
130
|
+
// });
|
|
131
|
+
// });
|
package/src/converter.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { ExpressionHelper as helper } from './helper';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
static readonly unixExpressionLength = 5;
|
|
5
|
-
static readonly quartzExpressionLengths = [6, 7];
|
|
3
|
+
export class CronConverterU2Q {
|
|
6
4
|
static readonly everyXUnitsReplacePlaceholder = `%s`
|
|
7
5
|
static readonly quartzEveryXUnitsRegex = /^0\/(\d+)$/; // For handling 0/5 units
|
|
8
6
|
static readonly unixEveryXUnitsRegex = /^\/(\d+)$/; // For handling */5 units
|
|
@@ -15,14 +13,16 @@ export class CronConverterU2Q {
|
|
|
15
13
|
* @returns the corresponding quartz expression
|
|
16
14
|
*/
|
|
17
15
|
public static unixToQuartz(unixExpression: string): string {
|
|
16
|
+
const parts = helper.GetExpressionParts(unixExpression);
|
|
17
|
+
const [min, hour, dom, month, dow] = parts.map(part => this.convertIntervalParts(part));
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
// Converting Unix DOW to Quartz DOW
|
|
20
|
+
let quartzDow = dow.includes(',') ? dow.split(',').map(day => {
|
|
21
|
+
if (day === '0' || day === '7') return '1';
|
|
22
|
+
return (parseInt(day, 10) + 1).toString();
|
|
23
|
+
}).join(',') : dow;
|
|
22
24
|
|
|
23
|
-
const [min, hour, dom, month, dow] = parts.map(part => this.convertIntervalParts(part));
|
|
24
25
|
let quartzDom = dom;
|
|
25
|
-
let quartzDow = dow;
|
|
26
26
|
|
|
27
27
|
if (dom !== '*' && dow === '*') quartzDow = '?';
|
|
28
28
|
else if (dom === '*') quartzDom = '?';
|
|
@@ -36,23 +36,27 @@ export class CronConverterU2Q {
|
|
|
36
36
|
* @returns the corresponding unix expression
|
|
37
37
|
*/
|
|
38
38
|
public static quartzToUnix(quartzExpression: string): string {
|
|
39
|
+
const parts = helper.GetExpressionParts(quartzExpression);
|
|
40
|
+
const [_, min, hour, dom, month, dow] = parts.map(part => this.convertIntervalParts(part, true));
|
|
39
41
|
|
|
40
|
-
this.validateIfNullOrEmpty(quartzExpression);
|
|
41
|
-
const parts = quartzExpression.replace('?', '*').split(this.delimiter);
|
|
42
|
-
if (!this.quartzExpressionLengths.includes(parts.length)) throw new Error(`Invalid quartz cron format`);
|
|
43
42
|
|
|
44
|
-
|
|
43
|
+
// Converting Quartz DOW to Unix DOW
|
|
44
|
+
let unixDow = dow.includes(',') ? dow.split(',').map(day => {
|
|
45
|
+
if (day === '1') return '0';
|
|
46
|
+
if (day === '?') return '*';
|
|
47
|
+
return (parseInt(day, 10) - 1).toString();
|
|
48
|
+
}).join(',') : dow;
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
let unixDom = dom;
|
|
51
|
+
|
|
52
|
+
// If dow in Quartz was '?', set unixDom to '*'
|
|
53
|
+
if (dow === '?') unixDom = '*';
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
|
|
56
|
+
return `${min} ${hour} ${unixDom} ${month} ${unixDow}`;
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
private static convertIntervalParts(part: string, isQuartz = false): string {
|
|
54
|
-
part = part.trim();
|
|
55
|
-
|
|
56
60
|
const everyXUnitsPattern = isQuartz ? this.quartzEveryXUnitsRegex : this.unixEveryXUnitsRegex;
|
|
57
61
|
const matches = part.match(everyXUnitsPattern);
|
|
58
62
|
const everyXUnitsReplacePattern = isQuartz ? this.quartzEveryXUnitsReplacePattern : this.unixEveryXUnitsReplacePattern;
|
package/src/describer.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ExpressionHelper as helper } from './helper';
|
|
2
|
+
|
|
3
|
+
export class CronDescriberU2Q {
|
|
4
|
+
|
|
5
|
+
public static describe(expression: string): string {
|
|
6
|
+
const [second, min, hour, dom, month, dow] = helper.GetExpressionParts(expression);
|
|
7
|
+
return `${this.describeSecond(second)} ${this.describeMinute(min)} ${this.describeHour(hour)} ${this.describeDayOfMonth(dom)} ${this.describeMonth(month)} ${this.describeDayOfWeek(dow)}`.trim();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// #region Standard cron description logic
|
|
11
|
+
|
|
12
|
+
private static describeSecond(second: string): string {
|
|
13
|
+
if (second === '*') return 'Every second';
|
|
14
|
+
if (second.startsWith('*/')) return `Every ${second.split('/')[1]} seconds`;
|
|
15
|
+
return `At second ${second}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private static describeMinute(min: string): string {
|
|
19
|
+
if (min === '*') return 'Every minute';
|
|
20
|
+
if (min.startsWith('*/')) return `Every ${min.split('/')[1]} minutes`;
|
|
21
|
+
return `At minute ${min}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private static describeHour(hour: string): string {
|
|
25
|
+
if (hour === '*') return 'of every hour';
|
|
26
|
+
if (hour.startsWith('*/')) return `Every ${hour.split('/')[1]} hours`;
|
|
27
|
+
return `At ${hour} o'clock`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private static describeDayOfMonth(dom: string): string {
|
|
31
|
+
if (dom === '*') return 'on every day';
|
|
32
|
+
return `on the ${this.ordinalSuffix(Number(dom))} of the month`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private static describeMonth(month: string): string {
|
|
36
|
+
if (month === '*') return 'of every month';
|
|
37
|
+
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
38
|
+
return `in ${months[Number(month) - 1]}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private static describeDayOfWeek(dow: string): string {
|
|
42
|
+
if (dow === '*') return '';
|
|
43
|
+
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
|
|
44
|
+
return `on ${days[Number(dow)]}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private static ordinalSuffix(i: number): string {
|
|
48
|
+
const j = i % 10;
|
|
49
|
+
const k = i % 100;
|
|
50
|
+
if (j == 1 && k != 11) return i + "st";
|
|
51
|
+
if (j == 2 && k != 12) return i + "nd";
|
|
52
|
+
if (j == 3 && k != 13) return i + "rd";
|
|
53
|
+
return i + "th";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
|
|
58
|
+
}
|
package/src/helper.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class ExpressionHelper {
|
|
2
|
+
static readonly delimiter = ' ';
|
|
3
|
+
static readonly unixExpressionLength = 5;
|
|
4
|
+
static readonly quartzExpressionLengths = [6, 7];
|
|
5
|
+
|
|
6
|
+
public static GetExpressionParts(expression: string): string[] {
|
|
7
|
+
this.validateIfNullOrEmpty(expression);
|
|
8
|
+
const parts = expression.split(this.delimiter).map(part => part.trim());
|
|
9
|
+
if (this.quartzExpressionLengths.includes(parts.length)) return parts.map(part => part.replace('?', '*')) //if quartz, cleanup
|
|
10
|
+
if (this.unixExpressionLength == parts.length) return parts;// if unix, return as it is
|
|
11
|
+
|
|
12
|
+
throw new Error(`Invalid cron expression!`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private static validateIfNullOrEmpty(cronExpression: string | undefined | null): void {
|
|
16
|
+
if (!cronExpression || cronExpression.trim() === '') throw new Error('Empty or null expression');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
name: Build
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches: [main]
|
|
5
|
-
pull_request:
|
|
6
|
-
branches: [main]
|
|
7
|
-
jobs:
|
|
8
|
-
build:
|
|
9
|
-
name: Build and Test
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
strategy:
|
|
12
|
-
matrix:
|
|
13
|
-
node-version: [16.x, 18.x, 20.x]
|
|
14
|
-
steps:
|
|
15
|
-
- name: Checkout code
|
|
16
|
-
uses: actions/checkout@v3
|
|
17
|
-
|
|
18
|
-
- name: Setup Node.js ${{ matrix.node-version }}
|
|
19
|
-
uses: actions/setup-node@v3
|
|
20
|
-
with:
|
|
21
|
-
node-version: ${{ matrix.node-version }}
|
|
22
|
-
|
|
23
|
-
- name: Clean Install Dependencies
|
|
24
|
-
run: npm ci
|
|
25
|
-
|
|
26
|
-
- name: Run Tests
|
|
27
|
-
run: npm test
|
|
28
|
-
|
|
29
|
-
- name: Linting
|
|
30
|
-
run: npm run lint
|
|
31
|
-
|
|
32
|
-
- name: Run build
|
|
33
|
-
run: npm run build --if-present
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
name: Release
|
|
2
|
-
on:
|
|
3
|
-
workflow_run:
|
|
4
|
-
workflows: ["Build"]
|
|
5
|
-
types:
|
|
6
|
-
- completed
|
|
7
|
-
jobs:
|
|
8
|
-
release:
|
|
9
|
-
permissions: write-all
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
steps:
|
|
12
|
-
- uses: actions/checkout@v3
|
|
13
|
-
- name: Setup node.js
|
|
14
|
-
uses: actions/setup-node@v3
|
|
15
|
-
with:
|
|
16
|
-
node-version: "18.x"
|
|
17
|
-
registry-url: "https://registry.npmjs.org"
|
|
18
|
-
|
|
19
|
-
- name: Publish to NPM
|
|
20
|
-
run: npm publish
|
|
21
|
-
env:
|
|
22
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
23
|
-
|
|
24
|
-
- name: Get package version
|
|
25
|
-
id: pkgver
|
|
26
|
-
run: echo "::set-output name=version::$(node -p "require('./package.json').version")"
|
|
27
|
-
|
|
28
|
-
# Creates a .tgz file in your working directory
|
|
29
|
-
- name: Package NPM tarball
|
|
30
|
-
run: npm pack
|
|
31
|
-
|
|
32
|
-
# - name: Debug GitHub event
|
|
33
|
-
# run: echo "${{ toJson(github.event.release) }}"
|
|
34
|
-
|
|
35
|
-
- name: Create Release
|
|
36
|
-
id: create_release
|
|
37
|
-
uses: actions/create-release@v1
|
|
38
|
-
env:
|
|
39
|
-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
40
|
-
with:
|
|
41
|
-
tag_name: ${{ steps.pkgver.outputs.version }}
|
|
42
|
-
release_name: Release ${{ steps.pkgver.outputs.version }}
|
|
43
|
-
draft: false
|
|
44
|
-
prerelease: false
|
|
45
|
-
|
|
46
|
-
- name: Upload Release Asset
|
|
47
|
-
id: upload-release-asset
|
|
48
|
-
uses: actions/upload-release-asset@v1
|
|
49
|
-
env:
|
|
50
|
-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
51
|
-
with:
|
|
52
|
-
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`.
|
|
53
|
-
asset_path: ./cron-converter-u2q-${{ steps.pkgver.outputs.version }}.tgz
|
|
54
|
-
asset_name: cron-converter-u2q-${{ steps.pkgver.outputs.version }}.tgz
|
|
55
|
-
asset_content_type: application/gzip
|