Skip to content

Commit

Permalink
upgrade date-fns thru major version, update calls accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo committed Feb 8, 2024
1 parent 3dd635a commit a26db9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
33 changes: 14 additions & 19 deletions app/util/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Copyright Oxide Computer Company
*/
import { formatDistanceToNowStrict } from 'date-fns'
import { formatDistanceToNowStrict, type FormatDistanceToNowStrictOptions } from 'date-fns'

// locale setup and formatDistance function copied from here and modified
// https://github.com/date-fns/date-fns/blob/56a3856/src/locale/en-US/_lib/formatDistance/index.js
Expand All @@ -29,21 +29,16 @@ const formatDistanceLocale = {
almostXYears: '~ {{count}}y',
}

interface Options {
addSuffix: boolean
}

function formatDistance(
token: keyof typeof formatDistanceLocale,
count: string,
options: Options
) {
const result = formatDistanceLocale[token]?.replace('{{count}}', count)
if (result && options.addSuffix) {
return result + ' ago'
}
return result
}

export const timeAgoAbbr = (d: Date, options: Options = { addSuffix: false }) =>
formatDistanceToNowStrict(d, { ...options, locale: { formatDistance } })
export const timeAgoAbbr = (d: Date, options?: FormatDistanceToNowStrictOptions) =>
formatDistanceToNowStrict(d, {
...options,
locale: {
formatDistance: (token, count, options) => {
const result = formatDistanceLocale[token]?.replace('{{count}}', count.toString())
if (result && options?.addSuffix) {
return result + ' ago'
}
return result
},
},
})
27 changes: 9 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@tanstack/react-query-devtools": "^5.17.4",
"@tanstack/react-table": "^8.11.3",
"classnames": "^2.5.1",
"date-fns": "^2.30.0",
"date-fns": "^3.3.1",
"filesize": "^10.1.0",
"lodash.throttle": "^4.1.1",
"match-sorter": "^6.3.1",
Expand Down

0 comments on commit a26db9e

Please sign in to comment.