@zohodesk/i18n 1.0.0-beta.2 → 1.0.0-beta.20
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 +80 -5
- package/es/I18NContext.js +1 -2
- package/es/components/DateTimeDiffFormat.js +191 -184
- package/es/components/FormatText.js +4 -25
- package/es/components/HOCI18N.js +33 -45
- package/es/components/I18N.js +46 -63
- package/es/components/I18NProvider.js +54 -84
- package/es/components/PluralFormat.js +29 -48
- package/es/components/UserTimeDiffFormat.js +65 -70
- 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/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/es/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/es/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/es/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/es/index.js +2 -6
- package/es/utils.js +250 -385
- package/lib/I18NContext.js +6 -6
- package/lib/components/DateTimeDiffFormat.js +166 -123
- package/lib/components/FormatText.js +32 -22
- package/lib/components/HOCI18N.js +47 -23
- package/lib/components/I18N.js +60 -36
- package/lib/components/I18NProvider.js +76 -69
- package/lib/components/PluralFormat.js +42 -32
- package/lib/components/UserTimeDiffFormat.js +81 -54
- 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/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/lib/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/lib/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/lib/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/lib/index.js +73 -119
- package/lib/utils.js +222 -329
- package/package.json +29 -29
- package/src/I18NContext.js +3 -0
- package/src/components/DateTimeDiffFormat.js +256 -0
- package/src/components/FormatText.js +14 -0
- package/src/components/HOCI18N.js +37 -0
- package/src/components/I18N.js +72 -0
- package/src/components/I18NProvider.js +110 -0
- package/src/components/PluralFormat.js +37 -0
- package/src/components/UserTimeDiffFormat.js +97 -0
- package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -0
- package/src/components/__tests__/FormatText.spec.js +27 -0
- package/src/components/__tests__/HOCI18N.spec.js +33 -0
- package/src/components/__tests__/I18N.spec.js +30 -0
- package/src/components/__tests__/I18NProvider.spec.js +65 -0
- package/src/components/__tests__/PluralFormat.spec.js +28 -0
- package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -0
- package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -0
- package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -0
- package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -0
- package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -0
- package/src/index.js +33 -0
- package/src/utils.js +527 -0
- package/.DS_Store +0 -0
- package/es/components/NewDateFormat.js +0 -50
- package/es/offset.js +0 -629
- package/es/timezones.js +0 -112
- package/lib/components/NewDateFormat.js +0 -68
- package/lib/offset.js +0 -634
- package/lib/timezones.js +0 -120
package/es/utils.js
CHANGED
|
@@ -1,57 +1,60 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { getTimezoneOffset } from './timezones';
|
|
4
|
-
import { offsetMap } from './offset';
|
|
5
|
-
|
|
6
|
-
export var browserOffset = -new Date().getTimezoneOffset();
|
|
7
|
-
var dateFormat = {
|
|
1
|
+
import datetime from '@zohodesk/datetimejs';
|
|
2
|
+
let dateFormat = {
|
|
8
3
|
dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
|
|
9
4
|
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
5
|
timeNames: ['a', 'p', 'am', 'pm', 'A', 'P', 'AM', 'PM']
|
|
11
6
|
};
|
|
12
7
|
export function formatDate(dateMill, mask, dateDiffObj) {
|
|
13
8
|
if (mask === 'facebookFormat') {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
let {
|
|
10
|
+
hours,
|
|
11
|
+
minutes,
|
|
12
|
+
seconds,
|
|
13
|
+
yDays,
|
|
14
|
+
years,
|
|
15
|
+
betweenleepYears,
|
|
16
|
+
crntMonth
|
|
17
|
+
} = dateDiffObj;
|
|
21
18
|
|
|
22
19
|
if (years > 0) {
|
|
23
|
-
return years
|
|
20
|
+
return `${years}y`;
|
|
24
21
|
} else if (yDays > 0) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
let isFeb = crntMonth === 1;
|
|
23
|
+
let isThoneMonth = [0, 2, 4, 6, 7, 9, 11].indexOf(crntMonth) != -1;
|
|
24
|
+
|
|
25
|
+
let weekCal = function (calDays) {
|
|
26
|
+
let weeks = calDays / 7;
|
|
27
|
+
return weeks < 1 ? `${calDays}d` : `${Math.trunc(weeks)}w`;
|
|
30
28
|
};
|
|
29
|
+
|
|
31
30
|
if (!isFeb) {
|
|
32
31
|
return isThoneMonth === true ? yDays === 31 ? '1m' : weekCal(yDays) : yDays === 30 ? '1m' : weekCal(yDays);
|
|
33
32
|
}
|
|
33
|
+
|
|
34
34
|
if (betweenleepYears != 0) {
|
|
35
35
|
return isFeb === true && yDays === 28 ? '1m' : weekCal(yDays);
|
|
36
36
|
}
|
|
37
|
+
|
|
37
38
|
return isFeb === true && yDays === 29 ? '1m' : weekCal(yDays);
|
|
38
39
|
} else if (hours < 24 && hours > 0) {
|
|
39
|
-
return hours
|
|
40
|
+
return `${hours}h`;
|
|
40
41
|
} else if (minutes < 60 && hours > 1) {
|
|
41
|
-
return minutes
|
|
42
|
+
return `${minutes}m`;
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
+
|
|
45
|
+
return seconds === 0 ? 'now' : `${seconds}s`;
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
|
|
48
|
+
let date = new Date(dateMill);
|
|
49
|
+
let d = date.getDate();
|
|
50
|
+
let D = date.getDay();
|
|
51
|
+
let m = date.getMonth();
|
|
52
|
+
let y = date.getFullYear();
|
|
53
|
+
let H = date.getHours();
|
|
54
|
+
let M = date.getMinutes();
|
|
55
|
+
let s = date.getSeconds();
|
|
56
|
+
let L = date.getMilliseconds();
|
|
57
|
+
let flags = {
|
|
55
58
|
d: d,
|
|
56
59
|
dd: pad(d, 2),
|
|
57
60
|
ddd: dateFormat.dayNames[D],
|
|
@@ -82,160 +85,175 @@ export function formatDate(dateMill, mask, dateDiffObj) {
|
|
|
82
85
|
A: H < 12 ? dateFormat.timeNames[6] : dateFormat.timeNames[7],
|
|
83
86
|
T: H < 12 ? dateFormat.timeNames[4] : dateFormat.timeNames[5]
|
|
84
87
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
var dat = mask.replace(token, function (match) {
|
|
88
|
+
let token = /D{1,4}|M{1,4}|YY(?:YY)?|([HhmsA])\1?|[LloSZWN]|\[[^\]]*\]|'[^']*'/gi;
|
|
89
|
+
let dat = mask.replace(token, match => {
|
|
88
90
|
if (match in flags) {
|
|
89
91
|
return flags[match];
|
|
90
92
|
}
|
|
93
|
+
|
|
91
94
|
return match.slice(1, match.length - 1);
|
|
92
95
|
});
|
|
93
96
|
return dat;
|
|
94
97
|
}
|
|
95
98
|
export function pad(n) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
let width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
100
|
+
let z = arguments.length > 2 ? arguments[2] : undefined;
|
|
99
101
|
z = z || '0';
|
|
100
|
-
n =
|
|
102
|
+
n = `${n}`;
|
|
101
103
|
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
102
104
|
}
|
|
103
105
|
export function replaceI18NValuesWithRegex(i18nStr, values) {
|
|
104
106
|
if (typeof values !== 'undefined') {
|
|
105
107
|
if (Array.isArray(values)) {
|
|
106
|
-
for (
|
|
107
|
-
i18nStr = i18nStr.replace(new RegExp(
|
|
108
|
+
for (let i = 0; i < values.length; i++) {
|
|
109
|
+
i18nStr = i18nStr.replace(new RegExp(`\\{${i}\\}`, 'g'), values[i]);
|
|
108
110
|
}
|
|
109
111
|
} else {
|
|
110
112
|
i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values);
|
|
111
113
|
}
|
|
112
114
|
}
|
|
115
|
+
|
|
113
116
|
return i18nStr;
|
|
114
117
|
}
|
|
115
|
-
|
|
116
118
|
export function unescapeUnicode(str) {
|
|
117
|
-
return str.replace(/\\u([a-fA-F0-9]{4})/g,
|
|
118
|
-
return String.fromCharCode(parseInt(m1, 16));
|
|
119
|
-
});
|
|
119
|
+
return str.replace(/\\u([a-fA-F0-9]{4})/g, (g, m1) => String.fromCharCode(parseInt(m1, 16)));
|
|
120
120
|
}
|
|
121
121
|
export function getValues() {
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
let params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
123
|
+
let diff = arguments.length > 1 ? arguments[1] : undefined;
|
|
124
|
+
return params.map(param => diff[param]);
|
|
125
|
+
}
|
|
126
|
+
let localizedData = {};
|
|
127
|
+
export function setLocalizedData(data) {
|
|
128
|
+
localizedData = data;
|
|
129
|
+
}
|
|
130
|
+
export function getLocalizedValue() {
|
|
131
|
+
let {
|
|
132
|
+
type,
|
|
133
|
+
moduleName,
|
|
134
|
+
apiName,
|
|
135
|
+
fieldValue
|
|
136
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
137
|
+
let localizedValue = localizedData;
|
|
124
138
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
switch (type) {
|
|
140
|
+
case 'field':
|
|
141
|
+
['Field', moduleName, apiName].map(key => {
|
|
142
|
+
localizedValue = localizedValue[key] || '';
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
|
|
146
|
+
case 'picklist':
|
|
147
|
+
['PickListValue', moduleName, apiName, fieldValue].map(key => {
|
|
148
|
+
localizedValue = localizedValue[key] || '';
|
|
149
|
+
});
|
|
150
|
+
break;
|
|
151
|
+
|
|
152
|
+
default:
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return localizedValue || null;
|
|
128
157
|
}
|
|
129
158
|
export function getI18NValue(i18n) {
|
|
130
159
|
if (typeof i18n === 'undefined') {
|
|
131
|
-
return
|
|
132
|
-
return key;
|
|
133
|
-
};
|
|
160
|
+
return key => key;
|
|
134
161
|
}
|
|
135
|
-
|
|
136
|
-
|
|
162
|
+
|
|
163
|
+
return (key, values, localizedProps) => {
|
|
164
|
+
const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : null;
|
|
165
|
+
let i18nStr = i18n[key];
|
|
166
|
+
|
|
137
167
|
if (i18nStr === undefined) {
|
|
138
|
-
return key;
|
|
168
|
+
return localizedValue || key;
|
|
139
169
|
}
|
|
170
|
+
|
|
140
171
|
i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
|
|
141
|
-
return unescapeUnicode(i18nStr);
|
|
172
|
+
return localizedValue || unescapeUnicode(i18nStr);
|
|
142
173
|
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// function getValues(params = [], diff) {
|
|
174
|
+
} // function getValues(params = [], diff) {
|
|
146
175
|
// return params.map(param => {
|
|
147
176
|
// return diff[param];
|
|
148
177
|
// });
|
|
149
178
|
// }
|
|
179
|
+
|
|
150
180
|
export function getI18NInfo(toDateObj, props, diffObj) {
|
|
151
|
-
|
|
152
|
-
values
|
|
181
|
+
let key = null,
|
|
182
|
+
values,
|
|
153
183
|
text = null;
|
|
184
|
+
|
|
154
185
|
if (typeof props === 'function') {
|
|
155
|
-
|
|
186
|
+
let value = props(diffObj1);
|
|
156
187
|
key = value.key;
|
|
157
188
|
values = getValues(value.params, diffObj);
|
|
158
|
-
} else if (
|
|
189
|
+
} else if (typeof props === 'object') {
|
|
159
190
|
key = props.key;
|
|
160
191
|
values = getValues(props.params, diffObj);
|
|
161
192
|
} else if (typeof props === 'string') {
|
|
162
193
|
text = toDateObj.format(props);
|
|
163
194
|
}
|
|
164
|
-
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
key,
|
|
198
|
+
values,
|
|
199
|
+
text
|
|
200
|
+
};
|
|
165
201
|
}
|
|
166
202
|
export function isToday(fromDate, toDate) {
|
|
167
|
-
|
|
203
|
+
let TODAY = toDate.clone().startOf('day');
|
|
168
204
|
return fromDate.isSame(TODAY, 'd');
|
|
169
205
|
}
|
|
170
206
|
export function isYesterday(fromDate, toDate) {
|
|
171
|
-
|
|
207
|
+
let YESTERDAY = toDate.clone().subtract(1, 'days').startOf('day');
|
|
172
208
|
return fromDate.isSame(YESTERDAY, 'd');
|
|
173
209
|
}
|
|
174
210
|
export function isTomorrow(fromDate, toDate) {
|
|
175
211
|
return isYesterday(toDate, fromDate);
|
|
176
212
|
}
|
|
177
213
|
export function isWithinAWeek(fromDate, toDate) {
|
|
178
|
-
|
|
214
|
+
let A_WEEK_OLD = toDate.clone().subtract(7, 'days').startOf('day');
|
|
179
215
|
return fromDate.isAfter(A_WEEK_OLD);
|
|
180
216
|
}
|
|
181
217
|
export function isTwoWeeksOrMore(fromDate, toDate) {
|
|
182
218
|
return !isWithinAWeek(fromDate, toDate);
|
|
183
219
|
}
|
|
220
|
+
export function getDatePatternWithoutYear(datePattern) {
|
|
221
|
+
let dateObj;
|
|
222
|
+
let delemiter;
|
|
184
223
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
}
|
|
224
|
+
if (datePattern.indexOf('.') !== -1) {
|
|
225
|
+
dateObj = datePattern.split('.');
|
|
226
|
+
delemiter = '.';
|
|
227
|
+
} else if (datePattern.indexOf('-') !== -1) {
|
|
228
|
+
dateObj = datePattern.split('-');
|
|
229
|
+
delemiter = '-';
|
|
230
|
+
} else if (datePattern.indexOf('/') !== -1) {
|
|
231
|
+
dateObj = datePattern.split('/');
|
|
232
|
+
delemiter = '/';
|
|
233
|
+
} else {
|
|
234
|
+
dateObj = datePattern.split(' ');
|
|
235
|
+
delemiter = ' ';
|
|
205
236
|
}
|
|
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
237
|
|
|
216
|
-
|
|
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);
|
|
238
|
+
return dateObj.filter(data => ['yy', 'yyyy'].indexOf(data) === -1).join(delemiter);
|
|
223
239
|
}
|
|
224
|
-
|
|
225
|
-
export function userDateFormat(getI18NValue, tzData) {
|
|
240
|
+
export function userDateFormat(getI18NValue, timezoneData, timeFormat, datePattern, isEnabledCurrentYear) {
|
|
226
241
|
return function (to, _ref, ago, later) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
242
|
+
let {
|
|
243
|
+
today,
|
|
244
|
+
yesterday,
|
|
245
|
+
tomorrow,
|
|
246
|
+
others
|
|
247
|
+
} = _ref;
|
|
248
|
+
let isSuffixEnable = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
249
|
+
let format = arguments.length > 5 ? arguments[5] : undefined;
|
|
250
|
+
let fromDateObj = datetime.toDate(datetime.tz.utcToTz(null, timezoneData));
|
|
251
|
+
let toDateObj = datetime.toDate(datetime.tz.utcToTz(to, timezoneData));
|
|
252
|
+
let diffMin = new Date(to).getTime() - new Date().getTime();
|
|
253
|
+
let from = new Date();
|
|
254
|
+
from = from.toISOString();
|
|
255
|
+
let suffix;
|
|
256
|
+
|
|
239
257
|
if (diffMin < 0) {
|
|
240
258
|
suffix = ago || '';
|
|
241
259
|
} else if (diffMin > 0) {
|
|
@@ -243,9 +261,10 @@ export function userDateFormat(getI18NValue, tzData) {
|
|
|
243
261
|
} else {
|
|
244
262
|
suffix = '';
|
|
245
263
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
264
|
+
|
|
265
|
+
let diff = getDiffObj(diffMin);
|
|
266
|
+
let withInAWeak = diff.y === 0 && diff.yd <= 7;
|
|
267
|
+
let diffObj = {
|
|
249
268
|
h: diff.h,
|
|
250
269
|
m: diff.m,
|
|
251
270
|
s: diff.s,
|
|
@@ -259,50 +278,62 @@ export function userDateFormat(getI18NValue, tzData) {
|
|
|
259
278
|
isWithInAWeek: withInAWeak,
|
|
260
279
|
suffix: suffix
|
|
261
280
|
};
|
|
262
|
-
|
|
281
|
+
let diffObj1 = {
|
|
263
282
|
hours: diff.h,
|
|
264
283
|
minutes: diff.m,
|
|
265
284
|
seconds: diff.s,
|
|
266
285
|
years: diff.y,
|
|
267
286
|
yDays: diff.yd,
|
|
287
|
+
crntYear: new Date(from).getFullYear(),
|
|
288
|
+
tYear: new Date(to).getFullYear(),
|
|
268
289
|
isWithInAWeek: withInAWeak,
|
|
269
|
-
suffix: suffix
|
|
270
|
-
|
|
271
|
-
|
|
290
|
+
suffix: suffix,
|
|
291
|
+
timeFormat: timeFormat,
|
|
292
|
+
datePattern: datePattern,
|
|
293
|
+
dateTimePattern: `${datePattern} ${timeFormat}`
|
|
294
|
+
}; //In if condition we'll remove year and set date format if the current year is not required
|
|
295
|
+
//In else part we'll set the date format as it is
|
|
296
|
+
|
|
297
|
+
if (isEnabledCurrentYear === true && diffObj1.years === 0 && diffObj1.tYear === diffObj1.crntYear) {
|
|
298
|
+
let dateFormat = getDatePatternWithoutYear(datePattern);
|
|
299
|
+
diffObj1.dateFormat = dateFormat;
|
|
300
|
+
diffObj1.dateTimeFormat = `${dateFormat} ${timeFormat}`;
|
|
301
|
+
} else {
|
|
302
|
+
diffObj1.dateFormat = datePattern;
|
|
303
|
+
diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
|
|
304
|
+
} //var daysDiff = toDateObj.diff(fromDateObj, 'days');
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
let key = '';
|
|
308
|
+
let values = [];
|
|
309
|
+
let text = null;
|
|
272
310
|
|
|
273
|
-
var key = '';
|
|
274
|
-
var values = [];
|
|
275
|
-
var text = null;
|
|
276
311
|
if (format) {
|
|
277
|
-
|
|
278
|
-
|
|
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;
|
|
312
|
+
let years, months, days, hours, minutes, seconds;
|
|
313
|
+
years = diffObj1.years > 1 ? '2' : diffObj1.years; // months = diffObj1.months > 1 ? '2' : diffObj1.months;
|
|
286
314
|
// days = diffObj1.days > 1 ? '2' : diffObj1.days;
|
|
315
|
+
|
|
287
316
|
days = diff.yd > 1 ? '2' : diff.yd;
|
|
288
317
|
hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
|
|
289
|
-
minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
|
|
290
|
-
//seconds = diffObj1.seconds > 1 ? '2' : diffObj1.seconds;
|
|
318
|
+
minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes; //seconds = diffObj1.seconds > 1 ? '2' : diffObj1.seconds;
|
|
291
319
|
// let pattern = '' + years + months + days + hours + minutes + seconds;
|
|
292
|
-
|
|
293
|
-
|
|
320
|
+
|
|
321
|
+
let count = 0;
|
|
322
|
+
let pattern = [years, days, hours, minutes].reduce((res, next) => {
|
|
294
323
|
if (count === 2 || next === 0) {
|
|
295
|
-
res = res
|
|
324
|
+
res = `${res}0`;
|
|
296
325
|
} else if (next !== 0) {
|
|
297
326
|
count++;
|
|
298
327
|
res = res + next;
|
|
299
328
|
} else {
|
|
300
329
|
res = res + next;
|
|
301
330
|
}
|
|
331
|
+
|
|
302
332
|
return res;
|
|
303
333
|
}, '');
|
|
304
|
-
|
|
305
|
-
|
|
334
|
+
let value = format(diffObj1, pattern);
|
|
335
|
+
|
|
336
|
+
if (value && typeof value === 'object') {
|
|
306
337
|
key = value.key;
|
|
307
338
|
values = getValues(value.params, diffObj);
|
|
308
339
|
isSuffixEnable = true;
|
|
@@ -310,69 +341,108 @@ export function userDateFormat(getI18NValue, tzData) {
|
|
|
310
341
|
text = formatDate(toDateObj, value);
|
|
311
342
|
}
|
|
312
343
|
} else {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
344
|
+
let dateObj = new Date(toDateObj);
|
|
345
|
+
let curDateObj = new Date(fromDateObj);
|
|
346
|
+
let diffDayType = diffObj1.yDays; //In this condition, to calculate different days we have copied it from live --> diffDayType
|
|
347
|
+
|
|
348
|
+
if (isOverdue && dateObj.getDate() < curDateObj.getDate() && diffObj1.yDays == 0) {
|
|
349
|
+
diffDayType = -1;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (!isOverdue) {
|
|
353
|
+
let diffHr = dateObj.getHours() - curDateObj.getHours();
|
|
354
|
+
|
|
355
|
+
if (diffHr < 0) {
|
|
356
|
+
diffDayType += 1;
|
|
357
|
+
} else if (diffHr == 0) {
|
|
358
|
+
let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
|
|
359
|
+
|
|
360
|
+
if (diffMins < 0) {
|
|
361
|
+
diffDayType += 1;
|
|
362
|
+
} else if (diffMins == 0) {
|
|
363
|
+
let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
|
|
364
|
+
|
|
365
|
+
if (diffSec < 0) {
|
|
366
|
+
diffDayType += 1;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
|
|
373
|
+
if (today && dateObj.getDate() === curDateObj.getDate()) {
|
|
374
|
+
if (typeof today === 'object') {
|
|
318
375
|
key = today.key;
|
|
319
376
|
values = getValues(today.params, diffObj);
|
|
320
377
|
isSuffixEnable = true;
|
|
321
378
|
} else if (typeof today === 'string') {
|
|
322
379
|
text = formatDate(toDateObj, today);
|
|
323
380
|
}
|
|
324
|
-
} else if (dateObj.getDate() < curDateObj.getDate()) {
|
|
325
|
-
if (
|
|
381
|
+
} else if (yesterday && dateObj.getMonth() === curDateObj.getMonth() && dateObj.getDate() < curDateObj.getDate() || yesterday && dateObj.getMonth() < curDateObj.getMonth()) {
|
|
382
|
+
if (typeof yesterday === 'object') {
|
|
326
383
|
key = yesterday.key;
|
|
327
384
|
values = getValues(yesterday.params, diffObj);
|
|
328
385
|
} else if (typeof yesterday === 'string') {
|
|
329
386
|
text = formatDate(toDateObj, yesterday);
|
|
330
387
|
}
|
|
331
|
-
} else if (
|
|
332
|
-
if (
|
|
388
|
+
} else if (tomorrow && !isOverdue && diff.y === 0 && diffDayType === 1) {
|
|
389
|
+
if (typeof tomorrow === 'object') {
|
|
333
390
|
key = tomorrow.key;
|
|
334
391
|
values = getValues(tomorrow.params, diffObj);
|
|
335
392
|
} else if (typeof tomorrow === 'string') {
|
|
336
393
|
text = formatDate(toDateObj, tomorrow);
|
|
337
394
|
}
|
|
395
|
+
} else {
|
|
396
|
+
let value = others(diffObj1);
|
|
397
|
+
|
|
398
|
+
if (typeof value === 'object') {
|
|
399
|
+
key = value.key;
|
|
400
|
+
values = getValues(value.params, diffObj);
|
|
401
|
+
isSuffixEnable = true;
|
|
402
|
+
} else if (typeof value === 'string') {
|
|
403
|
+
text = formatDate(toDateObj, value);
|
|
404
|
+
}
|
|
338
405
|
}
|
|
339
406
|
} else {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
407
|
+
let value = others(diffObj1);
|
|
408
|
+
|
|
409
|
+
if (typeof value === 'object') {
|
|
410
|
+
key = value.key;
|
|
411
|
+
values = getValues(value.params, diffObj);
|
|
344
412
|
isSuffixEnable = true;
|
|
345
|
-
} else if (typeof
|
|
346
|
-
text = formatDate(toDateObj,
|
|
413
|
+
} else if (typeof value === 'string') {
|
|
414
|
+
text = formatDate(toDateObj, value);
|
|
347
415
|
}
|
|
348
416
|
}
|
|
349
417
|
}
|
|
350
|
-
|
|
418
|
+
|
|
419
|
+
let key1 = isSuffixEnable && suffix != '' ? `${key}.${suffix}` : key;
|
|
351
420
|
return text || getI18NValue(key1, values);
|
|
352
421
|
};
|
|
353
422
|
}
|
|
423
|
+
let oneYearInMillis = 31536000000;
|
|
424
|
+
let oneDayInMillis = 86400000;
|
|
425
|
+
let oneHourInMillis = 3600000;
|
|
426
|
+
let oneMinuteInMillis = 60000;
|
|
354
427
|
|
|
355
|
-
var oneYearInMillis = 31536000000;
|
|
356
|
-
var oneDayInMillis = 86400000;
|
|
357
|
-
var oneHourInMillis = 3600000;
|
|
358
|
-
var oneMinuteInMillis = 60000;
|
|
359
428
|
function convertAsNonExponential(number) {
|
|
360
429
|
if (number.toString().toLowerCase().indexOf('e') !== -1) {
|
|
361
430
|
return number.toFixed(20);
|
|
362
431
|
}
|
|
432
|
+
|
|
363
433
|
return number;
|
|
364
434
|
}
|
|
365
435
|
|
|
366
436
|
export function getDiffObj(diff) {
|
|
367
437
|
diff = Math.abs(diff);
|
|
368
|
-
|
|
438
|
+
let diffYears = diff / oneYearInMillis;
|
|
369
439
|
diffYears = convertAsNonExponential(diffYears);
|
|
370
|
-
|
|
440
|
+
let diffDays = diff % oneYearInMillis / oneDayInMillis;
|
|
371
441
|
diffDays = convertAsNonExponential(diffDays);
|
|
372
|
-
|
|
442
|
+
let diffHours = diff % oneDayInMillis / oneHourInMillis;
|
|
373
443
|
diffHours = convertAsNonExponential(diffHours);
|
|
374
|
-
|
|
375
|
-
|
|
444
|
+
let diffMinutes = diff % oneDayInMillis % oneHourInMillis / oneMinuteInMillis;
|
|
445
|
+
let diffSeconds = diff % oneDayInMillis % oneHourInMillis % oneMinuteInMillis / 1000;
|
|
376
446
|
diffDays = parseInt(diffDays);
|
|
377
447
|
diffHours = parseInt(diffHours);
|
|
378
448
|
diffMinutes = parseInt(diffMinutes);
|
|
@@ -386,23 +456,24 @@ export function getDiffObj(diff) {
|
|
|
386
456
|
s: diffSeconds
|
|
387
457
|
};
|
|
388
458
|
}
|
|
389
|
-
|
|
390
459
|
export function getLyears(from, to) {
|
|
391
|
-
|
|
460
|
+
let dFrom = new Date(from),
|
|
392
461
|
dTo = new Date(to);
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
462
|
+
let fromYear = dFrom.getFullYear();
|
|
463
|
+
let toYear = dTo.getFullYear();
|
|
464
|
+
let arrayofLeapYears = [];
|
|
465
|
+
|
|
466
|
+
for (let loopYear = fromYear; loopYear <= toYear; loopYear++) {
|
|
397
467
|
if (loopYear % 4 === 0 && loopYear % 100 !== 0 || loopYear % 100 === 0 && loopYear % 400 === 0) {
|
|
398
468
|
arrayofLeapYears.push(loopYear);
|
|
399
469
|
}
|
|
400
470
|
}
|
|
471
|
+
|
|
401
472
|
return arrayofLeapYears.length;
|
|
402
473
|
}
|
|
403
|
-
|
|
404
474
|
export function getSuffix(min, ago, later) {
|
|
405
|
-
|
|
475
|
+
let suffix;
|
|
476
|
+
|
|
406
477
|
if (min < 0) {
|
|
407
478
|
suffix = ago || '';
|
|
408
479
|
} else if (min > 0) {
|
|
@@ -410,212 +481,6 @@ export function getSuffix(min, ago, later) {
|
|
|
410
481
|
} else {
|
|
411
482
|
suffix = '';
|
|
412
483
|
}
|
|
413
|
-
return suffix;
|
|
414
|
-
}
|
|
415
484
|
|
|
416
|
-
|
|
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 };
|
|
485
|
+
return suffix;
|
|
621
486
|
}
|