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

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