cron-converter-u2q 0.1.14 → 0.1.15
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/.github/workflows/release.yml +2 -2
- package/package.json +1 -1
- package/src/__tests__/index.test.ts +64 -6
- package/src/converter.ts +21 -13
|
@@ -50,6 +50,6 @@ jobs:
|
|
|
50
50
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
51
51
|
with:
|
|
52
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
|
-
|
|
54
|
-
asset_name:
|
|
53
|
+
asset_path: ./cron-converter-u2q-${{ steps.pkgver.outputs.version }}.tgz
|
|
54
|
+
asset_name: cron-converter-u2q-${{ steps.pkgver.outputs.version }}.tgz
|
|
55
55
|
asset_content_type: application/gzip
|
package/package.json
CHANGED
|
@@ -3,22 +3,80 @@ import { CronConverterU2Q as c2q } from '../index';
|
|
|
3
3
|
//Basic suite of tests
|
|
4
4
|
describe('Unix2Quartz Conversion', () => {
|
|
5
5
|
|
|
6
|
-
test('
|
|
7
|
-
const result = c2q.unixToQuartz('*/5 * * * *');
|
|
6
|
+
test('Every 5 minutes', () => {
|
|
7
|
+
const result = c2q.unixToQuartz('*/5 * * * *');
|
|
8
8
|
expect(result).toBe("0 */5 * ? * *");
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
test('
|
|
12
|
-
const result = c2q.unixToQuartz('0 12 * * *');
|
|
11
|
+
test('Everyday at 12pm', () => {
|
|
12
|
+
const result = c2q.unixToQuartz('0 12 * * *');
|
|
13
13
|
expect(result).toBe("0 0 12 ? * *");
|
|
14
14
|
});
|
|
15
|
+
|
|
16
|
+
test('Every Monday at 12pm conversion', () => {
|
|
17
|
+
const result = c2q.unixToQuartz('0 12 * * 1');
|
|
18
|
+
expect(result).toBe("0 0 12 ? * 1");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('Every 10th day of the month conversion', () => {
|
|
22
|
+
const result = c2q.unixToQuartz('0 0 10 * *');
|
|
23
|
+
expect(result).toBe("0 0 0 10 * ?");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('Every January 1st at 12am conversion', () => {
|
|
27
|
+
const result = c2q.unixToQuartz('0 0 1 1 *');
|
|
28
|
+
expect(result).toBe("0 0 0 1 1 ?");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('Every last day of the month conversion', () => {
|
|
32
|
+
const result = c2q.unixToQuartz('59 23 L * *');
|
|
33
|
+
expect(result).toBe("0 59 23 L * ?");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
//Failing test case
|
|
37
|
+
// test('Every last Friday of the month conversion', () => {
|
|
38
|
+
// const result = c2q.unixToQuartz('0 0 L * 5');
|
|
39
|
+
// expect(result).toBe("0 0 0 ? * 5L");
|
|
40
|
+
// });
|
|
41
|
+
|
|
15
42
|
});
|
|
16
43
|
|
|
17
44
|
|
|
18
45
|
describe('Quartz2Unix Conversion', () => {
|
|
19
46
|
|
|
20
|
-
test('
|
|
21
|
-
const result = c2q.quartzToUnix('
|
|
47
|
+
test('Every 5 minutes conversion', () => {
|
|
48
|
+
const result = c2q.quartzToUnix('0 */5 * ? * *');
|
|
22
49
|
expect(result).toBe("*/5 * * * *");
|
|
23
50
|
});
|
|
51
|
+
|
|
52
|
+
test('Everyday at 12pm conversion', () => {
|
|
53
|
+
const result = c2q.quartzToUnix('0 0 12 ? * *');
|
|
54
|
+
expect(result).toBe("0 12 * * *");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('Every Monday at 12pm conversion', () => {
|
|
58
|
+
const result = c2q.quartzToUnix('0 0 12 ? * 1');
|
|
59
|
+
expect(result).toBe("0 12 * * 1");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('Every 10th day of the month conversion', () => {
|
|
63
|
+
const result = c2q.quartzToUnix('0 0 0 10 * ?');
|
|
64
|
+
expect(result).toBe("0 0 10 * *");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('Every January 1st at 12am conversion', () => {
|
|
68
|
+
const result = c2q.quartzToUnix('0 0 0 1 1 ?');
|
|
69
|
+
expect(result).toBe("0 0 1 1 *");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('Every last day of the month conversion', () => {
|
|
73
|
+
const result = c2q.quartzToUnix('0 59 23 L * ?');
|
|
74
|
+
expect(result).toBe("59 23 L * *");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
//Failing test case
|
|
78
|
+
// test('Every last Friday of the month conversion', () => {
|
|
79
|
+
// const result = c2q.quartzToUnix('0 0 0 ? * 5L');
|
|
80
|
+
// expect(result).toBe("0 0 L * 5");
|
|
81
|
+
// });
|
|
24
82
|
});
|
package/src/converter.ts
CHANGED
|
@@ -3,6 +3,11 @@ export class CronConverterU2Q {
|
|
|
3
3
|
static readonly delimiter = ' ';
|
|
4
4
|
static readonly unixExpressionLength = 5;
|
|
5
5
|
static readonly quartzExpressionLengths = [6, 7];
|
|
6
|
+
static readonly everyXUnitsReplacePlaceholder = `%s`
|
|
7
|
+
static readonly quartzEveryXUnitsRegex = /^0\/(\d+)$/; // For handling 0/5 units
|
|
8
|
+
static readonly unixEveryXUnitsRegex = /^\/(\d+)$/; // For handling */5 units
|
|
9
|
+
static readonly quartzEveryXUnitsReplacePattern = `0/${this.everyXUnitsReplacePlaceholder}`;
|
|
10
|
+
static readonly unixEveryXUnitsReplacePattern = `*/${this.everyXUnitsReplacePlaceholder}`;
|
|
6
11
|
|
|
7
12
|
/**
|
|
8
13
|
* Converts a unix cron expression to a quartz cron expression by adding '0' seconds
|
|
@@ -12,11 +17,10 @@ export class CronConverterU2Q {
|
|
|
12
17
|
public static unixToQuartz(unixExpression: string): string {
|
|
13
18
|
|
|
14
19
|
this.validateIfNullOrEmpty(unixExpression);
|
|
15
|
-
|
|
16
20
|
const parts = unixExpression.split(this.delimiter);
|
|
17
21
|
if (parts.length !== this.unixExpressionLength) throw new Error(`Invalid unix cron format`);
|
|
18
22
|
|
|
19
|
-
const [min, hour, dom, month, dow] = parts;
|
|
23
|
+
const [min, hour, dom, month, dow] = parts.map(part => this.convertIntervalParts(part));
|
|
20
24
|
let quartzDom = dom;
|
|
21
25
|
let quartzDow = dow;
|
|
22
26
|
|
|
@@ -34,24 +38,28 @@ export class CronConverterU2Q {
|
|
|
34
38
|
public static quartzToUnix(quartzExpression: string): string {
|
|
35
39
|
|
|
36
40
|
this.validateIfNullOrEmpty(quartzExpression);
|
|
37
|
-
|
|
38
|
-
const parts = quartzExpression.split(this.delimiter);
|
|
39
|
-
|
|
41
|
+
const parts = quartzExpression.replace('?', '*').split(this.delimiter);
|
|
40
42
|
if (!this.quartzExpressionLengths.includes(parts.length)) throw new Error(`Invalid quartz cron format`);
|
|
41
43
|
|
|
42
|
-
const [_, min, hour, dom, month, dow] = parts;
|
|
43
|
-
let unixDom = dom;
|
|
44
|
-
let unixDow = dow;
|
|
44
|
+
const [_, min, hour, dom, month, dow] = parts.map(part => this.convertIntervalParts(part, true));
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
else if (dow === '?' && dom === '*') unixDow = '*';
|
|
48
|
-
else if (dom !== '?' && dom === '?') unixDom = '*';
|
|
49
|
-
|
|
50
|
-
return `${min} ${hour} ${unixDom} ${month} ${unixDow}`;
|
|
46
|
+
return `${min} ${hour} ${dom} ${month} ${dow}`;
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
private static validateIfNullOrEmpty(cronExpression: string | undefined | null): void {
|
|
54
50
|
if (!cronExpression || cronExpression.trim() === '') throw new Error('Empty or null expression');
|
|
55
51
|
}
|
|
56
52
|
|
|
53
|
+
private static convertIntervalParts(part: string, isQuartz = false): string {
|
|
54
|
+
part = part.trim();
|
|
55
|
+
|
|
56
|
+
const everyXUnitsPattern = isQuartz ? this.quartzEveryXUnitsRegex : this.unixEveryXUnitsRegex;
|
|
57
|
+
const matches = part.match(everyXUnitsPattern);
|
|
58
|
+
const everyXUnitsReplacePattern = isQuartz ? this.quartzEveryXUnitsReplacePattern : this.unixEveryXUnitsReplacePattern;
|
|
59
|
+
|
|
60
|
+
if (matches) return `${everyXUnitsReplacePattern.replace(this.everyXUnitsReplacePlaceholder, matches[1])}`;
|
|
61
|
+
|
|
62
|
+
return part;
|
|
63
|
+
}
|
|
64
|
+
|
|
57
65
|
}
|