Modules@robinpath/cron
cron

@robinpath/cron

0.1.1Node.jsPublic

Cron expression parsing, validation, scheduling, and human-readable descriptions

Cron

Cron expression parsing, validation, scheduling, and human-readable descriptions

Package: @robinpath/cron | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the cron module when you need to:

  • Validate a cron expression -- Use cron.isValid to perform this operation
  • Parse cron expression into expanded fields -- Use cron.parse to perform this operation
  • Get next occurrence after a date -- Use cron.next to perform this operation
  • Get next N occurrences -- Use cron.nextN to perform this operation
  • Get previous occurrence before a date -- Use cron.prev to perform this operation

Quick Reference

FunctionDescriptionReturns
isValidValidate a cron expressionTrue if valid
parseParse cron expression into expanded fields{minute[], hour[], dayOfMonth[], month[], dayOfWeek[]}
nextGet next occurrence after a dateISO date string
nextNGet next N occurrencesArray of ISO date strings
prevGet previous occurrence before a dateISO date string
matchesCheck if a date matches a cron expressionTrue if matches
describeHuman-readable description of a cron expressionDescription string
betweenGet all occurrences between two datesArray of ISO date strings

Functions

isValid

Validate a cron expression

Module: cron | Returns: boolean -- True if valid

cron.isValid "*/5 * * * *"
ParameterTypeRequiredDescription
expressionstringYesCron expression (5 fields)

parse

Parse cron expression into expanded fields

Module: cron | Returns: object -- {minute[], hour[], dayOfMonth[], month[], dayOfWeek[]}

cron.parse "0 9 * * 1-5"
ParameterTypeRequiredDescription
expressionstringYesCron expression

next

Get next occurrence after a date

Module: cron | Returns: string -- ISO date string

cron.next "*/5 * * * *"
ParameterTypeRequiredDescription
expressionstringYesCron expression
fromstringNoFrom date (default: now)

nextN

Get next N occurrences

Module: cron | Returns: array -- Array of ISO date strings

cron.nextN "0 * * * *" 10
ParameterTypeRequiredDescription
expressionstringYesCron expression
countnumberNoNumber of occurrences (default: 5)
fromstringNoFrom date (default: now)

prev

Get previous occurrence before a date

Module: cron | Returns: string -- ISO date string

cron.prev "0 9 * * *"
ParameterTypeRequiredDescription
expressionstringYesCron expression
fromstringNoFrom date (default: now)

matches

Check if a date matches a cron expression

Module: cron | Returns: boolean -- True if matches

cron.matches "0 9 * * *" "2024-01-15T09:00:00"
ParameterTypeRequiredDescription
expressionstringYesCron expression
datestringNoDate to check (default: now)

describe

Human-readable description of a cron expression

Module: cron | Returns: string -- Description string

cron.describe "*/5 * * * *"
ParameterTypeRequiredDescription
expressionstringYesCron expression

between

Get all occurrences between two dates

Module: cron | Returns: array -- Array of ISO date strings

cron.between "0 * * * *" "2024-01-01" "2024-01-02"
ParameterTypeRequiredDescription
expressionstringYesCron expression
startstringYesStart date
endstringYesEnd date

Error Handling

All functions throw on failure. Common errors:

ErrorCause
No matching date found within 1 yearCheck the error message for details
Invalid cron expressionCheck the error message for details
@desc "Is valid and validate result"
do
  set $result as cron.isValid "*/5 * * * *"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Cron workflow

Chain multiple cron operations together.

@desc "Is valid, parse, and more"
do
  set $r_isValid as cron.isValid "*/5 * * * *"
  set $r_parse as cron.parse "0 9 * * 1-5"
  set $r_next as cron.next "*/5 * * * *"
  print "All operations complete"
enddo

2. Safe isValid with validation

Check results before proceeding.

@desc "Is valid and validate result"
do
  set $result as cron.isValid "*/5 * * * *"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/cron

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size4.6 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops