1
- import { parseISO } from 'date-fns' ;
2
- import { it , expect } from 'vitest' ;
3
- import { createFormatter } from '../../src' ;
1
+ import { parseISO } from 'date-fns' ;
2
+ import { it , expect } from 'vitest' ;
3
+ import { createFormatter } from '../../src' ;
4
4
5
5
it ( 'formats a date and time' , ( ) => {
6
6
const formatter = createFormatter ( {
7
7
locale : 'en' ,
8
- timeZone : 'Europe/Berlin'
8
+ timeZone : 'Europe/Berlin' ,
9
9
} ) ;
10
10
expect (
11
11
formatter . dateTime ( parseISO ( '2020-11-20T10:36:01.516Z' ) , {
12
- dateStyle : 'medium'
12
+ dateStyle : 'medium' ,
13
+ } )
14
+ ) . toBe ( 'Nov 20, 2020' ) ;
15
+ } ) ;
16
+
17
+ it ( 'formats a ISO 8601 datetime string' , ( ) => {
18
+ const formatter = createFormatter ( {
19
+ locale : 'en' ,
20
+ timeZone : 'Europe/Berlin' ,
21
+ } ) ;
22
+ expect (
23
+ formatter . dateTime ( '2020-11-20T10:36:01.516Z' , {
24
+ dateStyle : 'medium' ,
13
25
} )
14
26
) . toBe ( 'Nov 20, 2020' ) ;
15
27
} ) ;
16
28
17
29
it ( 'formats a number' , ( ) => {
18
30
const formatter = createFormatter ( {
19
31
locale : 'en' ,
20
- timeZone : 'Europe/Berlin'
32
+ timeZone : 'Europe/Berlin' ,
21
33
} ) ;
22
34
expect ( formatter . number ( 123456 ) ) . toBe ( '123,456' ) ;
23
35
} ) ;
24
36
25
37
it ( 'formats a bigint' , ( ) => {
26
38
const formatter = createFormatter ( {
27
39
locale : 'en' ,
28
- timeZone : 'Europe/Berlin'
40
+ timeZone : 'Europe/Berlin' ,
29
41
} ) ;
30
42
expect ( formatter . number ( 123456789123456789n ) ) . toBe ( '123,456,789,123,456,789' ) ;
31
43
} ) ;
32
44
33
45
it ( 'formats a number as currency' , ( ) => {
34
46
const formatter = createFormatter ( {
35
47
locale : 'en' ,
36
- timeZone : 'Europe/Berlin'
48
+ timeZone : 'Europe/Berlin' ,
37
49
} ) ;
38
50
expect (
39
- formatter . number ( 123456.789 , { style : 'currency' , currency : 'USD' } )
51
+ formatter . number ( 123456.789 , { style : 'currency' , currency : 'USD' } )
40
52
) . toBe ( '$123,456.79' ) ;
41
53
} ) ;
42
54
43
55
it ( 'formats a bigint as currency' , ( ) => {
44
56
const formatter = createFormatter ( {
45
57
locale : 'en' ,
46
- timeZone : 'Europe/Berlin'
58
+ timeZone : 'Europe/Berlin' ,
47
59
} ) ;
48
60
expect (
49
61
formatter . number ( 123456789123456789n , {
50
62
style : 'currency' ,
51
- currency : 'USD'
63
+ currency : 'USD' ,
52
64
} )
53
65
) . toBe ( '$123,456,789,123,456,789.00' ) ;
54
66
} ) ;
55
67
56
68
it ( 'formats a relative time with the second unit' , ( ) => {
57
69
const formatter = createFormatter ( {
58
70
locale : 'en' ,
59
- timeZone : 'Europe/Berlin'
71
+ timeZone : 'Europe/Berlin' ,
60
72
} ) ;
61
73
expect (
62
74
formatter . relativeTime (
@@ -69,7 +81,7 @@ it('formats a relative time with the second unit', () => {
69
81
it ( 'formats a relative time with the minute unit' , ( ) => {
70
82
const formatter = createFormatter ( {
71
83
locale : 'en' ,
72
- timeZone : 'Europe/Berlin'
84
+ timeZone : 'Europe/Berlin' ,
73
85
} ) ;
74
86
expect (
75
87
formatter . relativeTime (
@@ -82,7 +94,7 @@ it('formats a relative time with the minute unit', () => {
82
94
it ( 'formats a relative time with the hour unit' , ( ) => {
83
95
const formatter = createFormatter ( {
84
96
locale : 'en' ,
85
- timeZone : 'Europe/Berlin'
97
+ timeZone : 'Europe/Berlin' ,
86
98
} ) ;
87
99
expect (
88
100
formatter . relativeTime (
@@ -95,7 +107,7 @@ it('formats a relative time with the hour unit', () => {
95
107
it ( 'formats a relative time with the day unit' , ( ) => {
96
108
const formatter = createFormatter ( {
97
109
locale : 'en' ,
98
- timeZone : 'Europe/Berlin'
110
+ timeZone : 'Europe/Berlin' ,
99
111
} ) ;
100
112
expect (
101
113
formatter . relativeTime (
@@ -108,7 +120,7 @@ it('formats a relative time with the day unit', () => {
108
120
it ( 'formats a relative time with the month unit' , ( ) => {
109
121
const formatter = createFormatter ( {
110
122
locale : 'en' ,
111
- timeZone : 'Europe/Berlin'
123
+ timeZone : 'Europe/Berlin' ,
112
124
} ) ;
113
125
expect (
114
126
formatter . relativeTime (
@@ -121,7 +133,7 @@ it('formats a relative time with the month unit', () => {
121
133
it ( 'formats a relative time with the year unit' , ( ) => {
122
134
const formatter = createFormatter ( {
123
135
locale : 'en' ,
124
- timeZone : 'Europe/Berlin'
136
+ timeZone : 'Europe/Berlin' ,
125
137
} ) ;
126
138
expect (
127
139
formatter . relativeTime (
@@ -134,7 +146,7 @@ it('formats a relative time with the year unit', () => {
134
146
it ( 'supports the future relative time' , ( ) => {
135
147
const formatter = createFormatter ( {
136
148
locale : 'en' ,
137
- timeZone : 'Europe/Berlin'
149
+ timeZone : 'Europe/Berlin' ,
138
150
} ) ;
139
151
expect (
140
152
formatter . relativeTime (
@@ -147,25 +159,25 @@ it('supports the future relative time', () => {
147
159
it ( 'formats a relative time with options' , ( ) => {
148
160
const formatter = createFormatter ( {
149
161
locale : 'en' ,
150
- timeZone : 'Europe/Berlin'
162
+ timeZone : 'Europe/Berlin' ,
151
163
} ) ;
152
164
expect (
153
165
formatter . relativeTime ( parseISO ( '2020-11-20T08:30:00.000Z' ) , {
154
166
now : parseISO ( '2020-11-20T10:36:00.000Z' ) ,
155
- unit : 'day'
167
+ unit : 'day' ,
156
168
} )
157
169
) . toBe ( 'today' ) ;
158
170
} ) ;
159
171
160
172
it ( 'supports the quarter unit' , ( ) => {
161
173
const formatter = createFormatter ( {
162
174
locale : 'en' ,
163
- timeZone : 'Europe/Berlin'
175
+ timeZone : 'Europe/Berlin' ,
164
176
} ) ;
165
177
expect (
166
178
formatter . relativeTime ( parseISO ( '2020-01-01T00:00:00.000Z' ) , {
167
179
now : parseISO ( '2020-11-01T01:00:00.000Z' ) ,
168
- unit : 'quarter'
180
+ unit : 'quarter' ,
169
181
} )
170
182
) . toBe ( '3 quarters ago' ) ;
171
183
} ) ;
@@ -174,33 +186,33 @@ it('formats a relative time with a globally defined `now`', () => {
174
186
const formatter = createFormatter ( {
175
187
locale : 'en' ,
176
188
now : parseISO ( '2020-11-20T01:00:00.000Z' ) ,
177
- timeZone : 'Europe/Berlin'
189
+ timeZone : 'Europe/Berlin' ,
178
190
} ) ;
179
191
expect (
180
192
formatter . relativeTime ( parseISO ( '2020-11-20T00:00:00.000Z' ) , {
181
- unit : 'day'
193
+ unit : 'day' ,
182
194
} )
183
195
) . toBe ( 'today' ) ;
184
196
} ) ;
185
197
186
198
it ( 'formats a list' , ( ) => {
187
199
const formatter = createFormatter ( {
188
200
locale : 'en' ,
189
- timeZone : 'Europe/Berlin'
201
+ timeZone : 'Europe/Berlin' ,
190
202
} ) ;
191
203
expect (
192
- formatter . list ( [ 'apple' , 'banana' , 'orange' ] , { type : 'disjunction' } )
204
+ formatter . list ( [ 'apple' , 'banana' , 'orange' ] , { type : 'disjunction' } )
193
205
) . toBe ( 'apple, banana, or orange' ) ;
194
206
} ) ;
195
207
196
208
it ( 'formats a set' , ( ) => {
197
209
const formatter = createFormatter ( {
198
210
locale : 'en' ,
199
- timeZone : 'Europe/Berlin'
211
+ timeZone : 'Europe/Berlin' ,
200
212
} ) ;
201
213
expect (
202
214
formatter . list ( new Set ( [ 'apple' , 'banana' , 'orange' ] ) , {
203
- type : 'disjunction'
215
+ type : 'disjunction' ,
204
216
} )
205
217
) . toBe ( 'apple, banana, or orange' ) ;
206
218
} ) ;
0 commit comments