MCPcopy Create free account
hub / github.com/Effect-TS/effect / parse

Function parse

packages/effect/src/Cron.ts:545–583  ·  view source on GitHub ↗
(cron: string, tz?: DateTime.TimeZone | string)

Source from the content-addressed store, hash-verified

543 * @since 2.0.0
544 */
545export const parse = (cron: string, tz?: DateTime.TimeZone | string): Result.Result<Cron, CronParseError> => {
546 const segments = cron.trim().split(/\s+/).filter(String.isNonEmpty)
547 if (segments.length !== 5 && segments.length !== 6) {
548 return Result.fail(new CronParseError({ message: `Invalid number of segments in cron expression`, input: cron }))
549 }
550
551 if (segments.length === 5) {
552 segments.unshift("0")
553 }
554
555 const [seconds, minutes, hours, days, months, weekdays] = segments
556 const zone = tz === undefined || dateTime.isTimeZone(tz) ?
557 Result.succeed(tz) :
558 Result.fromOption(
559 dateTime.zoneFromString(tz),
560 () => new CronParseError({ message: `Invalid time zone in cron expression`, input: tz })
561 )
562
563 return Result.all({
564 tz: zone,
565 seconds: parseSegment(seconds, secondOptions),
566 minutes: parseSegment(minutes, minuteOptions),
567 hours: parseSegment(hours, hourOptions),
568 days: parseSegment(days, dayOptions),
569 months: parseSegment(months, monthOptions),
570 weekdays: parseSegment(weekdays, weekdayOptions)
571 }).pipe(Result.map(({ tz, seconds, minutes, hours, days, months, weekdays }) =>
572 make({
573 tz,
574 seconds: seconds.values,
575 minutes: minutes.values,
576 hours: hours.values,
577 days: days.values,
578 months: months.values,
579 weekdays: weekdays.values,
580 and: (days.wildcard || weekdays.wildcard) && days.values.size !== 0 && weekdays.values.size !== 0
581 })
582 ))
583}
584
585/**
586 * Parses a cron expression into a `Cron` instance, throwing on failure.

Callers 3

getParserMethod · 0.70
parseUnsafeFunction · 0.70
Option.test.tsFile · 0.50

Calls 9

parseSegmentFunction · 0.85
filterMethod · 0.80
allMethod · 0.80
makeFunction · 0.70
pipeMethod · 0.65
splitMethod · 0.45
failMethod · 0.45
succeedMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected