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

Function parseSegment

packages/effect/src/Cron.ts:1398–1473  ·  view source on GitHub ↗
(
  input: string,
  options: SegmentOptions
)

Source from the content-addressed store, hash-verified

1396}
1397
1398const parseSegment = (
1399 input: string,
1400 options: SegmentOptions
1401): Result.Result<ParsedSegment, CronParseError> => {
1402 const values = new Set<number>()
1403 const fields = input.split(",")
1404 const first = splitStep(fields[0]!)
1405 const wildcard = first[0] === "*"
1406 const normalize = options.normalize ?? ((value: number) => value)
1407 const add = wildcard && (first[1] === undefined || first[1] === 1) ?
1408 constVoid :
1409 (value: number) => {
1410 values.add(normalize(value))
1411 }
1412
1413 for (let index = 0; index < fields.length; index++) {
1414 const field = fields[index]!
1415 const [raw, step] = index === 0 ? first : splitStep(field)
1416 if (step !== undefined) {
1417 if (!Number.isInteger(step)) {
1418 return Result.fail(new CronParseError({ message: `Expected step value to be a positive integer`, input }))
1419 }
1420 if (step < 1) {
1421 return Result.fail(new CronParseError({ message: `Expected step value to be greater than 0`, input }))
1422 }
1423 if (step > options.max) {
1424 return Result.fail(
1425 new CronParseError({ message: `Expected step value to be less than or equal to ${options.max}`, input })
1426 )
1427 }
1428 }
1429
1430 if (raw === "*") {
1431 if (index === 0 && (step === undefined || step === 1)) {
1432 continue
1433 }
1434 for (let i = options.min; i <= options.max; i += step ?? 1) {
1435 add(i)
1436 }
1437 } else {
1438 const [left, right] = splitRange(raw, options.aliases)
1439 if (!Number.isInteger(left)) {
1440 return Result.fail(new CronParseError({ message: `Expected a positive integer`, input }))
1441 }
1442 if (left < options.min || left > options.max) {
1443 return Result.fail(
1444 new CronParseError({ message: `Expected a value between ${options.min} and ${options.max}`, input })
1445 )
1446 }
1447
1448 if (right === undefined) {
1449 for (let i = left; i <= (step === undefined ? left : options.max); i += step ?? 1) {
1450 add(i)
1451 }
1452 } else {
1453 if (!Number.isInteger(right)) {
1454 return Result.fail(new CronParseError({ message: `Expected a positive integer`, input }))
1455 }

Callers 1

parseFunction · 0.70

Calls 8

splitStepFunction · 0.85
splitRangeFunction · 0.85
normalizeFunction · 0.70
addFunction · 0.70
addMethod · 0.65
failMethod · 0.65
splitMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected