parseFunctionCron function

FunctionCronSpec parseFunctionCron(
  1. String expression
)

Parses a cron expression or @ macro; throws FunctionCronException for anything else, with the TypeScript engine's message.

Implementation

FunctionCronSpec parseFunctionCron(String expression) {
  final text = expression.trim();
  final source = _macros[text.toLowerCase()] ?? text;
  if (source.startsWith('@')) {
    throw FunctionCronException(
      'Unknown cron macro $text; use @hourly, @daily, @weekly, @monthly or '
      '@yearly.',
    );
  }
  final fields = source.split(RegExp(r'\s+'));
  if (fields.length != 5) {
    throw FunctionCronException(
      'A cron expression has five fields (minute hour day-of-month month '
      'day-of-week); got ${fields.length}.',
    );
  }
  final parsed = [
    for (var i = 0; i < _fields.length; i++) _parseField(fields[i], _fields[i]),
  ];
  // Both 0 and 7 are Sunday.
  final daysOfWeek = {...parsed[4].values};
  if (daysOfWeek.remove(7)) daysOfWeek.add(0);
  return FunctionCronSpec._(
    minutes: _ascending(parsed[0].values),
    hours: _ascending(parsed[1].values),
    daysOfMonth: _ascending(parsed[2].values),
    months: _ascending(parsed[3].values),
    daysOfWeek: _ascending(daysOfWeek),
    dayOfMonthRestricted: parsed[2].restricted && parsed[2].values.length < 31,
    dayOfWeekRestricted: parsed[4].restricted && daysOfWeek.length < 7,
  );
}