@contentful/field-editor-rich-text 6.2.1 → 6.3.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/dist/cjs/helpers/formatDateAndTime.js +9 -12
- package/dist/cjs/helpers/formatDateAndTime.spec.js +71 -0
- package/dist/esm/helpers/formatDateAndTime.js +9 -7
- package/dist/esm/helpers/formatDateAndTime.spec.js +67 -0
- package/dist/types/helpers/formatDateAndTime.d.ts +1 -1
- package/dist/types/helpers/formatDateAndTime.spec.d.ts +1 -0
- package/package.json +4 -4
|
@@ -19,26 +19,23 @@ _export(exports, {
|
|
|
19
19
|
return formatTime;
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
|
-
const
|
|
23
|
-
function _interop_require_default(obj) {
|
|
24
|
-
return obj && obj.__esModule ? obj : {
|
|
25
|
-
default: obj
|
|
26
|
-
};
|
|
27
|
-
}
|
|
22
|
+
const _datefns = require("date-fns");
|
|
28
23
|
const formatDate = (date, short)=>{
|
|
29
|
-
|
|
24
|
+
const d = new Date(date);
|
|
25
|
+
const diff = (0, _datefns.differenceInCalendarDays)((0, _datefns.startOfDay)(new Date()), (0, _datefns.startOfDay)(d));
|
|
26
|
+
switch(diff){
|
|
30
27
|
case 0:
|
|
31
|
-
return short ? 'Today' : `Today, ${(0,
|
|
28
|
+
return short ? 'Today' : `Today, ${(0, _datefns.format)(d, 'dd MMM yyyy')}`;
|
|
32
29
|
case -1:
|
|
33
|
-
return short ? 'Tomorrow' : `Tomorrow, ${(0,
|
|
30
|
+
return short ? 'Tomorrow' : `Tomorrow, ${(0, _datefns.format)(d, 'dd MMM yyyy')}`;
|
|
34
31
|
case 1:
|
|
35
|
-
return short ? 'Yesterday' : `Yesterday, ${(0,
|
|
32
|
+
return short ? 'Yesterday' : `Yesterday, ${(0, _datefns.format)(d, 'dd MMM yyyy')}`;
|
|
36
33
|
default:
|
|
37
|
-
return (0,
|
|
34
|
+
return (0, _datefns.format)(d, 'eee, dd MMM yyyy');
|
|
38
35
|
}
|
|
39
36
|
};
|
|
40
37
|
const formatTime = (date)=>{
|
|
41
|
-
return
|
|
38
|
+
return (0, _datefns.format)(new Date(date), 'h:mm a');
|
|
42
39
|
};
|
|
43
40
|
const formatDateAndTime = (date, short)=>{
|
|
44
41
|
return `${formatDate(date, short)} at ${formatTime(date)}`;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _formatDateAndTime = require("./formatDateAndTime");
|
|
6
|
+
const TODAY = new Date('2024-06-15T12:00:00.000Z');
|
|
7
|
+
beforeEach(()=>{
|
|
8
|
+
jest.useFakeTimers();
|
|
9
|
+
jest.setSystemTime(TODAY);
|
|
10
|
+
});
|
|
11
|
+
afterEach(()=>{
|
|
12
|
+
jest.useRealTimers();
|
|
13
|
+
});
|
|
14
|
+
describe('formatDate', ()=>{
|
|
15
|
+
it('returns "Today" (short) for a date on the same calendar day', ()=>{
|
|
16
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-15T08:00:00.000Z', true)).toBe('Today');
|
|
17
|
+
});
|
|
18
|
+
it('returns long form for today', ()=>{
|
|
19
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-15T08:00:00.000Z')).toBe('Today, 15 Jun 2024');
|
|
20
|
+
});
|
|
21
|
+
it('returns "Tomorrow" (short) for the next calendar day', ()=>{
|
|
22
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-16T08:00:00.000Z', true)).toBe('Tomorrow');
|
|
23
|
+
});
|
|
24
|
+
it('returns long form for tomorrow', ()=>{
|
|
25
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-16T08:00:00.000Z')).toBe('Tomorrow, 16 Jun 2024');
|
|
26
|
+
});
|
|
27
|
+
it('returns "Yesterday" (short) for the previous calendar day', ()=>{
|
|
28
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-14T08:00:00.000Z', true)).toBe('Yesterday');
|
|
29
|
+
});
|
|
30
|
+
it('returns long form for yesterday', ()=>{
|
|
31
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-14T08:00:00.000Z')).toBe('Yesterday, 14 Jun 2024');
|
|
32
|
+
});
|
|
33
|
+
it('returns weekday + date for a past date beyond yesterday', ()=>{
|
|
34
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-01T08:00:00.000Z')).toBe('Sat, 01 Jun 2024');
|
|
35
|
+
});
|
|
36
|
+
it('returns weekday + date for a future date beyond tomorrow', ()=>{
|
|
37
|
+
expect((0, _formatDateAndTime.formatDate)('2024-06-20T08:00:00.000Z')).toBe('Thu, 20 Jun 2024');
|
|
38
|
+
});
|
|
39
|
+
it('accepts a Date object', ()=>{
|
|
40
|
+
expect((0, _formatDateAndTime.formatDate)(new Date('2024-06-15T09:30:00.000Z'), true)).toBe('Today');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('formatTime', ()=>{
|
|
44
|
+
it('formats a UTC ISO string to local h:mm AM/PM', ()=>{
|
|
45
|
+
expect((0, _formatDateAndTime.formatTime)('2024-06-15T15:36:45.000Z')).toBe('3:36 PM');
|
|
46
|
+
});
|
|
47
|
+
it('formats midnight UTC correctly', ()=>{
|
|
48
|
+
expect((0, _formatDateAndTime.formatTime)('2024-06-15T00:00:00.000Z')).toBe('12:00 AM');
|
|
49
|
+
});
|
|
50
|
+
it('formats noon UTC correctly', ()=>{
|
|
51
|
+
expect((0, _formatDateAndTime.formatTime)('2024-06-15T12:00:00.000Z')).toBe('12:00 PM');
|
|
52
|
+
});
|
|
53
|
+
it('timezone shift: two UTC times 1 hour apart produce different outputs', ()=>{
|
|
54
|
+
expect((0, _formatDateAndTime.formatTime)('2024-06-15T10:00:00.000Z')).toBe('10:00 AM');
|
|
55
|
+
expect((0, _formatDateAndTime.formatTime)('2024-06-15T11:00:00.000Z')).toBe('11:00 AM');
|
|
56
|
+
});
|
|
57
|
+
it('accepts a Date object', ()=>{
|
|
58
|
+
expect((0, _formatDateAndTime.formatTime)(new Date('2024-06-15T15:36:45.000Z'))).toBe('3:36 PM');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe('formatDateAndTime', ()=>{
|
|
62
|
+
it('combines date and time with " at " separator', ()=>{
|
|
63
|
+
expect((0, _formatDateAndTime.formatDateAndTime)('2024-06-15T15:36:45.000Z')).toBe('Today, 15 Jun 2024 at 3:36 PM');
|
|
64
|
+
});
|
|
65
|
+
it('passes short flag through to date portion', ()=>{
|
|
66
|
+
expect((0, _formatDateAndTime.formatDateAndTime)('2024-06-15T15:36:45.000Z', true)).toBe('Today at 3:36 PM');
|
|
67
|
+
});
|
|
68
|
+
it('works for a past date', ()=>{
|
|
69
|
+
expect((0, _formatDateAndTime.formatDateAndTime)('2024-06-01T08:00:00.000Z')).toBe('Sat, 01 Jun 2024 at 8:00 AM');
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { differenceInCalendarDays, format, startOfDay } from 'date-fns';
|
|
2
2
|
export const formatDate = (date, short)=>{
|
|
3
|
-
|
|
3
|
+
const d = new Date(date);
|
|
4
|
+
const diff = differenceInCalendarDays(startOfDay(new Date()), startOfDay(d));
|
|
5
|
+
switch(diff){
|
|
4
6
|
case 0:
|
|
5
|
-
return short ? 'Today' : `Today, ${
|
|
7
|
+
return short ? 'Today' : `Today, ${format(d, 'dd MMM yyyy')}`;
|
|
6
8
|
case -1:
|
|
7
|
-
return short ? 'Tomorrow' : `Tomorrow, ${
|
|
9
|
+
return short ? 'Tomorrow' : `Tomorrow, ${format(d, 'dd MMM yyyy')}`;
|
|
8
10
|
case 1:
|
|
9
|
-
return short ? 'Yesterday' : `Yesterday, ${
|
|
11
|
+
return short ? 'Yesterday' : `Yesterday, ${format(d, 'dd MMM yyyy')}`;
|
|
10
12
|
default:
|
|
11
|
-
return
|
|
13
|
+
return format(d, 'eee, dd MMM yyyy');
|
|
12
14
|
}
|
|
13
15
|
};
|
|
14
16
|
export const formatTime = (date)=>{
|
|
15
|
-
return
|
|
17
|
+
return format(new Date(date), 'h:mm a');
|
|
16
18
|
};
|
|
17
19
|
export const formatDateAndTime = (date, short)=>{
|
|
18
20
|
return `${formatDate(date, short)} at ${formatTime(date)}`;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { formatDate, formatDateAndTime, formatTime } from './formatDateAndTime';
|
|
2
|
+
const TODAY = new Date('2024-06-15T12:00:00.000Z');
|
|
3
|
+
beforeEach(()=>{
|
|
4
|
+
jest.useFakeTimers();
|
|
5
|
+
jest.setSystemTime(TODAY);
|
|
6
|
+
});
|
|
7
|
+
afterEach(()=>{
|
|
8
|
+
jest.useRealTimers();
|
|
9
|
+
});
|
|
10
|
+
describe('formatDate', ()=>{
|
|
11
|
+
it('returns "Today" (short) for a date on the same calendar day', ()=>{
|
|
12
|
+
expect(formatDate('2024-06-15T08:00:00.000Z', true)).toBe('Today');
|
|
13
|
+
});
|
|
14
|
+
it('returns long form for today', ()=>{
|
|
15
|
+
expect(formatDate('2024-06-15T08:00:00.000Z')).toBe('Today, 15 Jun 2024');
|
|
16
|
+
});
|
|
17
|
+
it('returns "Tomorrow" (short) for the next calendar day', ()=>{
|
|
18
|
+
expect(formatDate('2024-06-16T08:00:00.000Z', true)).toBe('Tomorrow');
|
|
19
|
+
});
|
|
20
|
+
it('returns long form for tomorrow', ()=>{
|
|
21
|
+
expect(formatDate('2024-06-16T08:00:00.000Z')).toBe('Tomorrow, 16 Jun 2024');
|
|
22
|
+
});
|
|
23
|
+
it('returns "Yesterday" (short) for the previous calendar day', ()=>{
|
|
24
|
+
expect(formatDate('2024-06-14T08:00:00.000Z', true)).toBe('Yesterday');
|
|
25
|
+
});
|
|
26
|
+
it('returns long form for yesterday', ()=>{
|
|
27
|
+
expect(formatDate('2024-06-14T08:00:00.000Z')).toBe('Yesterday, 14 Jun 2024');
|
|
28
|
+
});
|
|
29
|
+
it('returns weekday + date for a past date beyond yesterday', ()=>{
|
|
30
|
+
expect(formatDate('2024-06-01T08:00:00.000Z')).toBe('Sat, 01 Jun 2024');
|
|
31
|
+
});
|
|
32
|
+
it('returns weekday + date for a future date beyond tomorrow', ()=>{
|
|
33
|
+
expect(formatDate('2024-06-20T08:00:00.000Z')).toBe('Thu, 20 Jun 2024');
|
|
34
|
+
});
|
|
35
|
+
it('accepts a Date object', ()=>{
|
|
36
|
+
expect(formatDate(new Date('2024-06-15T09:30:00.000Z'), true)).toBe('Today');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe('formatTime', ()=>{
|
|
40
|
+
it('formats a UTC ISO string to local h:mm AM/PM', ()=>{
|
|
41
|
+
expect(formatTime('2024-06-15T15:36:45.000Z')).toBe('3:36 PM');
|
|
42
|
+
});
|
|
43
|
+
it('formats midnight UTC correctly', ()=>{
|
|
44
|
+
expect(formatTime('2024-06-15T00:00:00.000Z')).toBe('12:00 AM');
|
|
45
|
+
});
|
|
46
|
+
it('formats noon UTC correctly', ()=>{
|
|
47
|
+
expect(formatTime('2024-06-15T12:00:00.000Z')).toBe('12:00 PM');
|
|
48
|
+
});
|
|
49
|
+
it('timezone shift: two UTC times 1 hour apart produce different outputs', ()=>{
|
|
50
|
+
expect(formatTime('2024-06-15T10:00:00.000Z')).toBe('10:00 AM');
|
|
51
|
+
expect(formatTime('2024-06-15T11:00:00.000Z')).toBe('11:00 AM');
|
|
52
|
+
});
|
|
53
|
+
it('accepts a Date object', ()=>{
|
|
54
|
+
expect(formatTime(new Date('2024-06-15T15:36:45.000Z'))).toBe('3:36 PM');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('formatDateAndTime', ()=>{
|
|
58
|
+
it('combines date and time with " at " separator', ()=>{
|
|
59
|
+
expect(formatDateAndTime('2024-06-15T15:36:45.000Z')).toBe('Today, 15 Jun 2024 at 3:36 PM');
|
|
60
|
+
});
|
|
61
|
+
it('passes short flag through to date portion', ()=>{
|
|
62
|
+
expect(formatDateAndTime('2024-06-15T15:36:45.000Z', true)).toBe('Today at 3:36 PM');
|
|
63
|
+
});
|
|
64
|
+
it('works for a past date', ()=>{
|
|
65
|
+
expect(formatDateAndTime('2024-06-01T08:00:00.000Z')).toBe('Sat, 01 Jun 2024 at 8:00 AM');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type DateFormatFn = (date: Date | string, short?: boolean) => string;
|
|
2
2
|
/**
|
|
3
|
-
* @param {Date|string} date A valid
|
|
3
|
+
* @param {Date|string} date A valid Date object or ISO string
|
|
4
4
|
* @param {boolean=} short Render only Today/Tomorrow/Yesterday if valid. Defaults to false
|
|
5
5
|
*/
|
|
6
6
|
export declare const formatDate: DateFormatFn;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-rich-text",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.0",
|
|
4
4
|
"source": "./src/index.tsx",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@contentful/f36-icons": "^6.7.1",
|
|
46
46
|
"@contentful/f36-tokens": "^6.1.2",
|
|
47
47
|
"@contentful/f36-utils": "^6.1.0",
|
|
48
|
-
"@contentful/field-editor-reference": "^8.
|
|
48
|
+
"@contentful/field-editor-reference": "^8.2.0",
|
|
49
49
|
"@contentful/field-editor-shared": "^4.1.0",
|
|
50
50
|
"@contentful/rich-text-plain-text-renderer": "^17.0.0",
|
|
51
51
|
"@contentful/rich-text-types": "^17.2.5",
|
|
@@ -63,11 +63,11 @@
|
|
|
63
63
|
"@udecode/plate-table": "36.5.9",
|
|
64
64
|
"@udecode/plate-trailing-block": "36.0.0",
|
|
65
65
|
"constate": "^3.3.2",
|
|
66
|
+
"date-fns": "^2.30.0",
|
|
66
67
|
"fast-deep-equal": "^3.1.3",
|
|
67
68
|
"is-hotkey": "^0.2.0",
|
|
68
69
|
"is-plain-obj": "^3.0.0",
|
|
69
70
|
"lodash": "^4.17.21",
|
|
70
|
-
"moment": "^2.20.0",
|
|
71
71
|
"p-debounce": "^2.1.0",
|
|
72
72
|
"react-popper": "^2.3.0",
|
|
73
73
|
"slate": "0.94.1",
|
|
@@ -94,5 +94,5 @@
|
|
|
94
94
|
"publishConfig": {
|
|
95
95
|
"registry": "https://npm.pkg.github.com/"
|
|
96
96
|
},
|
|
97
|
-
"gitHead": "
|
|
97
|
+
"gitHead": "920695a78a03f560d41d4dd06793e9980f0a3de0"
|
|
98
98
|
}
|