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

Function parse

packages/effect/src/Cron.ts:614–655  ·  view source on GitHub ↗
(cron: string, tz?: DateTime.TimeZone | string, options?: {
  readonly onDstOverlap?: DstOverlapPolicy | undefined
})

Source from the content-addressed store, hash-verified

612 * @since 2.0.0
613 */
614export const parse = (cron: string, tz?: DateTime.TimeZone | string, options?: {
615 readonly onDstOverlap?: DstOverlapPolicy | undefined
616}): Result.Result<Cron, CronParseError> => {
617 const segments = cron.trim().split(/\s+/).filter(String.isNonEmpty)
618 if (segments.length !== 5 && segments.length !== 6) {
619 return Result.fail(new CronParseError({ message: `Invalid number of segments in cron expression`, input: cron }))
620 }
621
622 if (segments.length === 5) {
623 segments.unshift("0")
624 }
625
626 const [seconds, minutes, hours, days, months, weekdays] = segments
627 const zone = tz === undefined || dateTime.isTimeZone(tz) ?
628 Result.succeed(tz) :
629 Result.fromOption(
630 dateTime.zoneFromString(tz),
631 () => new CronParseError({ message: `Invalid time zone in cron expression`, input: tz })
632 )
633
634 return Result.all({
635 tz: zone,
636 seconds: parseSegment(seconds, secondOptions),
637 minutes: parseSegment(minutes, minuteOptions),
638 hours: parseSegment(hours, hourOptions),
639 days: parseSegment(days, dayOptions),
640 months: parseSegment(months, monthOptions),
641 weekdays: parseSegment(weekdays, weekdayOptions)
642 }).pipe(Result.map(({ tz, seconds, minutes, hours, days, months, weekdays }) =>
643 make({
644 tz,
645 seconds: seconds.values,
646 minutes: minutes.values,
647 hours: hours.values,
648 days: days.values,
649 months: months.values,
650 weekdays: weekdays.values,
651 and: (days.wildcard || weekdays.wildcard) && days.values.size !== 0 && weekdays.values.size !== 0,
652 onDstOverlap: options?.onDstOverlap
653 })
654 ))
655}
656
657/**
658 * Parses a cron expression into a `Cron` instance, throwing on failure.

Callers 8

parseMethod · 0.70
getParserMethod · 0.70
makeFallbackMethod · 0.70
parseUnsafeFunction · 0.70
netAddressFromStringFunction · 0.70
dnsCanonicalAddressFunction · 0.70
withParseOptionsFunction · 0.70

Calls 10

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

Tested by

no test coverage detected