Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cron-converter

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cron-converter - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

5

dist/cjs/index.d.ts

@@ -1,2 +0,2 @@

import { Options } from "./types.js";
import { Options, ParseOptions } from "./types.js";
import { Schedule } from "./schedule.js";

@@ -9,5 +9,6 @@ export { Schedule } from "./schedule.js";

* @param str The string to parse
* @param options Parse options
* @return The cron schedule as an array
*/
export declare function stringToArray(str: string): number[][];
export declare function stringToArray(str: string, options?: Partial<ParseOptions>): number[][];
/**

@@ -14,0 +15,0 @@ * Parses a 2-dimentional array of integers and serializes it to a string

13

dist/cjs/index.js

@@ -10,7 +10,6 @@ "use strict";

Object.defineProperty(exports, "Schedule", { enumerable: true, get: function () { return schedule_js_2.Schedule; } });
const defaultOptions = {
outputHashes: false,
outputMonthNames: false,
outputWeekdayNames: false,
const defaultParseOptions = {
enableLastDayOfMonth: true,
};
const defaultOptions = Object.assign(Object.assign({}, defaultParseOptions), { outputHashes: false, outputMonthNames: false, outputWeekdayNames: false });
/**

@@ -20,5 +19,6 @@ * Parses a cron string

* @param str The string to parse
* @param options Parse options
* @return The cron schedule as an array
*/
function stringToArray(str) {
function stringToArray(str, options) {
if (typeof str !== "string") {

@@ -32,7 +32,6 @@ throw new Error("Invalid cron string");

else {
return parts.map((str, idx) => (0, part_js_1.stringToArrayPart)(str, units_js_1.units[idx]));
return parts.map((str, idx) => (0, part_js_1.stringToArrayPart)(str, units_js_1.units[idx], Object.assign(Object.assign({}, defaultParseOptions), options)));
}
}
exports.stringToArray = stringToArray;
;
/**

@@ -39,0 +38,0 @@ * Parses a 2-dimentional array of integers and serializes it to a string

@@ -1,2 +0,2 @@

import { Options, Unit } from "./types.js";
import { Options, ParseOptions, Unit } from "./types.js";
/**

@@ -16,4 +16,5 @@ * Converts an array of numbers to a part of a cron string

* @param unit The unit for the part
* @param options The parse options
* @return An array of numbers
*/
export declare const stringToArrayPart: (str: string, unit: Unit) => number[];
export declare const stringToArrayPart: (str: string, unit: Unit, options: ParseOptions) => number[];

@@ -15,3 +15,3 @@ "use strict";

const values = (0, util_js_1.sort)((0, util_js_1.dedup)(fixSunday(arr.map((value) => {
const parsedValue = (0, util_js_1.parseNumber)(value);
const parsedValue = (0, util_js_1.parseNumber)(unit, value, options);
if (parsedValue === undefined) {

@@ -25,3 +25,3 @@ throw getError(`Invalid value "${value}"`, unit);

}
assertInRange(values, unit);
assertInRange(values, unit, options);
return toString(values, unit, options);

@@ -35,5 +35,6 @@ };

* @param unit The unit for the part
* @param options The parse options
* @return An array of numbers
*/
const stringToArrayPart = (str, unit) => {
const stringToArrayPart = (str, unit, options) => {
const values = (0, util_js_1.sort)((0, util_js_1.dedup)(fixSunday((0, util_js_1.flatten)(replaceAlternatives(str, unit)

@@ -53,8 +54,8 @@ .split(",")

else {
parsedValues = parseRange(left, str, unit);
parsedValues = parseRange(left, str, unit, options);
}
const step = parseStep(right, unit);
const step = parseStep(right, unit, options);
return applyInterval(parsedValues, step);
})), unit)));
assertInRange(values, unit);
assertInRange(values, unit, options);
return values;

@@ -94,3 +95,3 @@ };

* @param unit The unit for the part
* @param options The formatting options to use
* @param options The options to use
* @return The part of a cron string

@@ -108,2 +109,5 @@ */

}
else if (unit.name === "day" && values.length === 1 && values[0] === -1) {
retval = "L";
}
else {

@@ -183,8 +187,9 @@ const step = getStep(values);

* @param unit The unit for the part
* @param options Parse options
* @return The resulting array
*/
const parseRange = (rangeString, context, unit) => {
const parseRange = (rangeString, context, unit, options) => {
const subparts = rangeString.split("-");
if (subparts.length === 1) {
const value = (0, util_js_1.parseNumber)(subparts[0]);
const value = (0, util_js_1.parseNumber)(unit, subparts[0], options);
if (value === undefined) {

@@ -196,4 +201,4 @@ throw getError(`Invalid value "${context}"`, unit);

else if (subparts.length === 2) {
const minValue = (0, util_js_1.parseNumber)(subparts[0]);
const maxValue = (0, util_js_1.parseNumber)(subparts[1]);
const minValue = (0, util_js_1.parseNumber)(unit, subparts[0], options);
const maxValue = (0, util_js_1.parseNumber)(unit, subparts[1], options);
if (minValue === undefined || maxValue === undefined) {

@@ -216,7 +221,8 @@ throw getError(`Invalid value "${context}"`, unit);

* @param unit The unit for the part
* @param options Parse options
* @return The step value
*/
const parseStep = (step, unit) => {
const parseStep = (step, unit, options) => {
if (step !== undefined) {
const parsedStep = (0, util_js_1.parseNumber)(step);
const parsedStep = (0, util_js_1.parseNumber)(unit, step, options);
if (parsedStep === undefined) {

@@ -282,6 +288,11 @@ throw getError(`Invalid interval step value "${step}"`, unit);

* @param unit The unit for the part
* @param options The parse options
*/
const assertInRange = (values, unit) => {
const first = values[0];
const last = values[values.length - 1];
const assertInRange = (values, unit, options) => {
let first = values[0];
let last = values[values.length - 1];
if (options.enableLastDayOfMonth && unit.name === "day" && first === -1 && last === -1) {
first = Math.abs(first);
last = Math.abs(last);
}
if (first < unit.min) {

@@ -288,0 +299,0 @@ throw getError(`Value "${first}" out of range`, unit);

@@ -144,3 +144,3 @@ "use strict";

const currentMonth = date.month;
while (arr[2].indexOf(date.day) === -1 ||
while ((arr[2].indexOf(date.day) === -1 && arr[2].indexOf(date.day - date.endOf('month').day - 1) === -1) ||
// luxon uses 1-7 for weekdays, but we use 0-6

@@ -147,0 +147,0 @@ arr[4].indexOf(date.weekday === 7 ? 0 : date.weekday) === -1) {

@@ -7,3 +7,6 @@ export declare type Unit = {

};
export declare type Options = {
export declare type ParseOptions = {
enableLastDayOfMonth: boolean;
};
export declare type Options = ParseOptions & {
outputHashes: boolean;

@@ -10,0 +13,0 @@ outputWeekdayNames: boolean;

@@ -0,1 +1,2 @@

import { ParseOptions, Unit } from "./types.js";
/**

@@ -6,5 +7,7 @@ * Parses a value as an integer or returns `undefined`

* @param value The integer `number` to parse
* @param unit The unit
* @param options Parse options
* @returns The parsed integer or `undefined`
*/
export declare const parseNumber: (value: unknown) => number | undefined;
export declare const parseNumber: (unit: Unit, value: unknown, options: ParseOptions) => number | undefined;
/**

@@ -11,0 +14,0 @@ * Assert that a value is a valid cron array

@@ -9,5 +9,10 @@ "use strict";

* @param value The integer `number` to parse
* @param unit The unit
* @param options Parse options
* @returns The parsed integer or `undefined`
*/
const parseNumber = (value) => {
const parseNumber = (unit, value, options) => {
if ((options === null || options === void 0 ? void 0 : options.enableLastDayOfMonth) === true && unit.name === 'day' && value === 'L') {
return -1;
}
if (typeof value === "string") {

@@ -14,0 +19,0 @@ const str = value.trim();

@@ -1,2 +0,2 @@

import { Options } from "./types.js";
import { Options, ParseOptions } from "./types.js";
import { Schedule } from "./schedule.js";

@@ -9,5 +9,6 @@ export { Schedule } from "./schedule.js";

* @param str The string to parse
* @param options Parse options
* @return The cron schedule as an array
*/
export declare function stringToArray(str: string): number[][];
export declare function stringToArray(str: string, options?: Partial<ParseOptions>): number[][];
/**

@@ -14,0 +15,0 @@ * Parses a 2-dimentional array of integers and serializes it to a string

@@ -6,7 +6,6 @@ import { arrayToStringPart, stringToArrayPart } from "./part.js";

export { Schedule } from "./schedule.js";
const defaultOptions = {
outputHashes: false,
outputMonthNames: false,
outputWeekdayNames: false,
const defaultParseOptions = {
enableLastDayOfMonth: true,
};
const defaultOptions = Object.assign(Object.assign({}, defaultParseOptions), { outputHashes: false, outputMonthNames: false, outputWeekdayNames: false });
/**

@@ -16,5 +15,6 @@ * Parses a cron string

* @param str The string to parse
* @param options Parse options
* @return The cron schedule as an array
*/
export function stringToArray(str) {
export function stringToArray(str, options) {
if (typeof str !== "string") {

@@ -28,6 +28,5 @@ throw new Error("Invalid cron string");

else {
return parts.map((str, idx) => stringToArrayPart(str, units[idx]));
return parts.map((str, idx) => stringToArrayPart(str, units[idx], Object.assign(Object.assign({}, defaultParseOptions), options)));
}
}
;
/**

@@ -34,0 +33,0 @@ * Parses a 2-dimentional array of integers and serializes it to a string

@@ -1,2 +0,2 @@

import { Options, Unit } from "./types.js";
import { Options, ParseOptions, Unit } from "./types.js";
/**

@@ -16,4 +16,5 @@ * Converts an array of numbers to a part of a cron string

* @param unit The unit for the part
* @param options The parse options
* @return An array of numbers
*/
export declare const stringToArrayPart: (str: string, unit: Unit) => number[];
export declare const stringToArrayPart: (str: string, unit: Unit, options: ParseOptions) => number[];

@@ -12,3 +12,3 @@ import { dedup, flatten, parseNumber, range, sort } from "./util.js";

const values = sort(dedup(fixSunday(arr.map((value) => {
const parsedValue = parseNumber(value);
const parsedValue = parseNumber(unit, value, options);
if (parsedValue === undefined) {

@@ -22,3 +22,3 @@ throw getError(`Invalid value "${value}"`, unit);

}
assertInRange(values, unit);
assertInRange(values, unit, options);
return toString(values, unit, options);

@@ -31,5 +31,6 @@ };

* @param unit The unit for the part
* @param options The parse options
* @return An array of numbers
*/
export const stringToArrayPart = (str, unit) => {
export const stringToArrayPart = (str, unit, options) => {
const values = sort(dedup(fixSunday(flatten(replaceAlternatives(str, unit)

@@ -49,8 +50,8 @@ .split(",")

else {
parsedValues = parseRange(left, str, unit);
parsedValues = parseRange(left, str, unit, options);
}
const step = parseStep(right, unit);
const step = parseStep(right, unit, options);
return applyInterval(parsedValues, step);
})), unit)));
assertInRange(values, unit);
assertInRange(values, unit, options);
return values;

@@ -89,3 +90,3 @@ };

* @param unit The unit for the part
* @param options The formatting options to use
* @param options The options to use
* @return The part of a cron string

@@ -103,2 +104,5 @@ */

}
else if (unit.name === "day" && values.length === 1 && values[0] === -1) {
retval = "L";
}
else {

@@ -178,8 +182,9 @@ const step = getStep(values);

* @param unit The unit for the part
* @param options Parse options
* @return The resulting array
*/
const parseRange = (rangeString, context, unit) => {
const parseRange = (rangeString, context, unit, options) => {
const subparts = rangeString.split("-");
if (subparts.length === 1) {
const value = parseNumber(subparts[0]);
const value = parseNumber(unit, subparts[0], options);
if (value === undefined) {

@@ -191,4 +196,4 @@ throw getError(`Invalid value "${context}"`, unit);

else if (subparts.length === 2) {
const minValue = parseNumber(subparts[0]);
const maxValue = parseNumber(subparts[1]);
const minValue = parseNumber(unit, subparts[0], options);
const maxValue = parseNumber(unit, subparts[1], options);
if (minValue === undefined || maxValue === undefined) {

@@ -211,7 +216,8 @@ throw getError(`Invalid value "${context}"`, unit);

* @param unit The unit for the part
* @param options Parse options
* @return The step value
*/
const parseStep = (step, unit) => {
const parseStep = (step, unit, options) => {
if (step !== undefined) {
const parsedStep = parseNumber(step);
const parsedStep = parseNumber(unit, step, options);
if (parsedStep === undefined) {

@@ -277,6 +283,11 @@ throw getError(`Invalid interval step value "${step}"`, unit);

* @param unit The unit for the part
* @param options The parse options
*/
const assertInRange = (values, unit) => {
const first = values[0];
const last = values[values.length - 1];
const assertInRange = (values, unit, options) => {
let first = values[0];
let last = values[values.length - 1];
if (options.enableLastDayOfMonth && unit.name === "day" && first === -1 && last === -1) {
first = Math.abs(first);
last = Math.abs(last);
}
if (first < unit.min) {

@@ -283,0 +294,0 @@ throw getError(`Value "${first}" out of range`, unit);

@@ -140,3 +140,3 @@ import { assertValidArray } from "./util.js";

const currentMonth = date.month;
while (arr[2].indexOf(date.day) === -1 ||
while ((arr[2].indexOf(date.day) === -1 && arr[2].indexOf(date.day - date.endOf('month').day - 1) === -1) ||
// luxon uses 1-7 for weekdays, but we use 0-6

@@ -143,0 +143,0 @@ arr[4].indexOf(date.weekday === 7 ? 0 : date.weekday) === -1) {

@@ -7,3 +7,6 @@ export declare type Unit = {

};
export declare type Options = {
export declare type ParseOptions = {
enableLastDayOfMonth: boolean;
};
export declare type Options = ParseOptions & {
outputHashes: boolean;

@@ -10,0 +13,0 @@ outputWeekdayNames: boolean;

@@ -0,1 +1,2 @@

import { ParseOptions, Unit } from "./types.js";
/**

@@ -6,5 +7,7 @@ * Parses a value as an integer or returns `undefined`

* @param value The integer `number` to parse
* @param unit The unit
* @param options Parse options
* @returns The parsed integer or `undefined`
*/
export declare const parseNumber: (value: unknown) => number | undefined;
export declare const parseNumber: (unit: Unit, value: unknown, options: ParseOptions) => number | undefined;
/**

@@ -11,0 +14,0 @@ * Assert that a value is a valid cron array

@@ -6,5 +6,10 @@ /**

* @param value The integer `number` to parse
* @param unit The unit
* @param options Parse options
* @returns The parsed integer or `undefined`
*/
export const parseNumber = (value) => {
export const parseNumber = (unit, value, options) => {
if ((options === null || options === void 0 ? void 0 : options.enableLastDayOfMonth) === true && unit.name === 'day' && value === 'L') {
return -1;
}
if (typeof value === "string") {

@@ -11,0 +16,0 @@ const str = value.trim();

{
"name": "cron-converter",
"version": "2.0.1",
"version": "2.1.0",
"description": "Cron string converter",

@@ -5,0 +5,0 @@ "type": "module",

@@ -25,4 +25,6 @@ # cron-converter

Versions `2.x.x` of `cron-converter` are not backwards compatible with versions `1.x.x`.
Version `2.1.0` introduces support for the non standard `L` character which represents the last day of the month. This feature is on by default and can be turned off by setting the `enableLastDayOfMonth` option to `false`.
Versions `2.x.x` are not backwards compatible with versions `1.x.x`.
| | `2.x.x` | `1.x.x` |

@@ -70,3 +72,3 @@ | ---- | ------------- | ------------- |

## Formatting options
## Options

@@ -108,2 +110,20 @@ ### outputMonthNames

### enableLastDayOfMonth
Default: `true`
```ts
const arr = [[1], [1], [1], [-1], [1]];
// Prints: '1 1 1 L 1'
console.log(arrayToString(arr, { enableLastDayOfMonth: true }));
```
```ts
const str = '1 1 1 L 1';
// Prints: [[1], [1], [1], [-1], [1]]
console.log(stringToArray(str, { enableLastDayOfMonth: true }));
```
## Get the schedule execution times

@@ -110,0 +130,0 @@

import { arrayToStringPart, stringToArrayPart } from "./part.js";
import { assertValidArray } from "./util.js";
import { Options } from "./types.js";
import { Options, ParseOptions } from "./types.js";
import { Schedule } from "./schedule.js";

@@ -9,3 +9,8 @@ import { units } from "./units.js";

const defaultParseOptions: ParseOptions = {
enableLastDayOfMonth: true,
};
const defaultOptions: Options = {
...defaultParseOptions,
outputHashes: false,

@@ -20,5 +25,6 @@ outputMonthNames: false,

* @param str The string to parse
* @param options Parse options
* @return The cron schedule as an array
*/
export function stringToArray(str: string) {
export function stringToArray(str: string, options?: Partial<ParseOptions>) {
if (typeof str !== "string") {

@@ -31,5 +37,7 @@ throw new Error("Invalid cron string");

} else {
return parts.map((str, idx) => stringToArrayPart(str, units[idx]));
return parts.map((str, idx) =>
stringToArrayPart(str, units[idx], { ...defaultParseOptions, ...options })
);
}
};
}

@@ -36,0 +44,0 @@ /**

@@ -1,2 +0,2 @@

import { Options, Unit } from "./types.js";
import { Options, ParseOptions, Unit } from "./types.js";
import { dedup, flatten, parseNumber, range, sort } from "./util.js";

@@ -21,3 +21,3 @@

arr.map((value) => {
const parsedValue = parseNumber(value);
const parsedValue = parseNumber(unit, value, options);
if (parsedValue === undefined) {

@@ -35,3 +35,3 @@ throw getError(`Invalid value "${value}"`, unit);

}
assertInRange(values, unit);
assertInRange(values, unit, options);
return toString(values, unit, options);

@@ -45,5 +45,10 @@ };

* @param unit The unit for the part
* @param options The parse options
* @return An array of numbers
*/
export const stringToArrayPart = (str: string, unit: Unit) => {
export const stringToArrayPart = (
str: string,
unit: Unit,
options: ParseOptions
) => {
const values = sort(

@@ -66,5 +71,5 @@ dedup(

} else {
parsedValues = parseRange(left, str, unit);
parsedValues = parseRange(left, str, unit, options);
}
const step = parseStep(right, unit);
const step = parseStep(right, unit, options);
return applyInterval(parsedValues, step);

@@ -77,3 +82,3 @@ })

);
assertInRange(values, unit);
assertInRange(values, unit, options);
return values;

@@ -112,3 +117,3 @@ };

* @param unit The unit for the part
* @param options The formatting options to use
* @param options The options to use
* @return The part of a cron string

@@ -124,2 +129,4 @@ */

}
} else if (unit.name === "day" && values.length === 1 && values[0] === -1) {
retval = "L";
} else {

@@ -203,8 +210,14 @@ const step = getStep(values);

* @param unit The unit for the part
* @param options Parse options
* @return The resulting array
*/
const parseRange = (rangeString: string, context: string, unit: Unit) => {
const parseRange = (
rangeString: string,
context: string,
unit: Unit,
options: ParseOptions
) => {
const subparts = rangeString.split("-");
if (subparts.length === 1) {
const value = parseNumber(subparts[0]);
const value = parseNumber(unit, subparts[0], options);
if (value === undefined) {

@@ -215,4 +228,4 @@ throw getError(`Invalid value "${context}"`, unit);

} else if (subparts.length === 2) {
const minValue = parseNumber(subparts[0]);
const maxValue = parseNumber(subparts[1]);
const minValue = parseNumber(unit, subparts[0], options);
const maxValue = parseNumber(unit, subparts[1], options);
if (minValue === undefined || maxValue === undefined) {

@@ -238,7 +251,8 @@ throw getError(`Invalid value "${context}"`, unit);

* @param unit The unit for the part
* @param options Parse options
* @return The step value
*/
const parseStep = (step: string, unit: Unit) => {
const parseStep = (step: string, unit: Unit, options: ParseOptions) => {
if (step !== undefined) {
const parsedStep = parseNumber(step);
const parsedStep = parseNumber(unit, step, options);
if (parsedStep === undefined) {

@@ -310,6 +324,11 @@ throw getError(`Invalid interval step value "${step}"`, unit);

* @param unit The unit for the part
* @param options The parse options
*/
const assertInRange = (values: number[], unit: Unit) => {
const first = values[0];
const last = values[values.length - 1];
const assertInRange = (values: number[], unit: Unit, options: ParseOptions) => {
let first = values[0];
let last = values[values.length - 1];
if (options.enableLastDayOfMonth && unit.name === "day" && first === -1 && last === -1) {
first = Math.abs(first);
last = Math.abs(last);
}
if (first < unit.min) {

@@ -316,0 +335,0 @@ throw getError(`Value "${first}" out of range`, unit);

@@ -160,3 +160,3 @@ import { assertValidArray } from "./util.js";

while (
arr[2].indexOf(date.day) === -1 ||
(arr[2].indexOf(date.day) === -1 && arr[2].indexOf(date.day - date.endOf('month').day - 1) === -1) ||
// luxon uses 1-7 for weekdays, but we use 0-6

@@ -163,0 +163,0 @@ arr[4].indexOf(date.weekday === 7 ? 0 : date.weekday) === -1

@@ -6,8 +6,12 @@ export type Unit = {

alt?: ReadonlyArray<string>;
}
};
export type Options = {
export type ParseOptions = {
enableLastDayOfMonth: boolean;
};
export type Options = ParseOptions & {
outputHashes: boolean;
outputWeekdayNames: boolean;
outputMonthNames: boolean;
}
};

@@ -0,1 +1,3 @@

import { ParseOptions, Unit } from "./types.js";
/**

@@ -6,5 +8,11 @@ * Parses a value as an integer or returns `undefined`

* @param value The integer `number` to parse
* @param unit The unit
* @param options Parse options
* @returns The parsed integer or `undefined`
*/
export const parseNumber = (value: unknown) => {
export const parseNumber = (unit: Unit, value: unknown, options: ParseOptions) => {
if (options?.enableLastDayOfMonth === true && unit.name === 'day' && value === 'L') {
return -1;
}
if (typeof value === "string") {

@@ -11,0 +19,0 @@ const str: string = value.trim();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc