Skip to content

Commit

Permalink
Use narrowing instead of non-null assertion
Browse files Browse the repository at this point in the history
Same behavior but using better practices.
  • Loading branch information
victorlin committed Nov 14, 2024
1 parent f6513d6 commit 4200b79
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions static-site/src/components/ListResources/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,17 @@ const Title = styled.div`

function _snapshotSummary(dates: string[]) {
const d = [...dates].sort()
if (d.length < 1) throw new InternalError("Missing dates.")

const d1 = new Date(d.at( 0)!).getTime();
const d2 = new Date(d.at(-1)!).getTime();
const days = (d2 - d1)/1000/60/60/24;
const d1 = d[0];
const d2 = d.at(-1);
if (d1 === undefined || d2 === undefined) {
throw new InternalError("Missing dates.");
}
const days = (new Date(d2).getTime() - new Date(d1).getTime())/1000/60/60/24;
let duration = '';
if (days < 100) duration=`${days} days`;
else if (days < 365*2) duration=`${Math.round(days/(365/12))} months`;
else duration=`${Math.round(days/365)} years`;
return {duration, first: d[0], last:d.at(-1)};
return {duration, first: d1, last: d2};
}

function _draw(ref, resource: VersionedResource) {
Expand Down

0 comments on commit 4200b79

Please sign in to comment.