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