@zohodesk/i18n 1.0.0-beta.4 → 1.0.0-beta.41-murphy

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