cron-converter-u2q 0.1.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rahul
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # cron-converter-u2q
2
+ Easily convert cron expressions between Unix and Quartz formats with the `cron-converter-u2q` package
3
+
4
+
5
+
6
+ ### Features
7
+
8
+ :arrows_counterclockwise: Two-way conversion: from Unix to Quartz and Quartz to Unix.
9
+
10
+ :zap: Lightweight.
11
+
12
+ ### Installation
13
+
14
+ Using npm:
15
+
16
+ ```bash
17
+ npm install cron-converter
18
+ ```
19
+
20
+ Using yarn:
21
+
22
+ ```bash
23
+ yarn add cron-converter
24
+ ```
25
+
26
+ ### Usage
27
+ Firstly, import the CronConverterU2Q module:
28
+ ```javascript
29
+ import { CronConverterU2Q as c2q } from 'cron-converter';
30
+ ```
31
+
32
+ If you're using ES6 Modules
33
+ ```javascript
34
+ import { CronConverterU2Q as c2q } from 'cron-converter';
35
+ ```
36
+ Convert from Unix to Quartz
37
+ ```javascript
38
+ const quartzExpression = c2q.unixToQuartz('5 * * * *');
39
+ ```
40
+ Convert from Quartz to Unix
41
+ ```javascript
42
+ const unixExpression = c2q.quartzToUnix('* */5 * ? * * *');
43
+ ```
44
+
45
+ ### Development Notice
46
+ This package is still under active development. Some methods and features might not be stable yet. We're working diligently to improve and stabilize the package. Any feedback, suggestions, or contributions are highly appreciated!
47
+
48
+ ## License
49
+
50
+ This project is licensed under the [MIT License](https://opensource.org/license/mit/)
51
+
package/jest.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ roots: ['./src/'],
5
+ transform: {
6
+ '^.+\\.tsx?$': 'ts-jest'
7
+ },
8
+ testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
9
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
10
+ };
package/lib/index.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CronConverterU2Q = void 0;
4
+ class CronConverterU2Q {
5
+ constructor() {
6
+ this.delimiter = ' ';
7
+ this.unixExpressionLength = 5;
8
+ this.quartzExpressionLengths = [6, 7];
9
+ }
10
+ /**
11
+ * Converts a unix cron expression to a quartz cron expression by adding '0' seconds
12
+ * @param unixExpression - the unix expression
13
+ * @returns the corresponding quartz expression
14
+ */
15
+ unixToQuartz(unixExpression) {
16
+ this.validateIfNullOrEmpty(unixExpression);
17
+ const parts = unixExpression.split(this.delimiter);
18
+ if (parts.length !== this.unixExpressionLength)
19
+ throw new Error(`Invalid unix cron format`);
20
+ const [min, hour, dom, month, dow] = parts;
21
+ let quartzDom = dom;
22
+ let quartzDow = dow;
23
+ if (dom === '*' && (dow === '*' || dow !== '*'))
24
+ quartzDom = '?';
25
+ else if (dom !== '*' && dow === '*')
26
+ quartzDow = '?';
27
+ return `0 ${min} ${hour} ${quartzDom} ${month} ${quartzDow}`;
28
+ }
29
+ /**
30
+ * Converts a quartz cron expression to a unix cron expression
31
+ * @param quartzExpression - the quartz expression
32
+ * @returns the corresponding unix expression
33
+ */
34
+ quartzToUnix(quartzExpression) {
35
+ this.validateIfNullOrEmpty(quartzExpression);
36
+ const parts = quartzExpression.split(this.delimiter);
37
+ if (!this.quartzExpressionLengths.includes(parts.length))
38
+ throw new Error(`Invalid quartz cron format`);
39
+ const [_, min, hour, dom, month, dow] = parts;
40
+ let unixDom = dom;
41
+ let unixDow = dow;
42
+ if (dom === '?' && dow === '*')
43
+ unixDom = '*';
44
+ else if (dow === '?' && dom === '*')
45
+ unixDow = '*';
46
+ else if (dom !== '?' && dom === '?')
47
+ unixDom = '*';
48
+ return `${min} ${hour} ${unixDom} ${month} ${unixDow}`;
49
+ }
50
+ validateIfNullOrEmpty(cronExpression) {
51
+ if (!cronExpression || cronExpression.trim() === '')
52
+ throw new Error('Empty or null expression');
53
+ }
54
+ }
55
+ exports.CronConverterU2Q = CronConverterU2Q;
56
+ // if (process.env.NODE_ENV === 'development') {
57
+ // }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "cron-converter-u2q",
3
+ "version": "0.1.3",
4
+ "description": "Converts cron expressions between unix and quartz formats",
5
+ "main": "lib/index.js",
6
+ "types": "types/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "jest --no-watchman --coverage"
10
+ },
11
+ "author": "BitBundler <rahu619@gmail.com>",
12
+ "keywords": [
13
+ "cron",
14
+ "unix",
15
+ "quartz",
16
+ "cron-expression",
17
+ "cron-convert"
18
+ ],
19
+ "license": "MIT",
20
+ "devDependencies": {
21
+ "@types/jest": "^29.5.3",
22
+ "@types/mocha": "^10.0.1",
23
+ "@types/node": "^20.4.9",
24
+ "jest": "^29.6.2",
25
+ "ts-jest": "^29.1.1",
26
+ "ts-node": "^10.9.1",
27
+ "typescript": "^5.1.6"
28
+ }
29
+ }
@@ -0,0 +1,24 @@
1
+ import { CronConverterU2Q as c2q } from '../index';
2
+
3
+ //Basic suite of tests
4
+ describe('Unix2Quartz Conversion', () => {
5
+
6
+ test('unixToQuartz conversion', () => {
7
+ const result = c2q.unixToQuartz('*/5 * * * *'); //Every 5 minutes
8
+ expect(result).toBe("0 */5 * ? * *");
9
+ });
10
+
11
+ test('unixToQuartz conversion', () => {
12
+ const result = c2q.unixToQuartz('0 12 * * *'); //Everyday at 12pm
13
+ expect(result).toBe("0 0 12 ? * *");
14
+ });
15
+ });
16
+
17
+
18
+ describe('Quartz2Unix Conversion', () => {
19
+
20
+ test('quartzToUnix conversion', () => {
21
+ const result = c2q.quartzToUnix('* */5 * ? * * *'); //Every 5 minutes
22
+ expect(result).toBe("*/5 * * * *");
23
+ });
24
+ });
package/src/index.ts ADDED
@@ -0,0 +1,64 @@
1
+ export class CronConverterU2Q {
2
+
3
+ static readonly delimiter = ' ';
4
+ static readonly unixExpressionLength = 5;
5
+ static readonly quartzExpressionLengths = [6, 7];
6
+
7
+ /**
8
+ * Converts a unix cron expression to a quartz cron expression by adding '0' seconds
9
+ * @param unixExpression - the unix expression
10
+ * @returns the corresponding quartz expression
11
+ */
12
+ public static unixToQuartz(unixExpression: string): string {
13
+
14
+ this.validateIfNullOrEmpty(unixExpression);
15
+
16
+ const parts = unixExpression.split(this.delimiter);
17
+ if (parts.length !== this.unixExpressionLength) throw new Error(`Invalid unix cron format`);
18
+
19
+ const [min, hour, dom, month, dow] = parts;
20
+ let quartzDom = dom;
21
+ let quartzDow = dow;
22
+
23
+ if (dom === '*' && (dow === '*' || dow !== '*')) quartzDom = '?';
24
+ else if (dom !== '*' && dow === '*') quartzDow = '?';
25
+
26
+ return `0 ${min} ${hour} ${quartzDom} ${month} ${quartzDow}`;
27
+ }
28
+
29
+ /**
30
+ * Converts a quartz cron expression to a unix cron expression
31
+ * @param quartzExpression - the quartz expression
32
+ * @returns the corresponding unix expression
33
+ */
34
+ public static quartzToUnix(quartzExpression: string): string {
35
+
36
+ this.validateIfNullOrEmpty(quartzExpression);
37
+
38
+ const parts = quartzExpression.split(this.delimiter);
39
+
40
+ if (!this.quartzExpressionLengths.includes(parts.length)) throw new Error(`Invalid quartz cron format`);
41
+
42
+ const [_, min, hour, dom, month, dow] = parts;
43
+ let unixDom = dom;
44
+ let unixDow = dow;
45
+
46
+ if (dom === '?' && dow === '*') unixDom = '*';
47
+ else if (dow === '?' && dom === '*') unixDow = '*';
48
+ else if (dom !== '?' && dom === '?') unixDom = '*';
49
+
50
+ return `${min} ${hour} ${unixDom} ${month} ${unixDow}`;
51
+ }
52
+
53
+ private static validateIfNullOrEmpty(cronExpression: string | undefined | null): void {
54
+ if (!cronExpression || cronExpression.trim() === '') throw new Error('Empty or null expression');
55
+ }
56
+
57
+ }
58
+
59
+ export const CronConverterU2QModule = CronConverterU2Q;
60
+
61
+
62
+ // if (process.env.NODE_ENV === 'development') {
63
+ // }
64
+
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Language and Environment */
4
+ "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
5
+ "module": "commonjs" /* Specify what module code is generated. */,
6
+ "outDir": "./lib" /* Specify an output folder for all emitted files. */,
7
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
8
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
9
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
10
+
11
+ /* Type Checking */
12
+ "strict": true /* Enable all strict type-checking options. */,
13
+ /* Completeness */
14
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
15
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
16
+ },
17
+ "include": ["src/**/*.ts"],
18
+ "exclude": ["node_modules", "**/__tests__/**"]
19
+ }
@@ -0,0 +1,6 @@
1
+ declare module 'cron-converter-u2q' {
2
+ export const CronConverterU2QModule: {
3
+ unixToQuartz(unixExpression: string): string;
4
+ quartzToUnix(quartzExpression: string): string;
5
+ }
6
+ }