@zohodesk/i18n 1.0.0-beta.22 → 1.0.0-beta.24
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/README.md +97 -89
- package/es/components/I18N.js +1 -0
- package/es/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/es/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/es/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/es/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/es/index.js +1 -1
- package/es/utils.js +62 -9
- package/lib/components/I18N.js +1 -0
- package/lib/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/lib/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/lib/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/lib/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/lib/index.js +18 -0
- package/lib/utils.js +65 -9
- package/package.json +29 -29
- package/src/I18NContext.js +2 -2
- package/src/components/DateTimeDiffFormat.js +256 -256
- package/src/components/FormatText.js +14 -14
- package/src/components/HOCI18N.js +37 -37
- package/src/components/I18N.js +74 -73
- package/src/components/I18NProvider.js +110 -110
- package/src/components/PluralFormat.js +37 -37
- package/src/components/UserTimeDiffFormat.js +97 -97
- package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -618
- package/src/components/__tests__/FormatText.spec.js +26 -26
- package/src/components/__tests__/HOCI18N.spec.js +33 -33
- package/src/components/__tests__/I18N.spec.js +29 -29
- package/src/components/__tests__/I18NProvider.spec.js +65 -65
- package/src/components/__tests__/PluralFormat.spec.js +27 -27
- package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -1076
- package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/src/index.js +36 -33
- package/src/utils.js +585 -527
package/src/utils.js
CHANGED
|
@@ -1,527 +1,585 @@
|
|
|
1
|
-
import datetime from '@zohodesk/datetimejs';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
let
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
)
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
format
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
let
|
|
342
|
-
|
|
343
|
-
let
|
|
344
|
-
if (
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
1
|
+
import datetime from '@zohodesk/datetimejs';
|
|
2
|
+
import {
|
|
3
|
+
i18NProviderUtils
|
|
4
|
+
} from './components/I18NProvider';
|
|
5
|
+
|
|
6
|
+
export const dayi18n = {
|
|
7
|
+
'Sun': 'deskreact.calendar.daynameshort.sunday',
|
|
8
|
+
'Mon': 'deskreact.calendar.daynameshort.monday',
|
|
9
|
+
'Tue': 'deskreact.calendar.daynameshort.tuesday',
|
|
10
|
+
'Wed': 'deskreact.calendar.daynameshort.wednesday',
|
|
11
|
+
'Thu': 'deskreact.calendar.daynameshort.thursday',
|
|
12
|
+
'Fri': 'deskreact.calendar.daynameshort.friday',
|
|
13
|
+
'Sat': 'deskreact.calendar.daynameshort.saturday',
|
|
14
|
+
'Sunday': 'deskreact.calendar.dayname.sunday',
|
|
15
|
+
'Monday': 'deskreact.calendar.dayname.monday',
|
|
16
|
+
'Tuesday': 'deskreact.calendar.dayname.tuesday',
|
|
17
|
+
'Wednesday': 'deskreact.calendar.dayname.wednesday',
|
|
18
|
+
'Thursday': 'deskreact.calendar.dayname.thursday',
|
|
19
|
+
'Friday': 'deskreact.calendar.dayname.friday',
|
|
20
|
+
'Saturday': 'deskreact.calendar.dayname.saturday'
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const monthi18n = {
|
|
24
|
+
'Jan': 'deskreact.calendar.monthnameshort.january',
|
|
25
|
+
'Feb': 'deskreact.calendar.monthnameshort.february',
|
|
26
|
+
'Mar': 'deskreact.calendar.monthnameshort.march',
|
|
27
|
+
'Apr': 'deskreact.calendar.monthnameshort.april',
|
|
28
|
+
'May': 'deskreact.calendar.monthnameshort.may',
|
|
29
|
+
'Jun': 'deskreact.calendar.monthnameshort.june',
|
|
30
|
+
'Jul': 'deskreact.calendar.monthnameshort.july',
|
|
31
|
+
'Aug': 'deskreact.calendar.monthnameshort.august',
|
|
32
|
+
'Sep': 'deskreact.calendar.monthnameshort.september',
|
|
33
|
+
'Oct': 'deskreact.calendar.monthnameshort.october',
|
|
34
|
+
'Nov': 'deskreact.calendar.monthnameshort.november',
|
|
35
|
+
'Dec': 'deskreact.calendar.monthnameshort.december',
|
|
36
|
+
'January': 'deskreact.calendar.monthname.january',
|
|
37
|
+
'February': 'deskreact.calendar.monthname.february',
|
|
38
|
+
'March': 'deskreact.calendar.monthname.march',
|
|
39
|
+
'April': 'deskreact.calendar.monthname.april',
|
|
40
|
+
'May': 'deskreact.calendar.monthname.may',
|
|
41
|
+
'June': 'deskreact.calendar.monthname.june',
|
|
42
|
+
'July': 'deskreact.calendar.monthname.July',
|
|
43
|
+
'August': 'deskreact.calendar.monthname.august',
|
|
44
|
+
'September': 'deskreact.calendar.monthname.september',
|
|
45
|
+
'October': 'deskreact.calendar.monthname.october',
|
|
46
|
+
'November': 'deskreact.calendar.monthname.november',
|
|
47
|
+
'December': 'deskreact.calendar.monthname.december'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const timei18n = {
|
|
51
|
+
'a': 'support.time.format.time.am.short.small',
|
|
52
|
+
'p': 'support.time.format.time.pm.short.small',
|
|
53
|
+
'am': 'support.time.format.time.am.small',
|
|
54
|
+
'pm': 'support.time.format.time.pm.small',
|
|
55
|
+
'A': 'support.time.format.time.am.short',
|
|
56
|
+
'P': 'support.time.format.time.pm.short',
|
|
57
|
+
'AM': 'deskreact.calendar.ampmtext.am',
|
|
58
|
+
'PM': 'deskreact.calendar.ampmtext.pm'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let dateFormat = {
|
|
62
|
+
dayNames: [
|
|
63
|
+
'Sun',
|
|
64
|
+
'Mon',
|
|
65
|
+
'Tue',
|
|
66
|
+
'Wed',
|
|
67
|
+
'Thu',
|
|
68
|
+
'Fri',
|
|
69
|
+
'Sat',
|
|
70
|
+
'Sunday',
|
|
71
|
+
'Monday',
|
|
72
|
+
'Tuesday',
|
|
73
|
+
'Wednesday',
|
|
74
|
+
'Thursday',
|
|
75
|
+
'Friday',
|
|
76
|
+
'Saturday',
|
|
77
|
+
],
|
|
78
|
+
monthNames: [
|
|
79
|
+
'Jan',
|
|
80
|
+
'Feb',
|
|
81
|
+
'Mar',
|
|
82
|
+
'Apr',
|
|
83
|
+
'May',
|
|
84
|
+
'Jun',
|
|
85
|
+
'Jul',
|
|
86
|
+
'Aug',
|
|
87
|
+
'Sep',
|
|
88
|
+
'Oct',
|
|
89
|
+
'Nov',
|
|
90
|
+
'Dec',
|
|
91
|
+
'January',
|
|
92
|
+
'February',
|
|
93
|
+
'March',
|
|
94
|
+
'April',
|
|
95
|
+
'May',
|
|
96
|
+
'June',
|
|
97
|
+
'July',
|
|
98
|
+
'August',
|
|
99
|
+
'September',
|
|
100
|
+
'October',
|
|
101
|
+
'November',
|
|
102
|
+
'December',
|
|
103
|
+
],
|
|
104
|
+
timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM'],
|
|
105
|
+
};
|
|
106
|
+
export function formatDate(dateMill, mask, dateDiffObj) {
|
|
107
|
+
if (mask === 'facebookFormat') {
|
|
108
|
+
let { hours, minutes, seconds, yDays, years, betweenleepYears, crntMonth } =
|
|
109
|
+
dateDiffObj;
|
|
110
|
+
if (years > 0) {
|
|
111
|
+
return `${years}y`;
|
|
112
|
+
} else if (yDays > 0) {
|
|
113
|
+
let isFeb = crntMonth === 1;
|
|
114
|
+
let isThoneMonth = [0, 2, 4, 6, 7, 9, 11].indexOf(crntMonth) != -1;
|
|
115
|
+
let weekCal = function (calDays) {
|
|
116
|
+
let weeks = calDays / 7;
|
|
117
|
+
return weeks < 1 ? `${calDays}d` : `${Math.trunc(weeks)}w`;
|
|
118
|
+
};
|
|
119
|
+
if (!isFeb) {
|
|
120
|
+
return isThoneMonth === true
|
|
121
|
+
? yDays === 31
|
|
122
|
+
? '1m'
|
|
123
|
+
: weekCal(yDays)
|
|
124
|
+
: yDays === 30
|
|
125
|
+
? '1m'
|
|
126
|
+
: weekCal(yDays);
|
|
127
|
+
}
|
|
128
|
+
if (betweenleepYears != 0) {
|
|
129
|
+
return isFeb === true && yDays === 28 ? '1m' : weekCal(yDays);
|
|
130
|
+
}
|
|
131
|
+
return isFeb === true && yDays === 29 ? '1m' : weekCal(yDays);
|
|
132
|
+
} else if (hours < 24 && hours > 0) {
|
|
133
|
+
return `${hours}h`;
|
|
134
|
+
} else if (minutes < 60 && hours > 1) {
|
|
135
|
+
return `${minutes}m`;
|
|
136
|
+
}
|
|
137
|
+
return seconds === 0 ? 'now' : `${seconds}s`;
|
|
138
|
+
}
|
|
139
|
+
let date = new Date(dateMill);
|
|
140
|
+
let d = date.getDate();
|
|
141
|
+
let D = date.getDay();
|
|
142
|
+
let m = date.getMonth();
|
|
143
|
+
let y = date.getFullYear();
|
|
144
|
+
let H = date.getHours();
|
|
145
|
+
let M = date.getMinutes();
|
|
146
|
+
let s = date.getSeconds();
|
|
147
|
+
let L = date.getMilliseconds();
|
|
148
|
+
let flags = {
|
|
149
|
+
d: d,
|
|
150
|
+
dd: pad(d, 2),
|
|
151
|
+
ddd: i18NProviderUtils.getI18NValue(dayi18n[dateFormat.dayNames[D]]),
|
|
152
|
+
dddd: i18NProviderUtils.getI18NValue(dayi18n[dateFormat.dayNames[D + 7]]),
|
|
153
|
+
D: d,
|
|
154
|
+
DD: pad(d, 2),
|
|
155
|
+
DDD: i18NProviderUtils.getI18NValue(dayi18n[dateFormat.dayNames[D]]),
|
|
156
|
+
DDDD: i18NProviderUtils.getI18NValue(dayi18n[dateFormat.dayNames[D + 7]]),
|
|
157
|
+
M: m + 1,
|
|
158
|
+
MM: pad(m + 1, 2),
|
|
159
|
+
MMM: i18NProviderUtils.getI18NValue(monthi18n[dateFormat.monthNames[m]]),
|
|
160
|
+
MMMM: i18NProviderUtils.getI18NValue(monthi18n[dateFormat.monthNames[m + 12]]),
|
|
161
|
+
yy: String(y).slice(2),
|
|
162
|
+
YY: String(y).slice(2),
|
|
163
|
+
yyyy: y,
|
|
164
|
+
YYYY: y,
|
|
165
|
+
h: H % 12 || 12,
|
|
166
|
+
hh: pad(H % 12 || 12, 2),
|
|
167
|
+
H: H,
|
|
168
|
+
HH: pad(H, 2),
|
|
169
|
+
m: M,
|
|
170
|
+
mm: pad(M, 2),
|
|
171
|
+
s: s,
|
|
172
|
+
ss: pad(s, 2),
|
|
173
|
+
l: pad(L, 3),
|
|
174
|
+
L: pad(Math.round(L / 10)),
|
|
175
|
+
t: H < 12 ? i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[0]]) : i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[1]]),
|
|
176
|
+
A: H < 12 ? i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[6]]) : i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[7]]),
|
|
177
|
+
T: H < 12 ? i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[4]]) : i18NProviderUtils.getI18NValue(timei18n[dateFormat.timeNames[5]])
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
let token =
|
|
181
|
+
/D{1,4}|M{1,4}|YY(?:YY)?|([HhmsA])\1?|[LloSZWN]|\[[^\]]*\]|'[^']*'/gi;
|
|
182
|
+
let dat = mask.replace(token, (match) => {
|
|
183
|
+
if (match in flags) {
|
|
184
|
+
return flags[match];
|
|
185
|
+
}
|
|
186
|
+
return match.slice(1, match.length - 1);
|
|
187
|
+
});
|
|
188
|
+
return dat;
|
|
189
|
+
}
|
|
190
|
+
export function pad(n, width = 1, z) {
|
|
191
|
+
z = z || '0';
|
|
192
|
+
n = `${n}`;
|
|
193
|
+
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
194
|
+
}
|
|
195
|
+
export function replaceI18NValuesWithRegex(i18nStr, values) {
|
|
196
|
+
if (typeof values !== 'undefined') {
|
|
197
|
+
if (Array.isArray(values)) {
|
|
198
|
+
for (let i = 0; i < values.length; i++) {
|
|
199
|
+
i18nStr = i18nStr.replace(new RegExp(`\\{${i}\\}`, 'g'), values[i]);
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return i18nStr;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function unescapeUnicode(str) {
|
|
209
|
+
return str.replace(/\\u([a-fA-F0-9]{4})/g, (g, m1) =>
|
|
210
|
+
String.fromCharCode(parseInt(m1, 16))
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
export function getValues(params = [], diff) {
|
|
214
|
+
return params.map((param) => diff[param]);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
let localizedData = {};
|
|
218
|
+
|
|
219
|
+
export function setLocalizedData(data) {
|
|
220
|
+
localizedData = data;
|
|
221
|
+
}
|
|
222
|
+
export function getLocalizedValue({
|
|
223
|
+
type,
|
|
224
|
+
moduleName,
|
|
225
|
+
apiName,
|
|
226
|
+
fieldValue
|
|
227
|
+
} = {}) {
|
|
228
|
+
let localizedValue = localizedData;
|
|
229
|
+
switch(type) {
|
|
230
|
+
case 'field':
|
|
231
|
+
['Field', moduleName, apiName].map((key) => {
|
|
232
|
+
localizedValue = localizedValue[key] || '';
|
|
233
|
+
});
|
|
234
|
+
break;
|
|
235
|
+
case 'picklist':
|
|
236
|
+
['PickListValue', moduleName, apiName, fieldValue].map((key) => {
|
|
237
|
+
localizedValue = localizedValue[key] || '';
|
|
238
|
+
});
|
|
239
|
+
break;
|
|
240
|
+
default:
|
|
241
|
+
return null
|
|
242
|
+
}
|
|
243
|
+
return localizedValue || null
|
|
244
|
+
}
|
|
245
|
+
export function getI18NValue(i18n) {
|
|
246
|
+
if (typeof i18n === 'undefined') {
|
|
247
|
+
return (key) => key;
|
|
248
|
+
}
|
|
249
|
+
return (key, values, localizedProps) => {
|
|
250
|
+
const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : null;
|
|
251
|
+
let i18nStr = i18n[key];
|
|
252
|
+
if (i18nStr === undefined) {
|
|
253
|
+
return localizedValue || key;
|
|
254
|
+
}
|
|
255
|
+
i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
|
|
256
|
+
return localizedValue || unescapeUnicode(i18nStr);
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// function getValues(params = [], diff) {
|
|
261
|
+
// return params.map(param => {
|
|
262
|
+
// return diff[param];
|
|
263
|
+
// });
|
|
264
|
+
// }
|
|
265
|
+
export function getI18NInfo(toDateObj, props, diffObj) {
|
|
266
|
+
let key = null,
|
|
267
|
+
values,
|
|
268
|
+
text = null;
|
|
269
|
+
if (typeof props === 'function') {
|
|
270
|
+
let value = props(diffObj1);
|
|
271
|
+
key = value.key;
|
|
272
|
+
values = getValues(value.params, diffObj);
|
|
273
|
+
} else if (typeof props === 'object') {
|
|
274
|
+
key = props.key;
|
|
275
|
+
values = getValues(props.params, diffObj);
|
|
276
|
+
} else if (typeof props === 'string') {
|
|
277
|
+
text = toDateObj.format(props);
|
|
278
|
+
}
|
|
279
|
+
return { key, values, text };
|
|
280
|
+
}
|
|
281
|
+
export function isToday(fromDate, toDate) {
|
|
282
|
+
let TODAY = toDate.clone().startOf('day');
|
|
283
|
+
return fromDate.isSame(TODAY, 'd');
|
|
284
|
+
}
|
|
285
|
+
export function isYesterday(fromDate, toDate) {
|
|
286
|
+
let YESTERDAY = toDate.clone().subtract(1, 'days').startOf('day');
|
|
287
|
+
return fromDate.isSame(YESTERDAY, 'd');
|
|
288
|
+
}
|
|
289
|
+
export function isTomorrow(fromDate, toDate) {
|
|
290
|
+
return isYesterday(toDate, fromDate);
|
|
291
|
+
}
|
|
292
|
+
export function isWithinAWeek(fromDate, toDate) {
|
|
293
|
+
let A_WEEK_OLD = toDate.clone().subtract(7, 'days').startOf('day');
|
|
294
|
+
return fromDate.isAfter(A_WEEK_OLD);
|
|
295
|
+
}
|
|
296
|
+
export function isTwoWeeksOrMore(fromDate, toDate) {
|
|
297
|
+
return !isWithinAWeek(fromDate, toDate);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function getDatePatternWithoutYear(datePattern) {
|
|
301
|
+
let dateObj;
|
|
302
|
+
let delemiter;
|
|
303
|
+
|
|
304
|
+
if (datePattern.indexOf('.') !== -1) {
|
|
305
|
+
dateObj = datePattern.split('.');
|
|
306
|
+
delemiter = '.';
|
|
307
|
+
} else if (datePattern.indexOf('-') !== -1) {
|
|
308
|
+
dateObj = datePattern.split('-');
|
|
309
|
+
delemiter = '-';
|
|
310
|
+
} else if (datePattern.indexOf('/') !== -1) {
|
|
311
|
+
dateObj = datePattern.split('/');
|
|
312
|
+
delemiter = '/';
|
|
313
|
+
} else {
|
|
314
|
+
dateObj = datePattern.split(' ');
|
|
315
|
+
delemiter = ' ';
|
|
316
|
+
}
|
|
317
|
+
return dateObj
|
|
318
|
+
.filter((data) => ['yy', 'yyyy'].indexOf(data) === -1)
|
|
319
|
+
.join(delemiter);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function userDateFormat(
|
|
323
|
+
getI18NValue,
|
|
324
|
+
timezoneData,
|
|
325
|
+
timeFormat,
|
|
326
|
+
datePattern,
|
|
327
|
+
isEnabledCurrentYear
|
|
328
|
+
) {
|
|
329
|
+
return (
|
|
330
|
+
to,
|
|
331
|
+
{ today, yesterday, tomorrow, others },
|
|
332
|
+
ago,
|
|
333
|
+
later,
|
|
334
|
+
isSuffixEnable = false,
|
|
335
|
+
format
|
|
336
|
+
) => {
|
|
337
|
+
let fromDateObj = datetime.toDate(datetime.tz.utcToTz(null, timezoneData));
|
|
338
|
+
let toDateObj = datetime.toDate(datetime.tz.utcToTz(to, timezoneData));
|
|
339
|
+
|
|
340
|
+
let diffMin = new Date(to).getTime() - new Date().getTime();
|
|
341
|
+
let from = new Date();
|
|
342
|
+
from = from.toISOString();
|
|
343
|
+
let suffix;
|
|
344
|
+
if (diffMin < 0) {
|
|
345
|
+
suffix = ago || '';
|
|
346
|
+
} else if (diffMin > 0) {
|
|
347
|
+
suffix = later || '';
|
|
348
|
+
} else {
|
|
349
|
+
suffix = '';
|
|
350
|
+
}
|
|
351
|
+
let diff = getDiffObj(diffMin);
|
|
352
|
+
let withInAWeak = diff.y === 0 && diff.yd <= 7;
|
|
353
|
+
let diffObj = {
|
|
354
|
+
h: diff.h,
|
|
355
|
+
m: diff.m,
|
|
356
|
+
s: diff.s,
|
|
357
|
+
y: diff.y,
|
|
358
|
+
hh: pad(diff.h, 2),
|
|
359
|
+
mm: pad(diff.m, 2),
|
|
360
|
+
ss: pad(diff.s, 2),
|
|
361
|
+
yy: pad(diff.y, 2),
|
|
362
|
+
days: diff.yd,
|
|
363
|
+
yDays: pad(diff.yd, 2),
|
|
364
|
+
isWithInAWeek: withInAWeak,
|
|
365
|
+
suffix: suffix,
|
|
366
|
+
};
|
|
367
|
+
let diffObj1 = {
|
|
368
|
+
hours: diff.h,
|
|
369
|
+
minutes: diff.m,
|
|
370
|
+
seconds: diff.s,
|
|
371
|
+
years: diff.y,
|
|
372
|
+
yDays: diff.yd,
|
|
373
|
+
crntYear: new Date(from).getFullYear(),
|
|
374
|
+
tYear: new Date(to).getFullYear(),
|
|
375
|
+
isWithInAWeek: withInAWeak,
|
|
376
|
+
suffix: suffix,
|
|
377
|
+
timeFormat: timeFormat,
|
|
378
|
+
datePattern: datePattern,
|
|
379
|
+
dateTimePattern: `${datePattern} ${timeFormat}`,
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
//In if condition we'll remove year and set date format if the current year is not required
|
|
383
|
+
//In else part we'll set the date format as it is
|
|
384
|
+
if (
|
|
385
|
+
isEnabledCurrentYear === true &&
|
|
386
|
+
diffObj1.years === 0 &&
|
|
387
|
+
diffObj1.tYear === diffObj1.crntYear
|
|
388
|
+
) {
|
|
389
|
+
let dateFormat = getDatePatternWithoutYear(datePattern);
|
|
390
|
+
diffObj1.dateFormat = dateFormat;
|
|
391
|
+
diffObj1.dateTimeFormat = `${dateFormat} ${timeFormat}`;
|
|
392
|
+
} else {
|
|
393
|
+
diffObj1.dateFormat = datePattern;
|
|
394
|
+
diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
//var daysDiff = toDateObj.diff(fromDateObj, 'days');
|
|
398
|
+
|
|
399
|
+
let key = '';
|
|
400
|
+
let values = [];
|
|
401
|
+
let text = null;
|
|
402
|
+
if (format) {
|
|
403
|
+
let years, months, days, hours, minutes, seconds;
|
|
404
|
+
|
|
405
|
+
years = diffObj1.years > 1 ? '2' : diffObj1.years;
|
|
406
|
+
// months = diffObj1.months > 1 ? '2' : diffObj1.months;
|
|
407
|
+
// days = diffObj1.days > 1 ? '2' : diffObj1.days;
|
|
408
|
+
days = diff.yd > 1 ? '2' : diff.yd;
|
|
409
|
+
hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
|
|
410
|
+
minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
|
|
411
|
+
//seconds = diffObj1.seconds > 1 ? '2' : diffObj1.seconds;
|
|
412
|
+
// let pattern = '' + years + months + days + hours + minutes + seconds;
|
|
413
|
+
let count = 0;
|
|
414
|
+
let pattern = [years, days, hours, minutes].reduce((res, next) => {
|
|
415
|
+
if (count === 2 || next === 0) {
|
|
416
|
+
res = `${res}0`;
|
|
417
|
+
} else if (next !== 0) {
|
|
418
|
+
count++;
|
|
419
|
+
res = res + next;
|
|
420
|
+
} else {
|
|
421
|
+
res = res + next;
|
|
422
|
+
}
|
|
423
|
+
return res;
|
|
424
|
+
}, '');
|
|
425
|
+
let value = format(diffObj1, pattern);
|
|
426
|
+
if (value && typeof value === 'object') {
|
|
427
|
+
key = value.key;
|
|
428
|
+
values = getValues(value.params, diffObj);
|
|
429
|
+
isSuffixEnable = true;
|
|
430
|
+
} else if (typeof value === 'string') {
|
|
431
|
+
text = formatDate(toDateObj, value);
|
|
432
|
+
}
|
|
433
|
+
} else {
|
|
434
|
+
let dateObj = new Date(toDateObj);
|
|
435
|
+
let curDateObj = new Date(fromDateObj);
|
|
436
|
+
let diffDayType = diffObj1.yDays;
|
|
437
|
+
|
|
438
|
+
//In this condition, to calculate different days we have copied it from live --> diffDayType
|
|
439
|
+
if (
|
|
440
|
+
isOverdue &&
|
|
441
|
+
dateObj.getDate() < curDateObj.getDate() &&
|
|
442
|
+
diffObj1.yDays == 0
|
|
443
|
+
) {
|
|
444
|
+
diffDayType = -1;
|
|
445
|
+
}
|
|
446
|
+
if (!isOverdue) {
|
|
447
|
+
let diffHr = dateObj.getHours() - curDateObj.getHours();
|
|
448
|
+
if (diffHr < 0) {
|
|
449
|
+
diffDayType += 1;
|
|
450
|
+
} else if (diffHr == 0) {
|
|
451
|
+
let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
|
|
452
|
+
if (diffMins < 0) {
|
|
453
|
+
diffDayType += 1;
|
|
454
|
+
} else if (diffMins == 0) {
|
|
455
|
+
let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
|
|
456
|
+
if (diffSec < 0) {
|
|
457
|
+
diffDayType += 1;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
|
|
463
|
+
if (today && dateObj.getDate() === curDateObj.getDate()) {
|
|
464
|
+
if (typeof today === 'object') {
|
|
465
|
+
key = today.key;
|
|
466
|
+
values = getValues(today.params, diffObj);
|
|
467
|
+
isSuffixEnable = true;
|
|
468
|
+
} else if (typeof today === 'string') {
|
|
469
|
+
text = formatDate(toDateObj, today);
|
|
470
|
+
}
|
|
471
|
+
} else if (
|
|
472
|
+
(yesterday &&
|
|
473
|
+
dateObj.getMonth() === curDateObj.getMonth() &&
|
|
474
|
+
dateObj.getDate() < curDateObj.getDate()) ||
|
|
475
|
+
(yesterday && dateObj.getMonth() < curDateObj.getMonth())
|
|
476
|
+
) {
|
|
477
|
+
if (typeof yesterday === 'object') {
|
|
478
|
+
key = yesterday.key;
|
|
479
|
+
values = getValues(yesterday.params, diffObj);
|
|
480
|
+
} else if (typeof yesterday === 'string') {
|
|
481
|
+
text = formatDate(toDateObj, yesterday);
|
|
482
|
+
}
|
|
483
|
+
} else if (
|
|
484
|
+
tomorrow &&
|
|
485
|
+
!isOverdue &&
|
|
486
|
+
diff.y === 0 &&
|
|
487
|
+
diffDayType === 1
|
|
488
|
+
) {
|
|
489
|
+
if (typeof tomorrow === 'object') {
|
|
490
|
+
key = tomorrow.key;
|
|
491
|
+
values = getValues(tomorrow.params, diffObj);
|
|
492
|
+
} else if (typeof tomorrow === 'string') {
|
|
493
|
+
text = formatDate(toDateObj, tomorrow);
|
|
494
|
+
}
|
|
495
|
+
} else {
|
|
496
|
+
let value = others(diffObj1);
|
|
497
|
+
if (typeof value === 'object') {
|
|
498
|
+
key = value.key;
|
|
499
|
+
values = getValues(value.params, diffObj);
|
|
500
|
+
isSuffixEnable = true;
|
|
501
|
+
} else if (typeof value === 'string') {
|
|
502
|
+
text = formatDate(toDateObj, value);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
} else {
|
|
506
|
+
let value = others(diffObj1);
|
|
507
|
+
if (typeof value === 'object') {
|
|
508
|
+
key = value.key;
|
|
509
|
+
values = getValues(value.params, diffObj);
|
|
510
|
+
isSuffixEnable = true;
|
|
511
|
+
} else if (typeof value === 'string') {
|
|
512
|
+
text = formatDate(toDateObj, value);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
let key1 = isSuffixEnable && suffix != '' ? `${key}.${suffix}` : key;
|
|
517
|
+
return text || getI18NValue(key1, values);
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
let oneYearInMillis = 31536000000;
|
|
522
|
+
let oneDayInMillis = 86400000;
|
|
523
|
+
let oneHourInMillis = 3600000;
|
|
524
|
+
let oneMinuteInMillis = 60000;
|
|
525
|
+
function convertAsNonExponential(number) {
|
|
526
|
+
if (number.toString().toLowerCase().indexOf('e') !== -1) {
|
|
527
|
+
return number.toFixed(20);
|
|
528
|
+
}
|
|
529
|
+
return number;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export function getDiffObj(diff) {
|
|
533
|
+
diff = Math.abs(diff);
|
|
534
|
+
let diffYears = diff / oneYearInMillis;
|
|
535
|
+
diffYears = convertAsNonExponential(diffYears);
|
|
536
|
+
let diffDays = (diff % oneYearInMillis) / oneDayInMillis;
|
|
537
|
+
diffDays = convertAsNonExponential(diffDays);
|
|
538
|
+
let diffHours = (diff % oneDayInMillis) / oneHourInMillis;
|
|
539
|
+
diffHours = convertAsNonExponential(diffHours);
|
|
540
|
+
let diffMinutes =
|
|
541
|
+
((diff % oneDayInMillis) % oneHourInMillis) / oneMinuteInMillis;
|
|
542
|
+
let diffSeconds =
|
|
543
|
+
(((diff % oneDayInMillis) % oneHourInMillis) % oneMinuteInMillis) / 1000;
|
|
544
|
+
diffDays = parseInt(diffDays);
|
|
545
|
+
diffHours = parseInt(diffHours);
|
|
546
|
+
diffMinutes = parseInt(diffMinutes);
|
|
547
|
+
diffSeconds = parseInt(diffSeconds);
|
|
548
|
+
diffYears = parseInt(diffYears);
|
|
549
|
+
return {
|
|
550
|
+
y: diffYears,
|
|
551
|
+
yd: diffDays,
|
|
552
|
+
h: diffHours,
|
|
553
|
+
m: diffMinutes,
|
|
554
|
+
s: diffSeconds,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export function getLyears(from, to) {
|
|
559
|
+
let dFrom = new Date(from),
|
|
560
|
+
dTo = new Date(to);
|
|
561
|
+
let fromYear = dFrom.getFullYear();
|
|
562
|
+
let toYear = dTo.getFullYear();
|
|
563
|
+
let arrayofLeapYears = [];
|
|
564
|
+
for (let loopYear = fromYear; loopYear <= toYear; loopYear++) {
|
|
565
|
+
if (
|
|
566
|
+
(loopYear % 4 === 0 && loopYear % 100 !== 0) ||
|
|
567
|
+
(loopYear % 100 === 0 && loopYear % 400 === 0)
|
|
568
|
+
) {
|
|
569
|
+
arrayofLeapYears.push(loopYear);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return arrayofLeapYears.length;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
export function getSuffix(min, ago, later) {
|
|
576
|
+
let suffix;
|
|
577
|
+
if (min < 0) {
|
|
578
|
+
suffix = ago || '';
|
|
579
|
+
} else if (min > 0) {
|
|
580
|
+
suffix = later || '';
|
|
581
|
+
} else {
|
|
582
|
+
suffix = '';
|
|
583
|
+
}
|
|
584
|
+
return suffix;
|
|
585
|
+
}
|