@zohodesk/i18n 1.0.0-beta.3 → 1.0.0-beta.31

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 (60) hide show
  1. package/README.md +120 -2
  2. package/es/I18NContext.js +1 -2
  3. package/es/components/DateTimeDiffFormat.js +192 -200
  4. package/es/components/FormatText.js +4 -25
  5. package/es/components/HOCI18N.js +33 -45
  6. package/es/components/I18N.js +48 -63
  7. package/es/components/I18NProvider.js +60 -85
  8. package/es/components/PluralFormat.js +29 -48
  9. package/es/components/UserTimeDiffFormat.js +65 -74
  10. package/es/components/__tests__/DateTimeDiffFormat.spec.js +868 -657
  11. package/es/components/__tests__/FormatText.spec.js +20 -17
  12. package/es/components/__tests__/HOCI18N.spec.js +18 -22
  13. package/es/components/__tests__/I18N.spec.js +20 -19
  14. package/es/components/__tests__/I18NProvider.spec.js +36 -45
  15. package/es/components/__tests__/PluralFormat.spec.js +20 -17
  16. package/es/components/__tests__/UserTimeDiffFormat.spec.js +1343 -1095
  17. package/es/index.js +2 -6
  18. package/es/utils/__tests__/jsxTranslations.spec.js +174 -0
  19. package/es/utils/index.js +592 -0
  20. package/es/utils/jsxTranslations.js +193 -0
  21. package/lib/I18NContext.js +6 -6
  22. package/lib/components/DateTimeDiffFormat.js +151 -123
  23. package/lib/components/FormatText.js +32 -22
  24. package/lib/components/HOCI18N.js +47 -23
  25. package/lib/components/I18N.js +62 -36
  26. package/lib/components/I18NProvider.js +85 -72
  27. package/lib/components/PluralFormat.js +42 -32
  28. package/lib/components/UserTimeDiffFormat.js +79 -56
  29. package/lib/components/__tests__/DateTimeDiffFormat.spec.js +815 -629
  30. package/lib/components/__tests__/FormatText.spec.js +23 -25
  31. package/lib/components/__tests__/HOCI18N.spec.js +26 -34
  32. package/lib/components/__tests__/I18N.spec.js +21 -26
  33. package/lib/components/__tests__/I18NProvider.spec.js +43 -51
  34. package/lib/components/__tests__/PluralFormat.spec.js +24 -28
  35. package/lib/components/__tests__/UserTimeDiffFormat.spec.js +1256 -1039
  36. package/lib/index.js +85 -110
  37. package/lib/utils/__tests__/jsxTranslations.spec.js +183 -0
  38. package/lib/utils/index.js +658 -0
  39. package/lib/utils/jsxTranslations.js +242 -0
  40. package/package.json +3 -2
  41. package/src/components/DateTimeDiffFormat.js +86 -55
  42. package/src/components/I18N.js +2 -0
  43. package/src/components/I18NProvider.js +44 -33
  44. package/src/components/UserTimeDiffFormat.js +24 -18
  45. package/src/index.js +7 -9
  46. package/src/utils/__tests__/jsxTranslations.spec.js +213 -0
  47. package/src/utils/index.js +632 -0
  48. package/src/utils/jsxTranslations.js +180 -0
  49. package/es/components/NewDateFormat.js +0 -50
  50. package/es/offset.js +0 -629
  51. package/es/timezones.js +0 -118
  52. package/es/utils.js +0 -621
  53. package/lib/components/NewDateFormat.js +0 -68
  54. package/lib/offset.js +0 -634
  55. package/lib/timezones.js +0 -129
  56. package/lib/utils.js +0 -651
  57. package/src/components/NewDateFormat.js +0 -60
  58. package/src/offset.js +0 -629
  59. package/src/timezones.js +0 -113
  60. package/src/utils.js +0 -648
package/lib/utils.js DELETED
@@ -1,651 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.browserOffset = undefined;
7
-
8
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
-
10
- exports.formatDate = formatDate;
11
- exports.pad = pad;
12
- exports.replaceI18NValuesWithRegex = replaceI18NValuesWithRegex;
13
- exports.unescapeUnicode = unescapeUnicode;
14
- exports.getValues = getValues;
15
- exports.getI18NValue = getI18NValue;
16
- exports.getI18NInfo = getI18NInfo;
17
- exports.isToday = isToday;
18
- exports.isYesterday = isYesterday;
19
- exports.isTomorrow = isTomorrow;
20
- exports.isWithinAWeek = isWithinAWeek;
21
- exports.isTwoWeeksOrMore = isTwoWeeksOrMore;
22
- exports.convertTimezone = convertTimezone;
23
- exports.getUtcTimeStamp = getUtcTimeStamp;
24
- exports.convertUtcToUserTz = convertUtcToUserTz;
25
- exports.convertUserTzToUtc = convertUserTzToUtc;
26
- exports.userDateFormat = userDateFormat;
27
- exports.getDiffObj = getDiffObj;
28
- exports.getLyears = getLyears;
29
- exports.getSuffix = getSuffix;
30
- exports.getFormatedDate = getFormatedDate;
31
-
32
- var _timezones = require('./timezones');
33
-
34
- var _offset = require('./offset');
35
-
36
- var browserOffset = exports.browserOffset = -new Date().getTimezoneOffset();
37
- var dateFormat = {
38
- dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
39
- monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
40
- timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM']
41
- };
42
- function formatDate(dateMill, mask, dateDiffObj) {
43
- if (mask === 'facebookFormat') {
44
- var hours = dateDiffObj.hours,
45
- minutes = dateDiffObj.minutes,
46
- seconds = dateDiffObj.seconds,
47
- yDays = dateDiffObj.yDays,
48
- years = dateDiffObj.years,
49
- betweenleepYears = dateDiffObj.betweenleepYears,
50
- crntMonth = dateDiffObj.crntMonth;
51
-
52
- if (years > 0) {
53
- return years + 'y';
54
- } else if (yDays > 0) {
55
- var isFeb = crntMonth === 1;
56
- var isThoneMonth = [0, 2, 4, 6, 7, 9, 11].indexOf(crntMonth) != -1;
57
- var weekCal = function weekCal(calDays) {
58
- var weeks = calDays / 7;
59
- return weeks < 1 ? calDays + 'd' : Math.trunc(weeks) + 'w';
60
- };
61
- if (!isFeb) {
62
- return isThoneMonth === true ? yDays === 31 ? '1m' : weekCal(yDays) : yDays === 30 ? '1m' : weekCal(yDays);
63
- }
64
- if (betweenleepYears != 0) {
65
- return isFeb === true && yDays === 28 ? '1m' : weekCal(yDays);
66
- }
67
- return isFeb === true && yDays === 29 ? '1m' : weekCal(yDays);
68
- } else if (hours < 24 && hours > 0) {
69
- return hours + 'h';
70
- } else if (minutes < 60 && hours > 1) {
71
- return minutes + 'm';
72
- }
73
- return seconds === 0 ? 'now' : seconds + 's';
74
- }
75
- var date = new Date(dateMill);
76
- var d = date.getDate();
77
- var D = date.getDay();
78
- var m = date.getMonth();
79
- var y = date.getFullYear();
80
- var H = date.getHours();
81
- var M = date.getMinutes();
82
- var s = date.getSeconds();
83
- var L = date.getMilliseconds();
84
- var flags = {
85
- d: d,
86
- dd: pad(d, 2),
87
- ddd: dateFormat.dayNames[D],
88
- dddd: dateFormat.dayNames[D + 7],
89
- D: d,
90
- DD: pad(d, 2),
91
- DDD: dateFormat.dayNames[D],
92
- DDDD: dateFormat.dayNames[D + 7],
93
- M: m + 1,
94
- MM: pad(m + 1, 2),
95
- MMM: dateFormat.monthNames[m],
96
- MMMM: dateFormat.monthNames[m + 12],
97
- yy: String(y).slice(2),
98
- YY: String(y).slice(2),
99
- yyyy: y,
100
- YYYY: y,
101
- h: H % 12 || 12,
102
- hh: pad(H % 12 || 12, 2),
103
- H: H,
104
- HH: pad(H, 2),
105
- m: M,
106
- mm: pad(M, 2),
107
- s: s,
108
- ss: pad(s, 2),
109
- l: pad(L, 3),
110
- L: pad(Math.round(L / 10)),
111
- t: H < 12 ? dateFormat.timeNames[0] : dateFormat.timeNames[1],
112
- A: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7],
113
- T: H < 12 ? dateFormat.timeNames[4] : dateFormat.timeNames[5]
114
- };
115
-
116
- var token = /D{1,4}|M{1,4}|YY(?:YY)?|([HhmsA])\1?|[LloSZWN]|\[[^\]]*\]|'[^']*'/gi;
117
- var dat = mask.replace(token, function (match) {
118
- if (match in flags) {
119
- return flags[match];
120
- }
121
- return match.slice(1, match.length - 1);
122
- });
123
- return dat;
124
- }
125
- function pad(n) {
126
- var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
127
- var z = arguments[2];
128
-
129
- z = z || '0';
130
- n = '' + n;
131
- return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
132
- }
133
- function replaceI18NValuesWithRegex(i18nStr, values) {
134
- if (typeof values !== 'undefined') {
135
- if (Array.isArray(values)) {
136
- for (var i = 0; i < values.length; i++) {
137
- i18nStr = i18nStr.replace(new RegExp('\\{' + i + '\\}', 'g'), values[i]);
138
- }
139
- } else {
140
- i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values);
141
- }
142
- }
143
- return i18nStr;
144
- }
145
-
146
- function unescapeUnicode(str) {
147
- return str.replace(/\\u([a-fA-F0-9]{4})/g, function (g, m1) {
148
- return String.fromCharCode(parseInt(m1, 16));
149
- });
150
- }
151
- function getValues() {
152
- var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
153
- var diff = arguments[1];
154
-
155
- return params.map(function (param) {
156
- return diff[param];
157
- });
158
- }
159
- function getI18NValue(i18n) {
160
- if (typeof i18n === 'undefined') {
161
- return function (key) {
162
- return key;
163
- };
164
- }
165
- return function (key, values) {
166
- var i18nStr = i18n[key];
167
- if (i18nStr === undefined) {
168
- return key;
169
- }
170
- i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
171
- return unescapeUnicode(i18nStr);
172
- };
173
- }
174
-
175
- // function getValues(params = [], diff) {
176
- // return params.map(param => {
177
- // return diff[param];
178
- // });
179
- // }
180
- function getI18NInfo(toDateObj, props, diffObj) {
181
- var key = null,
182
- values = void 0,
183
- text = null;
184
- if (typeof props === 'function') {
185
- var value = props(diffObj1);
186
- key = value.key;
187
- values = getValues(value.params, diffObj);
188
- } else if ((typeof props === 'undefined' ? 'undefined' : _typeof(props)) === 'object') {
189
- key = props.key;
190
- values = getValues(props.params, diffObj);
191
- } else if (typeof props === 'string') {
192
- text = toDateObj.format(props);
193
- }
194
- return { key: key, values: values, text: text };
195
- }
196
- function isToday(fromDate, toDate) {
197
- var TODAY = toDate.clone().startOf('day');
198
- return fromDate.isSame(TODAY, 'd');
199
- }
200
- function isYesterday(fromDate, toDate) {
201
- var YESTERDAY = toDate.clone().subtract(1, 'days').startOf('day');
202
- return fromDate.isSame(YESTERDAY, 'd');
203
- }
204
- function isTomorrow(fromDate, toDate) {
205
- return isYesterday(toDate, fromDate);
206
- }
207
- function isWithinAWeek(fromDate, toDate) {
208
- var A_WEEK_OLD = toDate.clone().subtract(7, 'days').startOf('day');
209
- return fromDate.isAfter(A_WEEK_OLD);
210
- }
211
- function isTwoWeeksOrMore(fromDate, toDate) {
212
- return !isWithinAWeek(fromDate, toDate);
213
- }
214
-
215
- function convertTimezone(timeinlong, fromoffset, tooffset) {
216
- var diff_offset = tooffset - fromoffset;
217
- timeinlong = Number(timeinlong) + diff_offset * oneMinuteInMillis;
218
- return timeinlong;
219
- }
220
-
221
- function getUtcTimeStamp(date) {
222
- var timestamp = date ? new Date(date).getTime() : new Date().getTime();
223
- var browserOffset = -new Date().getTimezoneOffset();
224
- return convertTimezone(timestamp, browserOffset, 0);
225
- }
226
-
227
- function getOffset(utcTimeStamp, tzData) {
228
- var browserOffset = -new Date().getTimezoneOffset();
229
- if (tzData) {
230
- if (typeof tzData === 'string') {
231
- return _offset.offsetMap[tzData];
232
- } else if ((typeof tzData === 'undefined' ? 'undefined' : _typeof(tzData)) === 'object') {
233
- return (0, _timezones.getTimezoneOffset)(utcTimeStamp, tzData);
234
- }
235
- }
236
- return browserOffset;
237
- }
238
-
239
- function convertUtcToUserTz(date, tzData) {
240
- var utcTimeStamp = getUtcTimeStamp(date);
241
- var offset = getOffset(utcTimeStamp, tzData);
242
- var convertedTimeStamp = convertTimezone(utcTimeStamp, 0, offset);
243
- return new Date(convertedTimeStamp);
244
- }
245
-
246
- function convertUserTzToUtc(userDate, tzData) {
247
- var timestamp = userDate ? new Date(userDate).getTime() : new Date().getTime();
248
- var browserOffset = -new Date().getTimezoneOffset();
249
- var offset = getOffset(timestamp, tzData);
250
- var utcTimeStamp = convertTimezone(timestamp, offset, 0);
251
- var convertedTimeStamp = convertTimezone(utcTimeStamp, 0, browserOffset);
252
- return new Date(convertedTimeStamp);
253
- }
254
-
255
- function userDateFormat(getI18NValue, tzData) {
256
- return function (to, _ref, ago, later) {
257
- var today = _ref.today,
258
- yesterday = _ref.yesterday,
259
- tomorrow = _ref.tomorrow,
260
- others = _ref.others;
261
- var isSuffixEnable = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
262
- var format = arguments[5];
263
-
264
- var fromDateObj = convertUtcToUserTz(null, tzData);
265
- var toDateObj = convertUtcToUserTz(to, tzData);
266
-
267
- var diffMin = new Date(to).getTime() - new Date().getTime();
268
- var suffix = void 0;
269
- if (diffMin < 0) {
270
- suffix = ago || '';
271
- } else if (diffMin > 0) {
272
- suffix = later || '';
273
- } else {
274
- suffix = '';
275
- }
276
- var diff = getDiffObj(diffMin);
277
- var withInAWeak = diff.y === 0 && diff.yd <= 7;
278
- var diffObj = {
279
- h: diff.h,
280
- m: diff.m,
281
- s: diff.s,
282
- y: diff.y,
283
- hh: pad(diff.h, 2),
284
- mm: pad(diff.m, 2),
285
- ss: pad(diff.s, 2),
286
- yy: pad(diff.y, 2),
287
- days: diff.yd,
288
- yDays: pad(diff.yd, 2),
289
- isWithInAWeek: withInAWeak,
290
- suffix: suffix
291
- };
292
- var diffObj1 = {
293
- hours: diff.h,
294
- minutes: diff.m,
295
- seconds: diff.s,
296
- years: diff.y,
297
- yDays: diff.yd,
298
- isWithInAWeek: withInAWeak,
299
- suffix: suffix
300
- };
301
- //var daysDiff = toDateObj.diff(fromDateObj, 'days');
302
-
303
- var key = '';
304
- var values = [];
305
- var text = null;
306
- if (format) {
307
- var years = void 0,
308
- months = void 0,
309
- days = void 0,
310
- hours = void 0,
311
- minutes = void 0,
312
- seconds = void 0;
313
-
314
- years = diffObj1.years > 1 ? '2' : diffObj1.years;
315
- // months = diffObj1.months > 1 ? '2' : diffObj1.months;
316
- // days = diffObj1.days > 1 ? '2' : diffObj1.days;
317
- days = diff.yd > 1 ? '2' : diff.yd;
318
- hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
319
- minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
320
- //seconds = diffObj1.seconds > 1 ? '2' : diffObj1.seconds;
321
- // let pattern = '' + years + months + days + hours + minutes + seconds;
322
- var count = 0;
323
- var pattern = [years, days, hours, minutes].reduce(function (res, next) {
324
- if (count === 2 || next === 0) {
325
- res = res + '0';
326
- } else if (next !== 0) {
327
- count++;
328
- res = res + next;
329
- } else {
330
- res = res + next;
331
- }
332
- return res;
333
- }, '');
334
- var value = format(diffObj1, pattern);
335
- if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
336
- key = value.key;
337
- values = getValues(value.params, diffObj);
338
- isSuffixEnable = true;
339
- } else if (typeof value === 'string') {
340
- text = formatDate(toDateObj, value);
341
- }
342
- } else {
343
- if (diff.y === 0 && (diff.yd === 0 || diff.yd === 1)) {
344
- var dateObj = new Date(toDateObj);
345
- var curDateObj = new Date(fromDateObj);
346
- if (dateObj.getDate() === curDateObj.getDate()) {
347
- if ((typeof today === 'undefined' ? 'undefined' : _typeof(today)) === 'object') {
348
- key = today.key;
349
- values = getValues(today.params, diffObj);
350
- isSuffixEnable = true;
351
- } else if (typeof today === 'string') {
352
- text = formatDate(toDateObj, today);
353
- }
354
- } else if (dateObj.getDate() < curDateObj.getDate()) {
355
- if ((typeof yesterday === 'undefined' ? 'undefined' : _typeof(yesterday)) === 'object') {
356
- key = yesterday.key;
357
- values = getValues(yesterday.params, diffObj);
358
- } else if (typeof yesterday === 'string') {
359
- text = formatDate(toDateObj, yesterday);
360
- }
361
- } else if (dateObj.getDate() > curDateObj.getDate()) {
362
- if ((typeof tomorrow === 'undefined' ? 'undefined' : _typeof(tomorrow)) === 'object') {
363
- key = tomorrow.key;
364
- values = getValues(tomorrow.params, diffObj);
365
- } else if (typeof tomorrow === 'string') {
366
- text = formatDate(toDateObj, tomorrow);
367
- }
368
- }
369
- } else {
370
- var _value = others(diffObj1);
371
- if ((typeof _value === 'undefined' ? 'undefined' : _typeof(_value)) === 'object') {
372
- key = _value.key;
373
- values = getValues(_value.params, diffObj);
374
- isSuffixEnable = true;
375
- } else if (typeof _value === 'string') {
376
- text = formatDate(toDateObj, tomorrow);
377
- }
378
- }
379
- }
380
- var key1 = isSuffixEnable && suffix != '' ? key + '.' + suffix : key;
381
- return text || getI18NValue(key1, values);
382
- };
383
- }
384
-
385
- var oneYearInMillis = 31536000000;
386
- var oneDayInMillis = 86400000;
387
- var oneHourInMillis = 3600000;
388
- var oneMinuteInMillis = 60000;
389
- function convertAsNonExponential(number) {
390
- if (number.toString().toLowerCase().indexOf('e') !== -1) {
391
- return number.toFixed(20);
392
- }
393
- return number;
394
- }
395
-
396
- function getDiffObj(diff) {
397
- diff = Math.abs(diff);
398
- var diffYears = diff / oneYearInMillis;
399
- diffYears = convertAsNonExponential(diffYears);
400
- var diffDays = diff % oneYearInMillis / oneDayInMillis;
401
- diffDays = convertAsNonExponential(diffDays);
402
- var diffHours = diff % oneDayInMillis / oneHourInMillis;
403
- diffHours = convertAsNonExponential(diffHours);
404
- var diffMinutes = diff % oneDayInMillis % oneHourInMillis / oneMinuteInMillis;
405
- var diffSeconds = diff % oneDayInMillis % oneHourInMillis % oneMinuteInMillis / 1000;
406
- diffDays = parseInt(diffDays);
407
- diffHours = parseInt(diffHours);
408
- diffMinutes = parseInt(diffMinutes);
409
- diffSeconds = parseInt(diffSeconds);
410
- diffYears = parseInt(diffYears);
411
- return {
412
- y: diffYears,
413
- yd: diffDays,
414
- h: diffHours,
415
- m: diffMinutes,
416
- s: diffSeconds
417
- };
418
- }
419
-
420
- function getLyears(from, to) {
421
- var dFrom = new Date(from),
422
- dTo = new Date(to);
423
- var fromYear = dFrom.getFullYear();
424
- var toYear = dTo.getFullYear();
425
- var arrayofLeapYears = [];
426
- for (var loopYear = fromYear; loopYear <= toYear; loopYear++) {
427
- if (loopYear % 4 === 0 && loopYear % 100 !== 0 || loopYear % 100 === 0 && loopYear % 400 === 0) {
428
- arrayofLeapYears.push(loopYear);
429
- }
430
- }
431
- return arrayofLeapYears.length;
432
- }
433
-
434
- function getSuffix(min, ago, later) {
435
- var suffix = void 0;
436
- if (min < 0) {
437
- suffix = ago || '';
438
- } else if (min > 0) {
439
- suffix = later || '';
440
- } else {
441
- suffix = '';
442
- }
443
- return suffix;
444
- }
445
-
446
- function getFormatedDate(to, today, yesterday, tomorrow, others, ago, later, format, titleFormat, titleString, calculateFrom, tzData) {
447
- var fromDateObj = convertUtcToUserTz(calculateFrom, tzData);
448
- var toDateObj = convertUtcToUserTz(to, tzData);
449
-
450
- var diffMin = new Date(to).getTime() - new Date(calculateFrom).getTime();
451
- var suffix = getSuffix(diffMin, ago, later);
452
- var diff = getDiffObj(diffMin);
453
- var withInAWeak = diff.y === 0 && diff.yd <= 7;
454
- var diffObj = {
455
- h: diff.h,
456
- m: diff.m,
457
- s: diff.s,
458
- y: diff.y,
459
- hh: pad(diff.h, 2),
460
- mm: pad(diff.m, 2),
461
- ss: pad(diff.s, 2),
462
- yy: pad(diff.y, 2),
463
- days: diff.yd,
464
- yDays: pad(diff.yd, 2),
465
- isWithInAWeek: withInAWeak,
466
- suffix: suffix
467
- };
468
- var diffObj1 = {
469
- hours: diff.h,
470
- minutes: diff.m,
471
- seconds: diff.s,
472
- years: diff.y,
473
- yDays: diff.yd,
474
- isWithInAWeek: withInAWeak,
475
- suffix: suffix,
476
- crntMonth: new Date(to).getMonth(),
477
- betweenleepYears: getLyears(calculateFrom, to)
478
- };
479
- //var daysDiff = toDateObj.diff(fromDateObj, 'days', true);
480
- var key = '';
481
- var values = [];
482
- var text = null;
483
- var isSuffixEnable = false;
484
- if (format) {
485
- var years = void 0,
486
- months = void 0,
487
- days = void 0,
488
- hours = void 0,
489
- minutes = void 0,
490
- seconds = void 0;
491
- years = diffObj1.years > 1 ? '2' : diffObj1.years;
492
- // months = diffObj1.months > 1 ? '2' : diffObj1.months;
493
- //days = diffObj1.days > 1 ? '2' : diffObj1.days;
494
- days = diff.yd > 1 ? '2' : diff.yd;
495
- hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
496
- minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
497
- //seconds = diffObj1.seconds > 1 ? '2' : diffObj1.seconds;
498
- // let pattern = '' + years + months + days + hours + minutes + seconds;
499
- var count = 0;
500
- var pattern = [years, days, hours, minutes].reduce(function (res, next) {
501
- if (count === 2) {
502
- res = res + '0';
503
- } else if (next !== 0) {
504
- count++;
505
- res = res + next;
506
- } else {
507
- res = res + next;
508
- }
509
- return res;
510
- }, '');
511
- //let pattern = '' + years + days + hours + minutes;
512
- var _value2 = format(diffObj1, pattern);
513
- if (_value2 && (typeof _value2 === 'undefined' ? 'undefined' : _typeof(_value2)) === 'object') {
514
- key = _value2.key;
515
- values = getValues(_value2.params, diffObj);
516
- if (pattern.indexOf('00000') === 0) {
517
- //suffix ignore for second hook
518
- isSuffixEnable = false;
519
- } else {
520
- isSuffixEnable = true;
521
- }
522
- } else if (typeof _value2 === 'string') {
523
- if (_value2 === 'socialFormat') {
524
- var defaultRelativeTime = {
525
- future: function future(t) {
526
- return 'in ' + t;
527
- },
528
- past: function past(t) {
529
- return t + ' ago';
530
- },
531
- present: function present() {
532
- return 'just now';
533
- },
534
- s: function s() {
535
- return 'a few seconds ago';
536
- },
537
- ss: function ss(t) {
538
- return t + ' seconds ago';
539
- },
540
- m: function m() {
541
- return 'a minute ago';
542
- },
543
- mm: function mm(t) {
544
- return t + ' minutes ago';
545
- },
546
- h: function h() {
547
- return 'an hour ago';
548
- },
549
- hh: function hh(t) {
550
- return t + ' hours ago';
551
- },
552
- d: function d() {
553
- return 'a day ago';
554
- },
555
- dd: function dd(t) {
556
- return t + ' days ago';
557
- },
558
- M: function M() {
559
- return 'a month ago';
560
- },
561
- MM: function MM(t) {
562
- return t + ' months ago';
563
- },
564
- y: function y() {
565
- return 'a year ago';
566
- },
567
- yy: function yy(t) {
568
- return t + ' years ago';
569
- }
570
- };
571
- if (diff.yd === 0) {
572
- if (diffObj.h < 1) {
573
- if (diffObj.m < 1) {
574
- if (diffObj.s < 1) {
575
- text = defaultRelativeTime.present();
576
- } else if (diffObj.s === 1) {
577
- text = defaultRelativeTime.s();
578
- } else {
579
- text = defaultRelativeTime.ss(diffObj.s);
580
- }
581
- } else if (diffObj.m === 1) {
582
- text = defaultRelativeTime.m();
583
- } else {
584
- text = defaultRelativeTime.mm(diffObj.mm);
585
- }
586
- } else if (diffObj.h === 1) {
587
- text = defaultRelativeTime.h();
588
- } else {
589
- text = defaultRelativeTime.hh(diffObj.h);
590
- }
591
- } else {
592
- text = formatDate(toDateObj, 'DD MMM YYYY', diffObj1);
593
- }
594
- } else {
595
- text = formatDate(toDateObj, _value2, diffObj1);
596
- }
597
- }
598
- } else {
599
- if (diff.y === 0 && (diff.yd === 0 || diff.yd === 1)) {
600
- var dateObj = new Date(toDateObj);
601
- var curDateObj = new Date(fromDateObj);
602
- if (dateObj.getDate() === curDateObj.getDate()) {
603
- if (typeof today === 'function') {
604
- var value = today(diffObj1);
605
- key = value.key;
606
- values = getValues(value.params, diffObj);
607
- } else if ((typeof today === 'undefined' ? 'undefined' : _typeof(today)) === 'object') {
608
- key = today.key;
609
- values = getValues(today.params, diffObj);
610
- isSuffixEnable = true;
611
- } else if (typeof today === 'string') {
612
- text = formatDate(toDateObj, today);
613
- }
614
- } else if (dateObj.getDate() < curDateObj.getDate()) {
615
- if (typeof yesterday === 'function') {
616
- var value = yesterday(diffObj1);
617
- key = value.key;
618
- values = getValues(value.params, diffObj);
619
- } else if ((typeof yesterday === 'undefined' ? 'undefined' : _typeof(yesterday)) === 'object') {
620
- key = yesterday.key;
621
- values = getValues(yesterday.params, diffObj);
622
- } else if (typeof yesterday === 'string') {
623
- text = formatDate(toDateObj, yesterday);
624
- }
625
- } else if (dateObj.getDate() > curDateObj.getDate()) {
626
- if (typeof tomorrow === 'function') {
627
- var value = tomorrow(diffObj1);
628
- key = value.key;
629
- text = getValues(value.params, diffObj);
630
- } else if ((typeof tomorrow === 'undefined' ? 'undefined' : _typeof(tomorrow)) === 'object') {
631
- key = tomorrow.key;
632
- values = getValues(tomorrow.params, diffObj);
633
- } else if (typeof tomorrow === 'string') {
634
- text = formatDate(toDateObj, tomorrow);
635
- }
636
- }
637
- } else {
638
- var value = others(diffObj1);
639
- if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
640
- key = value.key;
641
- values = getValues(value.params, diffObj);
642
- isSuffixEnable = true;
643
- } else if (typeof value === 'string') {
644
- text = formatDate(toDateObj, value);
645
- }
646
- }
647
- }
648
- var i18nText = isSuffixEnable && suffix !== '' ? key + '.' + suffix : key;
649
- var titleFormated = titleString === undefined ? formatDate(to, titleFormat) : titleString;
650
- return { formatedDate: text, values: values, i18nText: i18nText, title: titleFormated };
651
- }
@@ -1,60 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
-
4
- import { getFormatedDate } from '../utils';
5
- import FormatText from './FormatText';
6
- import { I18NContext } from '../I18NContext';
7
-
8
- export default function NewDateFormat(props, context) {
9
- let currentTime = new Date();
10
- let {
11
- to,
12
- today,
13
- yesterday,
14
- tomorrow,
15
- others,
16
- ago,
17
- later,
18
- format,
19
- titleFormat,
20
- titleString,
21
- className,
22
- displayType,
23
- calculateFrom
24
- } = props;
25
- calculateFrom = calculateFrom === undefined ? currentTime : calculateFrom;
26
- let { tzData } = context;
27
- let formatResponse = getFormatedDate(
28
- to,
29
- today,
30
- yesterday,
31
- tomorrow,
32
- others,
33
- ago,
34
- later,
35
- format,
36
- titleFormat,
37
- titleString,
38
- calculateFrom,
39
- tzData
40
- );
41
- let isI18nValue = formatResponse.isFormatText;
42
- if (isI18nValue) {
43
- let { i18nText, values, title } = formatResponse;
44
- return (
45
- <FormatText
46
- i18NKey={i18nText}
47
- values={values}
48
- className={className}
49
- data-title={title}
50
- />
51
- );
52
- }
53
- let { title, formatedDate } = formatResponse;
54
- return (
55
- <span className={className} data-title={title}>
56
- {formatedDate}
57
- </span>
58
- );
59
- }
60
- NewDateFormat.contextType = I18NContext;