Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests to support docs #972

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/client/src/common/utils/__tests__/dateConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
9 changes: 3 additions & 6 deletions apps/client/src/common/utils/dateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -155,6 +151,7 @@ export const forgivingStringToMillis = (value: string): number => {
if (isPM && hoursMatchValue < 12) {
millis += 12 * MILLIS_PER_HOUR;
}

return millis;
};

Expand Down