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