cozy-ui 115.0.0 → 115.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [115.0.2](https://github.com/cozy/cozy-ui/compare/v115.0.1...v115.0.2) (2024-12-20)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **initFormat:** Ensure do not crash if no date is passed ([07564e7](https://github.com/cozy/cozy-ui/commit/07564e7))
|
|
7
|
+
|
|
8
|
+
## [115.0.1](https://github.com/cozy/cozy-ui/compare/v115.0.0...v115.0.1) (2024-12-20)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Ensure use correct format locales ([5d078db](https://github.com/cozy/cozy-ui/commit/5d078db)), closes [/github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20](https://github.com//github.com/date-fns/date-fns/blob/main/CHANGELOG.md/issues/200---2019-08-20)
|
|
14
|
+
|
|
1
15
|
# [115.0.0](https://github.com/cozy/cozy-ui/compare/v114.0.1...v115.0.0) (2024-12-19)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -6,18 +6,38 @@ import { DEFAULT_LANG } from '.'
|
|
|
6
6
|
const locales = {}
|
|
7
7
|
let lang = DEFAULT_LANG
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Ensure that the locale is in the correct format for date-fns (see 2.0.0 BC https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20)
|
|
11
|
+
* @param {string} lang
|
|
12
|
+
*/
|
|
13
|
+
const ensureLocaleFormat = lang => {
|
|
14
|
+
switch (lang) {
|
|
15
|
+
case 'en':
|
|
16
|
+
return 'en-US'
|
|
17
|
+
case 'zh_cn':
|
|
18
|
+
return 'zh-CN'
|
|
19
|
+
case 'zh_tw':
|
|
20
|
+
return 'zh-TW'
|
|
21
|
+
default:
|
|
22
|
+
return lang
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
9
26
|
const getWarningMessage = lang =>
|
|
10
27
|
`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.`
|
|
11
28
|
|
|
12
29
|
export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => {
|
|
13
|
-
lang = userLang
|
|
30
|
+
lang = ensureLocaleFormat(userLang)
|
|
31
|
+
const ensureDefaultLang = ensureLocaleFormat(defaultLang)
|
|
14
32
|
try {
|
|
15
|
-
locales[
|
|
33
|
+
locales[
|
|
34
|
+
ensureDefaultLang
|
|
35
|
+
] = require(`date-fns/locale/${ensureDefaultLang}/index.js`)
|
|
16
36
|
} catch (err) {
|
|
17
|
-
console.warn(getWarningMessage(
|
|
37
|
+
console.warn(getWarningMessage(ensureDefaultLang))
|
|
18
38
|
}
|
|
19
39
|
|
|
20
|
-
if (lang && lang !==
|
|
40
|
+
if (lang && lang !== ensureDefaultLang) {
|
|
21
41
|
try {
|
|
22
42
|
locales[lang] = require(`date-fns/locale/${lang}/index.js`)
|
|
23
43
|
} catch (e) {
|
|
@@ -33,7 +53,11 @@ export const initFormat =
|
|
|
33
53
|
const locale = provideDateFnsLocale(userLang, defaultLang)
|
|
34
54
|
const ensureDate = date && typeof date === 'string' ? new Date(date) : date
|
|
35
55
|
|
|
36
|
-
|
|
56
|
+
try {
|
|
57
|
+
return format(ensureDate, formatStr, { locale, ...opts })
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error('Error in initFormat', error)
|
|
60
|
+
}
|
|
37
61
|
}
|
|
38
62
|
|
|
39
63
|
export const formatLocallyDistanceToNow = date => {
|
|
@@ -10,6 +10,18 @@ describe('initFormat', () => {
|
|
|
10
10
|
it('should not throw if a date-fns locale can not be found', () => {
|
|
11
11
|
expect(() => initFormat('unknown-lang', 'unknown-default')).not.toThrow()
|
|
12
12
|
})
|
|
13
|
+
|
|
14
|
+
it('should use the correct locale', () => {
|
|
15
|
+
const f = initFormat('fr', 'en')
|
|
16
|
+
const date = f(0, 'yyyy-LLLL-dd')
|
|
17
|
+
expect(date).toBe('1970-janvier-01')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should fallback english if a date-fns locale can not be found', () => {
|
|
21
|
+
const f = initFormat('unknown-lang', 'unknown-default')
|
|
22
|
+
const date = f(0, 'yyyy-LLLL-dd')
|
|
23
|
+
expect(date).toBe('1970-January-01')
|
|
24
|
+
})
|
|
13
25
|
})
|
|
14
26
|
|
|
15
27
|
describe('formatLocallyDistanceToNow', () => {
|
|
@@ -9,6 +9,26 @@ import formatDistanceToNow from 'date-fns/formatDistanceToNow';
|
|
|
9
9
|
import { DEFAULT_LANG } from "cozy-ui/transpiled/react/providers/I18n";
|
|
10
10
|
var locales = {};
|
|
11
11
|
var lang = DEFAULT_LANG;
|
|
12
|
+
/**
|
|
13
|
+
* Ensure that the locale is in the correct format for date-fns (see 2.0.0 BC https://github.com/date-fns/date-fns/blob/main/CHANGELOG.md#200---2019-08-20)
|
|
14
|
+
* @param {string} lang
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
var ensureLocaleFormat = function ensureLocaleFormat(lang) {
|
|
18
|
+
switch (lang) {
|
|
19
|
+
case 'en':
|
|
20
|
+
return 'en-US';
|
|
21
|
+
|
|
22
|
+
case 'zh_cn':
|
|
23
|
+
return 'zh-CN';
|
|
24
|
+
|
|
25
|
+
case 'zh_tw':
|
|
26
|
+
return 'zh-TW';
|
|
27
|
+
|
|
28
|
+
default:
|
|
29
|
+
return lang;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
12
32
|
|
|
13
33
|
var getWarningMessage = function getWarningMessage(lang) {
|
|
14
34
|
return "The \"".concat(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.");
|
|
@@ -16,15 +36,16 @@ var getWarningMessage = function getWarningMessage(lang) {
|
|
|
16
36
|
|
|
17
37
|
export var provideDateFnsLocale = function provideDateFnsLocale(userLang) {
|
|
18
38
|
var defaultLang = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_LANG;
|
|
19
|
-
lang = userLang;
|
|
39
|
+
lang = ensureLocaleFormat(userLang);
|
|
40
|
+
var ensureDefaultLang = ensureLocaleFormat(defaultLang);
|
|
20
41
|
|
|
21
42
|
try {
|
|
22
|
-
locales[
|
|
43
|
+
locales[ensureDefaultLang] = require("date-fns/locale/".concat(ensureDefaultLang, "/index.js"));
|
|
23
44
|
} catch (err) {
|
|
24
|
-
console.warn(getWarningMessage(
|
|
45
|
+
console.warn(getWarningMessage(ensureDefaultLang));
|
|
25
46
|
}
|
|
26
47
|
|
|
27
|
-
if (lang && lang !==
|
|
48
|
+
if (lang && lang !== ensureDefaultLang) {
|
|
28
49
|
try {
|
|
29
50
|
locales[lang] = require("date-fns/locale/".concat(lang, "/index.js"));
|
|
30
51
|
} catch (e) {
|
|
@@ -40,9 +61,14 @@ export var initFormat = function initFormat(userLang) {
|
|
|
40
61
|
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
41
62
|
var locale = provideDateFnsLocale(userLang, defaultLang);
|
|
42
63
|
var ensureDate = date && typeof date === 'string' ? new Date(date) : date;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
return format(ensureDate, formatStr, _objectSpread({
|
|
67
|
+
locale: locale
|
|
68
|
+
}, opts));
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error('Error in initFormat', error);
|
|
71
|
+
}
|
|
46
72
|
};
|
|
47
73
|
};
|
|
48
74
|
export var formatLocallyDistanceToNow = function formatLocallyDistanceToNow(date) {
|