| 1216 | } |
| 1217 | |
| 1218 | const parseSegment = ( |
| 1219 | input: string, |
| 1220 | options: SegmentOptions |
| 1221 | ): Result.Result<ParsedSegment, CronParseError> => { |
| 1222 | const values = new Set<number>() |
| 1223 | const fields = input.split(",") |
| 1224 | const first = splitStep(fields[0]!) |
| 1225 | const wildcard = first[0] === "*" |
| 1226 | const normalize = options.normalize ?? ((value: number) => value) |
| 1227 | const add = wildcard && (first[1] === undefined || first[1] === 1) ? |
| 1228 | constVoid : |
| 1229 | (value: number) => { |
| 1230 | values.add(normalize(value)) |
| 1231 | } |
| 1232 | |
| 1233 | for (let index = 0; index < fields.length; index++) { |
| 1234 | const field = fields[index]! |
| 1235 | const [raw, step] = index === 0 ? first : splitStep(field) |
| 1236 | if (step !== undefined) { |
| 1237 | if (!Number.isInteger(step)) { |
| 1238 | return Result.fail(new CronParseError({ message: `Expected step value to be a positive integer`, input })) |
| 1239 | } |
| 1240 | if (step < 1) { |
| 1241 | return Result.fail(new CronParseError({ message: `Expected step value to be greater than 0`, input })) |
| 1242 | } |
| 1243 | if (step > options.max) { |
| 1244 | return Result.fail( |
| 1245 | new CronParseError({ message: `Expected step value to be less than or equal to ${options.max}`, input }) |
| 1246 | ) |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (raw === "*") { |
| 1251 | if (index === 0 && (step === undefined || step === 1)) { |
| 1252 | continue |
| 1253 | } |
| 1254 | for (let i = options.min; i <= options.max; i += step ?? 1) { |
| 1255 | add(i) |
| 1256 | } |
| 1257 | } else { |
| 1258 | const [left, right] = splitRange(raw, options.aliases) |
| 1259 | if (!Number.isInteger(left)) { |
| 1260 | return Result.fail(new CronParseError({ message: `Expected a positive integer`, input })) |
| 1261 | } |
| 1262 | if (left < options.min || left > options.max) { |
| 1263 | return Result.fail( |
| 1264 | new CronParseError({ message: `Expected a value between ${options.min} and ${options.max}`, input }) |
| 1265 | ) |
| 1266 | } |
| 1267 | |
| 1268 | if (right === undefined) { |
| 1269 | for (let i = left; i <= (step === undefined ? left : options.max); i += step ?? 1) { |
| 1270 | add(i) |
| 1271 | } |
| 1272 | } else { |
| 1273 | if (!Number.isInteger(right)) { |
| 1274 | return Result.fail(new CronParseError({ message: `Expected a positive integer`, input })) |
| 1275 | } |