chore: add tests to support docs

This commit is contained in:
Carlos Valente
2024-05-15 21:19:38 +02:00
committed by Carlos Valente
parent 37786c3528
commit ad46fac2d7
2 changed files with 31 additions and 6 deletions
@@ -45,6 +45,34 @@ describe('test forgivingStringToMillis()', () => {
}
});
describe('#33 separators are parsed according to doc examples ', () => {
const ts = [
{ value: '0.1', expect: MILLIS_PER_MINUTE },
{ value: '0 1', expect: MILLIS_PER_MINUTE },
{ value: '0:1', expect: MILLIS_PER_MINUTE },
{ value: '0,1', expect: MILLIS_PER_MINUTE },
{ value: '2.2.2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
{ value: '2 2 2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
{ value: '2:2:2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
{ value: '2,2,2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
{ value: '2,2,2', expect: 2 * MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 2 * MILLIS_PER_SECOND },
{ value: '10:', expect: 10 * MILLIS_PER_HOUR },
{ value: ':10', expect: 10 * MILLIS_PER_MINUTE },
{ value: '10', expect: 10 * MILLIS_PER_MINUTE },
{ value: '120', expect: MILLIS_PER_HOUR + 20 * MILLIS_PER_MINUTE },
{ value: '90m', expect: 90 * MILLIS_PER_MINUTE },
{ value: '1.2', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE },
{ value: '1.2.3', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 3 * MILLIS_PER_SECOND },
{ value: '123456', expect: 12 * MILLIS_PER_HOUR + 34 * MILLIS_PER_MINUTE + 56 * MILLIS_PER_SECOND },
];
for (const s of ts) {
it(`handles ${s.value}`, () => {
expect(forgivingStringToMillis(s.value)).toBe(s.expect);
});
}
});
describe('parses time strings', () => {
const ts = [
{ value: '1h2m3s', expect: MILLIS_PER_HOUR + 2 * MILLIS_PER_MINUTE + 3 * MILLIS_PER_SECOND },
+3 -6
View File
@@ -97,12 +97,7 @@ function inferSeparators(value: string, isAM: boolean, isPM: boolean) {
inferredMillis = parse(value[0]) * MILLIS_PER_HOUR + parse(value.substring(1)) * MILLIS_PER_MINUTE;
} else if (length === 4) {
inferredMillis = parse(value.substring(0, 2)) * MILLIS_PER_HOUR + parse(value.substring(2)) * MILLIS_PER_MINUTE;
} else if (length === 5) {
const hours = parse(value.substring(0, 2));
const minutes = parse(value.substring(2, 4));
const seconds = parse(value.substring(4));
inferredMillis = hours * MILLIS_PER_HOUR + minutes * MILLIS_PER_MINUTE + seconds * MILLIS_PER_SECOND;
} else if (length >= 6) {
} else if (length >= 5) {
const hours = parse(value.substring(0, 2));
const minutes = parse(value.substring(2, 4));
const seconds = parse(value.substring(4));
@@ -147,6 +142,7 @@ export const forgivingStringToMillis = (value: string): number => {
hoursMatchValue = addAM;
}
if (first != null && second != null && third == null) {
// if string has two sections, treat as [hours] [minutes]
millis = parse(first) * MILLIS_PER_HOUR;
millis += parse(second) * MILLIS_PER_MINUTE;
}
@@ -155,6 +151,7 @@ export const forgivingStringToMillis = (value: string): number => {
if (isPM && hoursMatchValue < 12) {
millis += 12 * MILLIS_PER_HOUR;
}
return millis;
};