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