@zohodesk/i18n 1.0.0-beta.2 → 1.0.0-beta.21

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 (67) hide show
  1. package/README.md +80 -0
  2. package/es/I18NContext.js +1 -2
  3. package/es/components/DateTimeDiffFormat.js +192 -184
  4. package/es/components/FormatText.js +4 -25
  5. package/es/components/HOCI18N.js +33 -45
  6. package/es/components/I18N.js +46 -63
  7. package/es/components/I18NProvider.js +54 -84
  8. package/es/components/PluralFormat.js +29 -48
  9. package/es/components/UserTimeDiffFormat.js +65 -70
  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.js +250 -385
  19. package/lib/I18NContext.js +6 -6
  20. package/lib/components/DateTimeDiffFormat.js +167 -123
  21. package/lib/components/FormatText.js +32 -22
  22. package/lib/components/HOCI18N.js +47 -23
  23. package/lib/components/I18N.js +60 -36
  24. package/lib/components/I18NProvider.js +76 -69
  25. package/lib/components/PluralFormat.js +42 -32
  26. package/lib/components/UserTimeDiffFormat.js +81 -54
  27. package/lib/components/__tests__/DateTimeDiffFormat.spec.js +815 -629
  28. package/lib/components/__tests__/FormatText.spec.js +23 -25
  29. package/lib/components/__tests__/HOCI18N.spec.js +26 -34
  30. package/lib/components/__tests__/I18N.spec.js +21 -26
  31. package/lib/components/__tests__/I18NProvider.spec.js +43 -51
  32. package/lib/components/__tests__/PluralFormat.spec.js +24 -28
  33. package/lib/components/__tests__/UserTimeDiffFormat.spec.js +1256 -1039
  34. package/lib/index.js +73 -119
  35. package/lib/utils.js +222 -329
  36. package/package.json +2 -2
  37. package/src/I18NContext.js +3 -0
  38. package/src/components/DateTimeDiffFormat.js +256 -0
  39. package/src/components/FormatText.js +14 -0
  40. package/src/components/HOCI18N.js +37 -0
  41. package/src/components/I18N.js +72 -0
  42. package/src/components/I18NProvider.js +110 -0
  43. package/src/components/PluralFormat.js +37 -0
  44. package/src/components/UserTimeDiffFormat.js +97 -0
  45. package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -0
  46. package/src/components/__tests__/FormatText.spec.js +27 -0
  47. package/src/components/__tests__/HOCI18N.spec.js +33 -0
  48. package/src/components/__tests__/I18N.spec.js +30 -0
  49. package/src/components/__tests__/I18NProvider.spec.js +65 -0
  50. package/src/components/__tests__/PluralFormat.spec.js +28 -0
  51. package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -0
  52. package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -0
  53. package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -0
  54. package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -0
  55. package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -0
  56. package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -0
  57. package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -0
  58. package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -0
  59. package/src/index.js +33 -0
  60. package/src/utils.js +527 -0
  61. package/.DS_Store +0 -0
  62. package/es/components/NewDateFormat.js +0 -50
  63. package/es/offset.js +0 -629
  64. package/es/timezones.js +0 -112
  65. package/lib/components/NewDateFormat.js +0 -68
  66. package/lib/offset.js +0 -634
  67. package/lib/timezones.js +0 -120
package/src/index.js ADDED
@@ -0,0 +1,33 @@
1
+ export {
2
+ formatDate,
3
+ pad,
4
+ replaceI18NValuesWithRegex,
5
+ unescapeUnicode,
6
+ getValues,
7
+ getI18NInfo,
8
+ isToday,
9
+ isYesterday,
10
+ isTomorrow,
11
+ isWithinAWeek,
12
+ isTwoWeeksOrMore,
13
+ userDateFormat,
14
+ getDiffObj,
15
+ getLyears,
16
+ getSuffix,
17
+ getDatePatternWithoutYear,
18
+ setLocalizedData
19
+ } from './utils';
20
+ import { getI18NValue as getI18NValue1 } from './utils';
21
+ export { I18NContext } from './I18NContext';
22
+ export {
23
+ default as I18NProvider,
24
+ i18NProviderUtils,
25
+ } from './components/I18NProvider';
26
+ export { default as I18N } from './components/I18N';
27
+ export { default as HOCI18N } from './components/HOCI18N';
28
+ export { default as FormatText } from './components/FormatText';
29
+ export { default as PluralFormat } from './components/PluralFormat';
30
+ export { default as DateTimeDiffFormat } from './components/DateTimeDiffFormat';
31
+ export { default as UserTimeDiffFormat } from './components/UserTimeDiffFormat';
32
+ export const getI18NValue = (i18n, key, values) =>
33
+ getI18NValue1(i18n)(key, values);
package/src/utils.js ADDED
@@ -0,0 +1,527 @@
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
+ }
package/.DS_Store DELETED
Binary file
@@ -1,50 +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
- var currentTime = new Date();
10
- var to = props.to,
11
- today = props.today,
12
- yesterday = props.yesterday,
13
- tomorrow = props.tomorrow,
14
- others = props.others,
15
- ago = props.ago,
16
- later = props.later,
17
- format = props.format,
18
- titleFormat = props.titleFormat,
19
- titleString = props.titleString,
20
- className = props.className,
21
- displayType = props.displayType,
22
- calculateFrom = props.calculateFrom;
23
-
24
- calculateFrom = calculateFrom === undefined ? currentTime : calculateFrom;
25
- var tzData = context.tzData;
26
-
27
- var formatResponse = getFormatedDate(to, today, yesterday, tomorrow, others, ago, later, format, titleFormat, titleString, calculateFrom, tzData);
28
- var isI18nValue = formatResponse.isFormatText;
29
- if (isI18nValue) {
30
- var i18nText = formatResponse.i18nText,
31
- values = formatResponse.values,
32
- _title = formatResponse.title;
33
-
34
- return React.createElement(FormatText, {
35
- i18NKey: i18nText,
36
- values: values,
37
- className: className,
38
- 'data-title': _title
39
- });
40
- }
41
- var title = formatResponse.title,
42
- formatedDate = formatResponse.formatedDate;
43
-
44
- return React.createElement(
45
- 'span',
46
- { className: className, 'data-title': title },
47
- formatedDate
48
- );
49
- }
50
- NewDateFormat.contextType = I18NContext;