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

Function parse

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

Source from the content-addressed store, hash-verified

296 * @category constructors
297 */
298export const parse = (cron: string, tz?: DateTime.TimeZone | string): Either.Either<Cron, ParseError> => {
299 const segments = cron.trim().split(/\s+/).filter(String.isNonEmpty)
300 if (segments.length !== 5 && segments.length !== 6) {
301 return Either.left(
302 new ParseError({
303 message: `Invalid number of segments in cron expression`,
304 input: cron
305 })
306 )
307 }
308
309 if (segments.length === 5) {
310 segments.unshift("0")
311 }
312
313 const [seconds, minutes, hours, days, months, weekdays] = segments
314 const zone = tz === undefined || dateTime.isTimeZone(tz) ?
315 Either.right(tz) :
316 Either.fromOption(dateTime.zoneFromString(tz), () =>
317 new ParseError({
318 message: `Invalid time zone in cron expression`,
319 input: tz
320 }))
321
322 return Either.all({
323 tz: zone,
324 seconds: parseSegment(seconds, secondOptions),
325 minutes: parseSegment(minutes, minuteOptions),
326 hours: parseSegment(hours, hourOptions),
327 days: parseSegment(days, dayOptions),
328 months: parseSegment(months, monthOptions),
329 weekdays: parseSegment(weekdays, weekdayOptions)
330 }).pipe(Either.map(({ days, hours, minutes, months, seconds, tz, weekdays }) =>
331 make({
332 tz,
333 seconds: seconds.values,
334 minutes: minutes.values,
335 hours: hours.values,
336 days: days.values,
337 months: months.values,
338 weekdays: weekdays.values,
339 and: (days.wildcard || weekdays.wildcard) && days.values.size !== 0 && weekdays.values.size !== 0
340 })
341 ))
342}
343
344/**
345 * Parses a cron expression into a `Cron` instance.

Callers 3

goFunction · 0.70
unsafeParseFunction · 0.70
Option.test.tsFile · 0.50

Calls 5

parseSegmentFunction · 0.85
splitMethod · 0.80
makeFunction · 0.70
pipeMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected