mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-23 16:09:11 +00:00
feat: improve smart time entry (#2029)
Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
This commit is contained in:
committed by
GitHub
parent
8c52556cc0
commit
4662cc8a26
@@ -30,6 +30,22 @@ describe('test parseUserTime()', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('infer unit from bigger unit', () => {
|
||||||
|
const testData = [
|
||||||
|
{ value: '1h2', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE },
|
||||||
|
{ value: '1h2s', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_SECOND },
|
||||||
|
{ value: '1m2', expect: MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
|
||||||
|
{ value: '1m2s', expect: MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const s of testData) {
|
||||||
|
it(`handles ${s.value}`, () => {
|
||||||
|
expect(typeof parseUserTime(s.value)).toBe('number');
|
||||||
|
expect(parseUserTime(s.value)).toBe(s.expect);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
describe('parses strings correctly', () => {
|
describe('parses strings correctly', () => {
|
||||||
const ts = [
|
const ts = [
|
||||||
{ value: '1.1.1', expect: MILLIS_PER_HOUR + MILLIS_PER_MINUTE + MILLIS_PER_SECOND },
|
{ value: '1.1.1', expect: MILLIS_PER_HOUR + MILLIS_PER_MINUTE + MILLIS_PER_SECOND },
|
||||||
|
|||||||
@@ -116,12 +116,14 @@ function checkMatchers(value: string): number | null {
|
|||||||
// if there's a number after 'h' without a unit and it's not followed by 's', treat it as minutes
|
// if there's a number after 'h' without a unit and it's not followed by 's', treat it as minutes
|
||||||
const implicitMinutesValue = hoursMatch?.[2] ? parse(hoursMatch[2]) : 0;
|
const implicitMinutesValue = hoursMatch?.[2] ? parse(hoursMatch[2]) : 0;
|
||||||
|
|
||||||
const minutesMatch = /(\d+)m/i.exec(value);
|
const minutesMatch = /(\d+)m(?:(\d+)?)/i.exec(value);
|
||||||
// only use implicit minutes if there's no explicit minutes match
|
// only use implicit minutes if there's no explicit minutes match
|
||||||
const minutesMatchValue = minutesMatch ? parse(minutesMatch[1]) : implicitMinutesValue;
|
const minutesMatchValue = minutesMatch ? parse(minutesMatch[1]) : implicitMinutesValue;
|
||||||
|
// if there's a number after 'm' without a unit treat it as seconds
|
||||||
|
const implicitSecondsValue = minutesMatch?.[2] ? parse(minutesMatch[2]) : 0;
|
||||||
|
|
||||||
const secondsMatch = /(\d+)s/i.exec(value);
|
const secondsMatch = /(\d+)s/i.exec(value);
|
||||||
const secondsMatchValue = secondsMatch ? parse(secondsMatch[1]) : 0;
|
const secondsMatchValue = secondsMatch ? parse(secondsMatch[1]) : implicitSecondsValue;
|
||||||
|
|
||||||
if (hoursMatch || minutesMatch || secondsMatch) {
|
if (hoursMatch || minutesMatch || secondsMatch) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user