cozy-ui 79.0.0 → 79.1.0

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,10 @@
1
+ # [79.1.0](https://github.com/cozy/cozy-ui/compare/v79.0.0...v79.1.0) (2022-12-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * Add a `formatLocallyDistanceToNowStrict` helper ([ff6b565](https://github.com/cozy/cozy-ui/commit/ff6b565))
7
+
1
8
  # [79.0.0](https://github.com/cozy/cozy-ui/compare/v78.1.1...v79.0.0) (2022-12-15)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui",
3
- "version": "79.0.0",
3
+ "version": "79.1.0",
4
4
  "description": "Cozy apps UI SDK",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -1,6 +1,7 @@
1
1
  import format from 'date-fns/format'
2
2
  import { DEFAULT_LANG } from '.'
3
3
  import formatDistanceToNow from 'date-fns/distance_in_words_to_now'
4
+ import formatDistanceStrict from 'date-fns/distance_in_words_strict'
4
5
 
5
6
  const locales = {}
6
7
  let lang = DEFAULT_LANG
@@ -36,3 +37,6 @@ export const initFormat = (userLang, defaultLang = DEFAULT_LANG) => (
36
37
 
37
38
  export const formatLocallyDistanceToNow = date =>
38
39
  formatDistanceToNow(date, { locale: locales[lang] })
40
+
41
+ export const formatLocallyDistanceToNowStrict = date =>
42
+ formatDistanceStrict(Date.now(), date, { locale: locales[lang] })
@@ -1,4 +1,8 @@
1
- import { initFormat, formatLocallyDistanceToNow } from './format'
1
+ import {
2
+ initFormat,
3
+ formatLocallyDistanceToNow,
4
+ formatLocallyDistanceToNowStrict
5
+ } from './format'
2
6
 
3
7
  describe('initFormat', () => {
4
8
  beforeEach(() => {
@@ -14,7 +18,7 @@ describe('initFormat', () => {
14
18
 
15
19
  describe('formatLocallyDistanceToNow', () => {
16
20
  it('should formatDistanceToNow with small value', () => {
17
- const date = Date.now() + 29 * 1000
21
+ const date = Date.now() + 29 * 1000 // 29s
18
22
 
19
23
  const result = formatLocallyDistanceToNow(date)
20
24
 
@@ -22,7 +26,7 @@ describe('formatLocallyDistanceToNow', () => {
22
26
  })
23
27
 
24
28
  it('should formatDistanceToNow with medium value', () => {
25
- const date = Date.now() + 2671 * 1000
29
+ const date = Date.now() + (44 * 60 + 31) * 1000 // 44min 31s
26
30
 
27
31
  const result = formatLocallyDistanceToNow(date)
28
32
 
@@ -30,18 +34,52 @@ describe('formatLocallyDistanceToNow', () => {
30
34
  })
31
35
 
32
36
  it('should formatDistanceToNow with high value', () => {
33
- const date = Date.now() + 5371 * 1000
37
+ const date = Date.now() + (89 * 60 + 31) * 1000 // 89min 31s
34
38
 
35
39
  const result = formatLocallyDistanceToNow(date)
36
40
 
37
41
  expect(result).toEqual('about 2 hours')
38
42
  })
39
43
 
40
- it('should not throw if a date-fns locale can not be found', () => {
41
- jest.spyOn(console, 'warn').mockImplementation()
44
+ it('should formatDistanceToNow with very high value', () => {
45
+ const date = Date.now() + 42 * 24 * 60 * 60 * 1000 // 42d
42
46
 
43
- expect(() => formatLocallyDistanceToNow('unknown-lang')).not.toThrow()
47
+ const result = formatLocallyDistanceToNow(date)
44
48
 
45
- console.warn.mockRestore()
49
+ expect(result).toEqual('about 1 month')
50
+ })
51
+ })
52
+
53
+ describe('formatLocallyDistanceToNowStrict', () => {
54
+ it('should formatDistanceToNowStrict with small value', () => {
55
+ const date = Date.now() + 29 * 1000 // 29s
56
+
57
+ const result = formatLocallyDistanceToNowStrict(date)
58
+
59
+ expect(result).toEqual('29 seconds')
60
+ })
61
+
62
+ it('should formatDistanceToNowStrict with medium value', () => {
63
+ const date = Date.now() + (44 * 60 + 31) * 1000 // 44min 31s
64
+
65
+ const result = formatLocallyDistanceToNowStrict(date)
66
+
67
+ expect(result).toEqual('44 minutes')
68
+ })
69
+
70
+ it('should formatDistanceToNowStrict with high value', () => {
71
+ const date = Date.now() + (89 * 60 + 31) * 1000 // 89min 31s
72
+
73
+ const result = formatLocallyDistanceToNowStrict(date)
74
+
75
+ expect(result).toEqual('1 hour')
76
+ })
77
+
78
+ it('should formatDistanceToNowStrict with very high value', () => {
79
+ const date = Date.now() + 42 * 24 * 60 * 60 * 1000 // 42d
80
+
81
+ const result = formatLocallyDistanceToNowStrict(date)
82
+
83
+ expect(result).toEqual('1 month')
46
84
  })
47
85
  })
@@ -9,7 +9,7 @@ import Link from '../../Link'
9
9
  import Typography from '../../Typography'
10
10
  import { withViewerLocales } from '../hoc/withViewerLocales'
11
11
  import { useI18n } from '../../I18n'
12
- import { formatLocallyDistanceToNow } from '../../I18n/format'
12
+ import { formatLocallyDistanceToNowStrict } from '../../I18n/format'
13
13
 
14
14
  const FILES_DOCTYPE = 'io.cozy.files'
15
15
 
@@ -51,7 +51,7 @@ const ExpirationAlert = ({ file }) => {
51
51
  <Typography component="span" variant="inherit">
52
52
  <Typography component="span" variant="inherit">
53
53
  {t('Viewer.panel.expiration.description', {
54
- duration: formatLocallyDistanceToNow(expirationDate)
54
+ duration: formatLocallyDistanceToNowStrict(expirationDate)
55
55
  })}
56
56
  </Typography>
57
57
  {expirationNoticeLink && (
@@ -5,7 +5,7 @@ import { models } from 'cozy-client'
5
5
 
6
6
  import Typography from '../../Typography'
7
7
  import { useI18n } from '../../I18n'
8
- import { formatLocallyDistanceToNow } from '../../I18n/format'
8
+ import { formatLocallyDistanceToNowStrict } from '../../I18n/format'
9
9
 
10
10
  const { computeExpirationDate, isExpired } = models.paper
11
11
 
@@ -25,7 +25,7 @@ const ExpirationAnnotation = ({ file }) => {
25
25
  return (
26
26
  <Typography component="span" variant="inherit" className="u-warning">
27
27
  {t('Viewer.panel.qualification.expiresIn', {
28
- duration: formatLocallyDistanceToNow(expirationDate)
28
+ duration: formatLocallyDistanceToNowStrict(expirationDate)
29
29
  })}
30
30
  </Typography>
31
31
  )
@@ -1,6 +1,7 @@
1
1
  import format from 'date-fns/format';
2
2
  import { DEFAULT_LANG } from "cozy-ui/transpiled/react/I18n";
3
3
  import formatDistanceToNow from 'date-fns/distance_in_words_to_now';
4
+ import formatDistanceStrict from 'date-fns/distance_in_words_strict';
4
5
  var locales = {};
5
6
  var lang = DEFAULT_LANG;
6
7
 
@@ -41,4 +42,9 @@ export var formatLocallyDistanceToNow = function formatLocallyDistanceToNow(date
41
42
  return formatDistanceToNow(date, {
42
43
  locale: locales[lang]
43
44
  });
45
+ };
46
+ export var formatLocallyDistanceToNowStrict = function formatLocallyDistanceToNowStrict(date) {
47
+ return formatDistanceStrict(Date.now(), date, {
48
+ locale: locales[lang]
49
+ });
44
50
  };
@@ -16,7 +16,7 @@ import Link from "cozy-ui/transpiled/react/Link";
16
16
  import Typography from "cozy-ui/transpiled/react/Typography";
17
17
  import { withViewerLocales } from "cozy-ui/transpiled/react/Viewer/hoc/withViewerLocales";
18
18
  import { useI18n } from "cozy-ui/transpiled/react/I18n";
19
- import { formatLocallyDistanceToNow } from "cozy-ui/transpiled/react/I18n/format";
19
+ import { formatLocallyDistanceToNowStrict } from "cozy-ui/transpiled/react/I18n/format";
20
20
  var FILES_DOCTYPE = 'io.cozy.files';
21
21
  var _models$paper = models.paper,
22
22
  computeExpirationDate = _models$paper.computeExpirationDate,
@@ -84,7 +84,7 @@ var ExpirationAlert = function ExpirationAlert(_ref) {
84
84
  component: "span",
85
85
  variant: "inherit"
86
86
  }, t('Viewer.panel.expiration.description', {
87
- duration: formatLocallyDistanceToNow(expirationDate)
87
+ duration: formatLocallyDistanceToNowStrict(expirationDate)
88
88
  })), expirationNoticeLink && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Typography, {
89
89
  component: "span",
90
90
  variant: "inherit"
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
3
3
  import { models } from 'cozy-client';
4
4
  import Typography from "cozy-ui/transpiled/react/Typography";
5
5
  import { useI18n } from "cozy-ui/transpiled/react/I18n";
6
- import { formatLocallyDistanceToNow } from "cozy-ui/transpiled/react/I18n/format";
6
+ import { formatLocallyDistanceToNowStrict } from "cozy-ui/transpiled/react/I18n/format";
7
7
  var _models$paper = models.paper,
8
8
  computeExpirationDate = _models$paper.computeExpirationDate,
9
9
  isExpired = _models$paper.isExpired;
@@ -28,7 +28,7 @@ var ExpirationAnnotation = function ExpirationAnnotation(_ref) {
28
28
  variant: "inherit",
29
29
  className: "u-warning"
30
30
  }, t('Viewer.panel.qualification.expiresIn', {
31
- duration: formatLocallyDistanceToNow(expirationDate)
31
+ duration: formatLocallyDistanceToNowStrict(expirationDate)
32
32
  }));
33
33
  };
34
34