(cron: string, tz?: DateTime.TimeZone | string): Result.Result<
Cron,
CronParseError
>Parses a cron expression safely into a Cron instance, returning a Result
instead of throwing.
When to use
Use to parse cron expressions from configuration or user input while handling
invalid input as a Result.
Details
The expression may contain five fields, where seconds default to 0, or six
fields including seconds. Fields support *, comma-separated values, ranges,
steps, and month or weekday aliases. Invalid expressions fail with
CronParseError.
Example (Parsing cron expressions)
import { Cron, Result } from "effect"
import * as assert from "node:assert"
// At 04:00 on every day-of-month from 8 through 14.
assert.deepStrictEqual(
Cron.parse("0 0 4 8-14 * *"),
Result.succeed(Cron.make({
seconds: [0],
minutes: [0],
hours: [4],
days: [8, 9, 10, 11, 12, 13, 14],
months: [],
weekdays: []
}))
)export const const parse: (
cron: string,
tz?: DateTime.TimeZone | string
) => Result.Result<Cron, CronParseError>
Parses a cron expression safely into a Cron instance, returning a Result
instead of throwing.
When to use
Use to parse cron expressions from configuration or user input while handling
invalid input as a Result.
Details
The expression may contain five fields, where seconds default to 0, or six
fields including seconds. Fields support *, comma-separated values, ranges,
steps, and month or weekday aliases. Invalid expressions fail with
CronParseError.
Example (Parsing cron expressions)
import { Cron, Result } from "effect"
import * as assert from "node:assert"
// At 04:00 on every day-of-month from 8 through 14.
assert.deepStrictEqual(
Cron.parse("0 0 4 8-14 * *"),
Result.succeed(Cron.make({
seconds: [0],
minutes: [0],
hours: [4],
days: [8, 9, 10, 11, 12, 13, 14],
months: [],
weekdays: []
}))
)
parse = (cron: stringcron: string, tz: DateTime.TimeZone | stringtz?: import DateTimeDateTime.type TimeZone = DateTime.TimeZone.Offset | DateTime.TimeZone.NamedRepresents a time zone used by DateTime.Zoned.
Details
A TimeZone is either a fixed offset from UTC or a named IANA time zone.
Companion namespace containing the public variant and protocol types for
TimeZone.
TimeZone | string): import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<Cron, class CronParseErrorclass CronParseError {
name: string;
message: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
input: string | undefined;
}
Represents an error that occurs when parsing a cron expression fails.
When to use
Use to handle invalid cron expression failures returned by parse.
Details
This error provides information about what went wrong during parsing,
including the error message and optionally the input that caused the error.
Example (Handling cron parse failures)
import { Cron, Result } from "effect"
const result = Cron.parse("invalid expression")
if (Result.isFailure(result)) {
const error: Cron.CronParseError = result.failure
console.log(error.message) // "Invalid number of segments in cron expression"
console.log(error.input) // "invalid expression"
}
CronParseError> => {
const const segments: string[]segments = cron: stringcron.globalThis.String.trim(): stringRemoves the leading and trailing white space and line terminator characters from a string.
trim().globalThis.String.split(splitter: {
[Symbol.split](string: string, limit?: number): string[];
}, limit?: number): string[] (+1 overload)
Split a string into substrings using the specified separator and return them as an array.
split(/\s+/).Array<string>.filter(predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[] (+1 overload)Returns the elements of an array that meet the condition specified in a callback function.
filter(import StringString.const isNonEmpty: (
self: string
) => boolean
Checks whether a string is non-empty.
Example (Checking for non-empty strings)
import { String } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(String.isNonEmpty(""), false)
assert.deepStrictEqual(String.isNonEmpty("a"), true)
isNonEmpty)
if (const segments: string[]segments.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 5 && const segments: string[]segments.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 6) {
return import ResultResult.const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(new constructor CronParseError<{
readonly message: string;
readonly input?: string;
}>(args: {
readonly message: string;
readonly input?: string | undefined;
}): Arr & {
readonly _tag: Tag;
} & Readonly<A>
Represents an error that occurs when parsing a cron expression fails.
When to use
Use to handle invalid cron expression failures returned by parse.
Details
This error provides information about what went wrong during parsing,
including the error message and optionally the input that caused the error.
Example (Handling cron parse failures)
import { Cron, Result } from "effect"
const result = Cron.parse("invalid expression")
if (Result.isFailure(result)) {
const error: Cron.CronParseError = result.failure
console.log(error.message) // "Invalid number of segments in cron expression"
console.log(error.input) // "invalid expression"
}
CronParseError({ message: stringmessage: `Invalid number of segments in cron expression`, input?: string | undefinedinput: cron: stringcron }))
}
if (const segments: string[]segments.Array<T>.length: 5 | 6Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 5) {
const segments: string[]segments.Array<string>.unshift(...items: string[]): numberInserts new elements at the start of an array, and returns the new length of the array.
unshift("0")
}
const [const seconds: stringseconds, const minutes: stringminutes, const hours: stringhours, const days: stringdays, const months: stringmonths, const weekdays: stringweekdays] = const segments: string[]segments
const const zone:
| Result.Result<
DateTime.TimeZone | undefined,
never
>
| Result.Result<
DateTime.TimeZone,
CronParseError
>
zone = tz: DateTime.TimeZone | stringtz === var undefinedundefined || import dateTimedateTime.const isTimeZone: (
u: unknown
) => u is DateTime.TimeZone
isTimeZone(tz: DateTime.TimeZone | stringtz) ?
import ResultResult.const succeed: <A>(right: A) => Result<A>Creates a Result holding a Success value.
Details
- Use when you have a value and want to lift it into the
Result type
- The error type
E defaults to never
Example (Wrapping a value)
import { Result } from "effect"
const result = Result.succeed(42)
console.log(Result.isSuccess(result))
// Output: true
succeed(tz: DateTime.TimeZone | stringtz) :
import ResultResult.const fromOption: {
<E>(onNone: () => E): <A>(
self: Option<A>
) => Result<A, E>
<A, E>(
self: Option<A>,
onNone: () => E
): Result<A, E>
}
fromOption(
import dateTimedateTime.const zoneFromString: (
zone: string
) => Option.Option<DateTime.TimeZone>
zoneFromString(tz: DateTime.TimeZone | stringtz),
() => new constructor CronParseError<{
readonly message: string;
readonly input?: string;
}>(args: {
readonly message: string;
readonly input?: string | undefined;
}): Arr & {
readonly _tag: Tag;
} & Readonly<A>
Represents an error that occurs when parsing a cron expression fails.
When to use
Use to handle invalid cron expression failures returned by parse.
Details
This error provides information about what went wrong during parsing,
including the error message and optionally the input that caused the error.
Example (Handling cron parse failures)
import { Cron, Result } from "effect"
const result = Cron.parse("invalid expression")
if (Result.isFailure(result)) {
const error: Cron.CronParseError = result.failure
console.log(error.message) // "Invalid number of segments in cron expression"
console.log(error.input) // "invalid expression"
}
CronParseError({ message: stringmessage: `Invalid time zone in cron expression`, input?: string | undefinedinput: tz: DateTime.TimeZone | stringtz })
)
return import ResultResult.const all: <
I extends
| Iterable<Result<any, any>>
| Record<string, Result<any, any>>
>(
input: I
) => [I] extends [ReadonlyArray<Result<any, any>>]
? Result<
{
-readonly [K in keyof I]: [I[K]] extends [
Result<infer R, any>
]
? R
: never
},
I[number] extends never
? never
: [I[number]] extends [
Result<any, infer L>
]
? L
: never
>
: [I] extends [
Iterable<Result<infer R, infer L>>
]
? Result<Array<R>, L>
: Result<
{
-readonly [K in keyof I]: [I[K]] extends [
Result<infer R, any>
]
? R
: never
},
I[keyof I] extends never
? never
: [I[keyof I]] extends [
Result<any, infer L>
]
? L
: never
>
Collects a structure of Results into a single Result of collected values.
When to use
Use to collect independent Result values into one Result while preserving
the original structure.
Details
Accepts:
- A tuple/array: returns
Result with a tuple/array of success values
- A struct (record): returns
Result with a struct of success values
- An iterable: returns
Result with an array of success values
Short-circuits on the first Failure encountered; later elements are not inspected.
Example (Collecting a tuple and a struct)
import { Result } from "effect"
// Tuple
const tuple = Result.all([Result.succeed(1), Result.succeed("two")])
console.log(tuple)
// Output: { _tag: "Success", success: [1, "two"], ... }
// Struct
const struct = Result.all({ x: Result.succeed(1), y: Result.fail("err") })
console.log(struct)
// Output: { _tag: "Failure", failure: "err", ... }
all({
tz: | Result.Result<
DateTime.TimeZone | undefined,
never
>
| Result.Result<
DateTime.TimeZone,
CronParseError
>
tz: const zone:
| Result.Result<
DateTime.TimeZone | undefined,
never
>
| Result.Result<
DateTime.TimeZone,
CronParseError
>
zone,
seconds: Result.Result<
ParsedSegment,
CronParseError
>
seconds: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const seconds: stringseconds, const secondOptions: SegmentOptionsconst secondOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
secondOptions),
minutes: Result.Result<
ParsedSegment,
CronParseError
>
minutes: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const minutes: stringminutes, const minuteOptions: SegmentOptionsconst minuteOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
minuteOptions),
hours: Result.Result<
ParsedSegment,
CronParseError
>
hours: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const hours: stringhours, const hourOptions: SegmentOptionsconst hourOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
hourOptions),
days: Result.Result<
ParsedSegment,
CronParseError
>
days: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const days: stringdays, const dayOptions: SegmentOptionsconst dayOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
dayOptions),
months: Result.Result<
ParsedSegment,
CronParseError
>
months: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const months: stringmonths, const monthOptions: SegmentOptionsconst monthOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
monthOptions),
weekdays: Result.Result<
ParsedSegment,
CronParseError
>
weekdays: const parseSegment: (
input: string,
options: SegmentOptions
) => Result.Result<ParsedSegment, CronParseError>
parseSegment(const weekdays: stringweekdays, const weekdayOptions: SegmentOptionsconst weekdayOptions: {
min: number;
max: number;
aliases: Record<string, number> | undefined;
normalize: ((value: number) => number) | undefined;
}
weekdayOptions)
}).Pipeable.pipe<Result.Result<{
tz: DateTime.TimeZone | undefined;
seconds: ParsedSegment;
minutes: ParsedSegment;
hours: ParsedSegment;
days: ParsedSegment;
months: ParsedSegment;
weekdays: ParsedSegment;
}, CronParseError>, Result.Result<Cron, CronParseError>>(this: Result.Result<...>, ab: (_: Result.Result<{
tz: DateTime.TimeZone | undefined;
seconds: ParsedSegment;
minutes: ParsedSegment;
hours: ParsedSegment;
days: ParsedSegment;
months: ParsedSegment;
weekdays: ParsedSegment;
}, CronParseError>) => Result.Result<...>): Result.Result<...> (+21 overloads)
pipe(import ResultResult.const map: {
<A, A2>(f: (ok: A) => A2): <E>(
self: Result<A, E>
) => Result<A2, E>
<A, E, A2>(
self: Result<A, E>,
f: (ok: A) => A2
): Result<A2, E>
}
map(({ tz: DateTime.TimeZone | undefinedtz, seconds: ParsedSegment(parameter) seconds: {
values: Set<number>;
wildcard: boolean;
}
seconds, minutes: ParsedSegment(parameter) minutes: {
values: Set<number>;
wildcard: boolean;
}
minutes, hours: ParsedSegment(parameter) hours: {
values: Set<number>;
wildcard: boolean;
}
hours, days: ParsedSegment(parameter) days: {
values: Set<number>;
wildcard: boolean;
}
days, months: ParsedSegment(parameter) months: {
values: Set<number>;
wildcard: boolean;
}
months, weekdays: ParsedSegment(parameter) weekdays: {
values: Set<number>;
wildcard: boolean;
}
weekdays }) =>
const make: (values: {
readonly seconds?: Iterable<number> | undefined
readonly minutes: Iterable<number>
readonly hours: Iterable<number>
readonly days: Iterable<number>
readonly months: Iterable<number>
readonly weekdays: Iterable<number>
readonly and?: boolean | undefined
readonly tz?: DateTime.TimeZone | undefined
}) => Cron
Creates a Cron instance from time constraints.
When to use
Use to build a cron schedule from explicit sets of allowed time-field values.
Details
Constructs a cron schedule by specifying which seconds, minutes, hours,
days, months, and weekdays the schedule should match. Empty arrays mean
"match all" for that time unit. When both days and weekdays are restricted,
the default matches either field; set and: true to require both fields to
match.
Example (Creating schedules from constraints)
import { Cron } from "effect"
// Every day at midnight
const midnight = Cron.make({
minutes: [0],
hours: [0],
days: [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
],
months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
weekdays: [0, 1, 2, 3, 4, 5, 6]
})
// Every 15 minutes during business hours on weekdays
const businessHours = Cron.make({
minutes: [0, 15, 30, 45],
hours: [9, 10, 11, 12, 13, 14, 15, 16, 17],
days: [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31
],
months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
weekdays: [1, 2, 3, 4, 5] // Monday to Friday
})
make({
tz?: DateTime.TimeZone | undefinedtz,
seconds?: Iterable<number> | undefinedseconds: seconds: ParsedSegment(parameter) seconds: {
values: Set<number>;
wildcard: boolean;
}
seconds.ParsedSegment.values: Set<number>values,
minutes: Iterable<number>minutes: minutes: ParsedSegment(parameter) minutes: {
values: Set<number>;
wildcard: boolean;
}
minutes.ParsedSegment.values: Set<number>values,
hours: Iterable<number>hours: hours: ParsedSegment(parameter) hours: {
values: Set<number>;
wildcard: boolean;
}
hours.ParsedSegment.values: Set<number>values,
days: Iterable<number>days: days: ParsedSegment(parameter) days: {
values: Set<number>;
wildcard: boolean;
}
days.ParsedSegment.values: Set<number>values,
months: Iterable<number>months: months: ParsedSegment(parameter) months: {
values: Set<number>;
wildcard: boolean;
}
months.ParsedSegment.values: Set<number>values,
weekdays: Iterable<number>weekdays: weekdays: ParsedSegment(parameter) weekdays: {
values: Set<number>;
wildcard: boolean;
}
weekdays.ParsedSegment.values: Set<number>values,
and?: boolean | undefinedand: (days: ParsedSegment(parameter) days: {
values: Set<number>;
wildcard: boolean;
}
days.ParsedSegment.wildcard: booleanwildcard || weekdays: ParsedSegment(parameter) weekdays: {
values: Set<number>;
wildcard: boolean;
}
weekdays.ParsedSegment.wildcard: booleanwildcard) && days: ParsedSegment(parameter) days: {
values: Set<number>;
wildcard: boolean;
}
days.ParsedSegment.values: Set<number>values.Set<T>.size: numbersize !== 0 && weekdays: ParsedSegment(parameter) weekdays: {
values: Set<number>;
wildcard: boolean;
}
weekdays.ParsedSegment.values: Set<number>values.Set<T>.size: numbersize !== 0
})
))
}