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

Function parseSegment

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

Source from the content-addressed store, hash-verified

785}
786
787const parseSegment = (
788 input: string,
789 options: SegmentOptions
790): Either.Either<ParsedSegment, ParseError> => {
791 const values = new Set<number>()
792 const fields = input.split(",")
793 const first = splitStep(fields[0]!)
794 const wildcard = first[0] === "*"
795 const normalize = options.normalize ?? ((value: number) => value)
796 const add = wildcard && (first[1] === undefined || first[1] === 1)
797 ? constVoid
798 : (value: number) => {
799 values.add(normalize(value))
800 }
801
802 for (let index = 0; index < fields.length; index++) {
803 const field = fields[index]!
804 const [raw, step] = index === 0 ? first : splitStep(field)
805 if (step !== undefined) {
806 if (!Number.isInteger(step)) {
807 return Either.left(new ParseError({ message: `Expected step value to be a positive integer`, input }))
808 }
809 if (step < 1) {
810 return Either.left(new ParseError({ message: `Expected step value to be greater than 0`, input }))
811 }
812 if (step > options.max) {
813 return Either.left(new ParseError({ message: `Expected step value to be less than ${options.max}`, input }))
814 }
815 }
816
817 if (raw === "*") {
818 if (index === 0 && (step === undefined || step === 1)) {
819 continue
820 }
821 for (let i = options.min; i <= options.max; i += step ?? 1) {
822 add(i)
823 }
824 } else {
825 const [left, right] = splitRange(raw, options.aliases)
826 if (!Number.isInteger(left)) {
827 return Either.left(new ParseError({ message: `Expected a positive integer`, input }))
828 }
829 if (left < options.min || left > options.max) {
830 return Either.left(
831 new ParseError({ message: `Expected a value between ${options.min} and ${options.max}`, input })
832 )
833 }
834
835 if (right === undefined) {
836 for (let i = left; i <= (step === undefined ? left : options.max); i += step ?? 1) {
837 add(i)
838 }
839 } else {
840 if (!Number.isInteger(right)) {
841 return Either.left(new ParseError({ message: `Expected a positive integer`, input }))
842 }
843 if (right < options.min || right > options.max) {
844 return Either.left(

Callers 1

parseFunction · 0.85

Calls 6

splitStepFunction · 0.85
splitRangeFunction · 0.85
splitMethod · 0.80
normalizeFunction · 0.70
addMethod · 0.65
addFunction · 0.50

Tested by

no test coverage detected