Enforce strict format for interval durations
This commit is contained in:
parent
4c23180621
commit
ca451fd2d6
|
|
@ -121,4 +121,41 @@ Cooldown: 5:30 70%..45%
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const parseInterval = (interval: string) =>
|
||||||
|
parse(`Name: My Workout\n${interval}`).intervals[0];
|
||||||
|
|
||||||
|
it("parses correct duration formats", () => {
|
||||||
|
expect(parseInterval("Interval: 0:10 50%").duration).toEqual(10);
|
||||||
|
expect(parseInterval("Interval: 00:10 50%").duration).toEqual(10);
|
||||||
|
expect(parseInterval("Interval: 0:00:10 50%").duration).toEqual(10);
|
||||||
|
expect(parseInterval("Interval: 0:02:05 50%").duration).toEqual(125);
|
||||||
|
expect(parseInterval("Interval: 1:00:00 50%").duration).toEqual(3600);
|
||||||
|
expect(parseInterval("Interval: 1:00:0 50%").duration).toEqual(3600);
|
||||||
|
expect(parseInterval("Interval: 1:0:0 50%").duration).toEqual(3600);
|
||||||
|
expect(parseInterval("Interval: 10:00:00 50%").duration).toEqual(36000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws error for incorrect duration formats", () => {
|
||||||
|
expect(() =>
|
||||||
|
parseInterval("Interval: 10 50%")
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Unrecognized interval parameter \\"10\\""`
|
||||||
|
);
|
||||||
|
expect(() =>
|
||||||
|
parseInterval("Interval: :10 50%")
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Unrecognized interval parameter \\":10\\""`
|
||||||
|
);
|
||||||
|
expect(() =>
|
||||||
|
parseInterval("Interval: 0:100 50%")
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Unrecognized interval parameter \\"0:100\\""`
|
||||||
|
);
|
||||||
|
expect(() =>
|
||||||
|
parseInterval("Interval: 00:00:00:10 50%")
|
||||||
|
).toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Unrecognized interval parameter \\"00:00:00:10\\""`
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ const toSeconds = (str: string): number => {
|
||||||
const toFraction = (percentage: number): number => percentage / 100;
|
const toFraction = (percentage: number): number => percentage / 100;
|
||||||
|
|
||||||
const tokenizeValueParam = (text: string): Token => {
|
const tokenizeValueParam = (text: string): Token => {
|
||||||
if (/^[0-9:]+$/.test(text)) {
|
if (/^([0-9]{1,2}:)?[0-9]{1,2}:[0-9]{1,2}$/.test(text)) {
|
||||||
return { type: "duration", value: toSeconds(text) };
|
return { type: "duration", value: toSeconds(text) };
|
||||||
}
|
}
|
||||||
if (/^[0-9]+rpm$/.test(text)) {
|
if (/^[0-9]+rpm$/.test(text)) {
|
||||||
|
|
@ -63,7 +63,7 @@ const tokenizeValueParam = (text: string): Token => {
|
||||||
if (/^[0-9]+%$/.test(text)) {
|
if (/^[0-9]+%$/.test(text)) {
|
||||||
return { type: "intensity", value: toFraction(toInteger(text)) };
|
return { type: "intensity", value: toFraction(toInteger(text)) };
|
||||||
}
|
}
|
||||||
throw new Error(`Unrecognized parameter "${text}"`);
|
throw new Error(`Unrecognized interval parameter "${text}"`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const tokenizeParams = (type: LabelTokenValue, text: string): Token[] => {
|
const tokenizeParams = (type: LabelTokenValue, text: string): Token[] => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue