-
Notifications
You must be signed in to change notification settings - Fork 36
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
Update date-fns
to 2.29.3
#2310
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
import format from 'date-fns/format' | ||
import { DEFAULT_LANG } from '.' | ||
import formatDistanceToNow from 'date-fns/distance_in_words_to_now' | ||
import formatDistanceStrict from 'date-fns/distance_in_words_strict' | ||
import formatDistanceToNow from 'date-fns/formatDistanceToNow' | ||
import formatDistanceToNowStrict from 'date-fns/formatDistanceToNowStrict' | ||
|
||
const locales = {} | ||
let lang = DEFAULT_LANG | ||
let lang = DEFAULT_LANG === 'en' ? 'en-US' : DEFAULT_LANG | ||
|
||
const getWarningMessage = lang => | ||
`The "${lang}" locale isn't supported by date-fns. or has not been included in the build. Check if you have configured a ContextReplacementPlugin that is too restrictive.` | ||
|
||
export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => { | ||
lang = userLang | ||
const resolvedDefaultLang = defaultLang === 'en' ? 'en-US' : defaultLang | ||
const resolvedUserLang = userLang === 'en' ? 'en-US' : userLang | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be done for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. French and Spanish localizations have not been moved. They are still in |
||
try { | ||
locales[defaultLang] = require(`date-fns/locale/${defaultLang}/index.js`) | ||
locales[ | ||
resolvedDefaultLang | ||
] = require(`date-fns/locale/${resolvedDefaultLang}/index.js`) | ||
} catch (err) { | ||
console.warn(getWarningMessage(defaultLang)) | ||
console.warn(getWarningMessage(resolvedDefaultLang)) | ||
} | ||
|
||
if (lang && lang !== defaultLang) { | ||
if (resolvedUserLang && resolvedUserLang !== resolvedDefaultLang) { | ||
try { | ||
locales[lang] = require(`date-fns/locale/${lang}/index.js`) | ||
locales[ | ||
resolvedUserLang | ||
] = require(`date-fns/locale/${resolvedUserLang}/index.js`) | ||
} catch (e) { | ||
console.warn(getWarningMessage(lang)) | ||
console.warn(getWarningMessage(resolvedUserLang)) | ||
} | ||
} | ||
lang = resolvedUserLang | ||
return locales[lang] | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also means that the consuming app should change the call to |
||
export const initFormat = (userLang, defaultLang = DEFAULT_LANG) => ( | ||
date, | ||
formatStr | ||
) => { | ||
const dateObject = new Date(date) | ||
const locale = provideDateFnsLocale(userLang, defaultLang) | ||
return format(date, formatStr, { locale }) | ||
return format(dateObject, formatStr, { locale }) | ||
} | ||
|
||
export const formatLocallyDistanceToNow = date => | ||
formatDistanceToNow(date, { locale: locales[lang] }) | ||
export const formatLocallyDistanceToNow = date => { | ||
const dateObject = new Date(date) | ||
return formatDistanceToNow(dateObject, { locale: locales[lang] }) | ||
} | ||
|
||
export const formatLocallyDistanceToNowStrict = date => | ||
formatDistanceStrict(Date.now(), date, { locale: locales[lang] }) | ||
export const formatLocallyDistanceToNowStrict = date => { | ||
const dateObject = new Date(date) | ||
return formatDistanceToNowStrict(dateObject, { locale: locales[lang] }) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6460,12 +6460,12 @@ data-urls@^2.0.0: | |
whatwg-mimetype "^2.3.0" | ||
whatwg-url "^8.0.0" | ||
|
||
[email protected]: | ||
[email protected], date-fns@^2.29.3: | ||
version "2.29.3" | ||
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8" | ||
integrity sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA== | ||
|
||
date-fns@^1.28.5, date-fns@^1.30.1: | ||
date-fns@^1.30.1: | ||
version "1.30.1" | ||
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c" | ||
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw== | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't,
f
deal with this BC by itself? It it's a string, then create a new Date() by itself? In order to help the migration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is there any codemods that are helping to convert old date notation (ex
DDD MMM
) to the new one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is this table in the changelog of the lib that compares the old pattern with the new pattern : https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20 . There are sometimes multiple way to convert the pattern, depending on what we really expect.