@zohodesk/i18n 1.0.0-beta.23 → 1.0.0-beta.25

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