0.1.3 • Published 3 years ago

cronix v0.1.3

Weekly downloads
190
License
MIT
Repository
github
Last release
3 years ago

npm codecov

Cronix

A cron parser/generator with support for multiple cron dialect such as Quartz or Jenkins.

Installation

Add Cronix as a dependency using yarn

yarn add cronix

Or npm

npm install cronix

Usage

To parse an expression simply call the cronix function. By default expression are parsed according to the standard crontab format. You can specify the mode as a second parameter to change that behaviour.

import { cronix, CronixMode } from "cronix";
// Every day at 00:00
// const expression = "0 0 * * *";
const expression = {
  minute: "0",
  hour: "0"
};

const parsedCron = cronix(expression);
const cronAst = parsedCron.ast;
// Get the computed expression using the value() method
// should be equal to expression
console.log(cronAst.value());
// should be an empty array
console.log(parsedCron.errors);

expression.year = "*/2";

// Quartz supports a year field that is not available in standard cron
const parsedQuartz = cronix(expression, CronixMode.QUARTZ);
console.log(parsedQuartz.ast.value());

Alternatively you can use the class parser. Choose the implementation relevant to the desired dialect.

import { CronParser } from "cronix";

// Every day at 00:00
const expression = "0 0 * * *";
const parser = new CronParser();

const ast = parser.parse(expression);
// should be equal to expression
console.log(ast.value());

API

CronixExpression

An object representing an input expression. Can be used when you only need to fill some fields and want to leave the rest to their default value *.

fieldtypeoptionaldescription
minutestringyesThe minute field
hourstringyesThe hour field
dayOfMonthstringyesThe dayOfMonth field
monthstringyesThe month field
dayOfWeekstringyesThe dayOfWeek field
yearstringyesThe year field. Quartz only
secondstringyesThe second field. Quartz only

cronix

Parse an expression into the corresponding ast. The output can be a subclass of CronExpression depending on the selected mode. If parsing fails a CronixParserException is thrown.

function cronix(expression: string|CronixExpression, mode: CronixMode): {ast: CronExpression|null, errors: ParserException[]}
Parametertypeoptionaldescription
expressionstring or CronixExpressionThe expression to parse
modeCronixModeYes (defaults to Crontab mode)The parser mode. Available options are CRONTAB, JENKINS, or QUARTZ
Return fieldtypeoptionaldescription
astCronExpressionThe parsed expression. Null if the input cannot be parsed
errorsarray of ParserExceptionThe errors encountered by the parser

CronixParser

Class holding a parsing context to reuse in repeated parsing. The base CronixParser class is abstract and should be subclassed to handle a specific dialect.

Available implementations

Dialecttypeconstructor
crontabCronParsernew CronParser()
quartzQuartzParsernew QuartzParser()
jenkinsJenkinsParsernew JenkinsParser()

parse

Parse a cron expression according to the parser's context. Returns an ast corresponding to the parsed expression. If parsing fails one or more ParserException will be added to the error list and the return value will be null.

function CronixParser.parse(expression: string|CronixExpression): CronExpression
Parametertypeoptionaldescription
expressionstring or CronixExpressionThe expression to parse

errors

A getters that returns a list of errors encountered during. Useful for figuring out where the parsing fail. It is reset with each call of parse method.

ParserException

An exception indicating an error occured during a parse or parseField call. It wraps the caught expression with an indication of the step at which it failed.

fieldtypeoptionaldescription
innerExceptionError or ILexingErrorThe root cause
steplexing or parsing or semanticThe step at which parsing failed

Other fields are inherited from the Error interface definition

Output AST

Below are described the different classes that composes the output AST. At the top is a single CronExpression instance, composed of several Field (minute, hour, day of month, ...) that can contain a list of either literals (singular expression) or dual expression (range or step). Dialects can have their own special expression.

SyntaxNode

The base interface for all nodes in the AST.

methodsignaturedescription
valuevalue(): stringThe generated value for this node. Recursively called for each child node.

CronExpression

The root element of the AST. Can be subclassed by dialects wish to have a different signature (Quartz having the second field first).

methodsignaturedescription
constructorCronExpression(minute?: SyntaxNode, hour?: SyntaxNode, dom?: SyntaxNode, month?: SyntaxNode, dow?: SyntaxNodeInstantiate a CronExpression. Each field is optional and defaults to *

Field

Represents a single field in the cron expression.

methodsignaturedescription
constructorField(exprs: SyntaxNode[])Instantiate a field with the given expressions

StringLiteral

Nodes containing terminal values in the ast. Can be a number or a literal alias (MON,TUE,SUN... for days for instance).

methodsignaturedescription
constructorStringLiteral(value: string)Instantiate a literal with the given value

DualExpression

An expression comprising of two node operands. Known implementations are range and step expression.

methodsignaturedescription
constructorDualExpression(lhs: SyntaxNode, rhs: SyntaxNode)Instantiate a dual expression with the given operands

development

Scripts

build

Build into a ES5 package. Output can be found in the dist folder.

test

Run Jest unit tests

lint

run eslint on the sources.

lint:fix

Run eslint and correct fixable violations.

clean

Clean and delete the output folder.

0.1.3

3 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.1.0-RC1

4 years ago

0.1.0-beta5

4 years ago

0.1.0-beta4

5 years ago

0.1.0-beta3

5 years ago

0.1.0-beta2

5 years ago