@ui5/webcomponents-localization 1.7.3 → 1.7.4
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/CHANGELOG.md +8 -0
- package/global.d.ts +105 -0
- package/package.json +3 -3
- package/src/DateFormat.ts +3 -0
- package/src/LocaleData.ts +8 -0
- package/src/dates/CalendarDate.ts +231 -0
- package/src/getCachedLocaleDataInstance.ts +14 -0
package/CHANGELOG.md
CHANGED
@@ -3,6 +3,14 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
5
|
|
6
|
+
## [1.7.4](https://github.com/SAP/ui5-webcomponents/compare/v1.7.3...v1.7.4) (2022-12-05)
|
7
|
+
|
8
|
+
**Note:** Version bump only for package @ui5/webcomponents-localization
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
6
14
|
## [1.7.2](https://github.com/SAP/ui5-webcomponents/compare/v1.7.1...v1.7.2) (2022-11-07)
|
7
15
|
|
8
16
|
**Note:** Version bump only for package @ui5/webcomponents-localization
|
package/global.d.ts
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
// eslint-disable-next-line
|
2
|
+
import "@ui5/webcomponents-base/global";
|
3
|
+
import LocaleData from "sap/ui/core/LocaleData";
|
4
|
+
|
5
|
+
export {};
|
6
|
+
|
7
|
+
declare global {
|
8
|
+
|
9
|
+
module "*./sap/ui/core/LocaleData.js" {
|
10
|
+
const content: LocaleData;
|
11
|
+
export default content;
|
12
|
+
}
|
13
|
+
|
14
|
+
|
15
|
+
// sap/ui/core/format/DateFormat.js
|
16
|
+
// type DateFormatOptions = {
|
17
|
+
// calendarType: string
|
18
|
+
// strictParsing?: boolean,
|
19
|
+
// pattern?: string,
|
20
|
+
// style?: string,
|
21
|
+
// }
|
22
|
+
|
23
|
+
// type DateFormatInstance = {
|
24
|
+
// format: (JSDate: Date, bUTC: boolean) => string,
|
25
|
+
// parse: (value: string, bUTC: boolean, bStrict: boolean) => Date | Array<Date | null | undefined> | null,
|
26
|
+
// }
|
27
|
+
|
28
|
+
// type DateFormat = {
|
29
|
+
// getDateInstance: (options: DateFormatOptions) => DateFormatInstance,
|
30
|
+
// getInstance: (options: DateFormatOptions) => DateFormatInstance,
|
31
|
+
// }
|
32
|
+
|
33
|
+
// module "*./sap/ui/core/format/DateFormat.js" {
|
34
|
+
// const content: DateFormat;
|
35
|
+
// export default content;
|
36
|
+
// }
|
37
|
+
|
38
|
+
// sap/ui/core/LocaleData.js
|
39
|
+
// type Locale = {
|
40
|
+
// getScript: () => string,
|
41
|
+
// getRegion: () => string,
|
42
|
+
// getLanguage: () => string,
|
43
|
+
// }
|
44
|
+
|
45
|
+
// type LocaleDataInstance = {
|
46
|
+
// getPreferredCalendarType: () => string,
|
47
|
+
// }
|
48
|
+
|
49
|
+
// type LocaleData = {
|
50
|
+
// getInstance: (locale: Locale) => LocaleDataInstance,
|
51
|
+
// }
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
// sap/ui/core/date/UniversalDate.js
|
56
|
+
// type UniversalDate = {
|
57
|
+
// new (time: number): UniversalDate;
|
58
|
+
// oDate: Date,
|
59
|
+
// sCalendarType: string,
|
60
|
+
// getInstance: (oDate: Date, sCalendarType: string) => UniversalDate,
|
61
|
+
// getDate: () => number,
|
62
|
+
// getMonth: () => number,
|
63
|
+
// getFullYear: () => number,
|
64
|
+
// getYear: () => number,
|
65
|
+
// getDay: () => number,
|
66
|
+
// getHours: () => number,
|
67
|
+
// getMinutes: () => number,
|
68
|
+
// getSeconds: () => number,
|
69
|
+
// getMilliseconds: () => number,
|
70
|
+
// getUTCDate: () => number,
|
71
|
+
// getUTCMonth: () => number,
|
72
|
+
// getUTCFullYear: () => number,
|
73
|
+
// getUTCDay: () => number,
|
74
|
+
// getUTCHours: () => number,
|
75
|
+
// getUTCMinutes: () => number,
|
76
|
+
// getUTCSeconds: () => number,
|
77
|
+
// getUTCMilliseconds: () => number,
|
78
|
+
// getTime: () => number,
|
79
|
+
// valueOf: () => number,
|
80
|
+
// getTimezoneOffset: () => number,
|
81
|
+
// toString: () => string,
|
82
|
+
// toDateString: () => string,
|
83
|
+
// setDate: (dayValue: number) => number,
|
84
|
+
// setFullYear: (yearValue: number, monthValue?: number, dateValue?: number) => number,
|
85
|
+
// setYear: (yearValue: number) => number,
|
86
|
+
// setMonth: (monthValue: number, dayValue?: number) => number,
|
87
|
+
// setHours: (hoursValue: number, minutesValue?: number, secondsValue?: number, msValue?: number) => number,
|
88
|
+
// setMinutes: (minutesValue: number, secondsValue?: number, msValue?: number) => number,
|
89
|
+
// setSeconds: (secondsValue: number, msValue?: number) => number,
|
90
|
+
// setMilliseconds: (setMilliseconds: number) => number,
|
91
|
+
// setUTCDate:(dayValue: number) => number,
|
92
|
+
// setUTCFullYear: (yearValue: number, monthValue?: number, dayValue?: number) => number,
|
93
|
+
// setUTCMonth: (monthValue: number, dayValue?: number) => number,
|
94
|
+
// setUTCHours: (hoursValue: number, minutesValue?: number, secondsValue?: number, msValue?: number) => number,
|
95
|
+
// setUTCMinutes: (minutesValue: number, secondsValue?: number, msValue?: number) => number,
|
96
|
+
// setUTCSeconds: (secondsValue: number, msValue?: number) => number,
|
97
|
+
// setUTCMilliseconds: (setMilliseconds: number) => number,
|
98
|
+
// }
|
99
|
+
|
100
|
+
// module "*../sap/ui/core/date/UniversalDate.js" {
|
101
|
+
// const content: UniversalDate;
|
102
|
+
// export default content;
|
103
|
+
// }
|
104
|
+
|
105
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ui5/webcomponents-localization",
|
3
|
-
"version": "1.7.
|
3
|
+
"version": "1.7.4",
|
4
4
|
"description": "Localization for UI5 Web Components",
|
5
5
|
"author": "SAP SE (https://www.sap.com)",
|
6
6
|
"license": "Apache-2.0",
|
@@ -28,12 +28,12 @@
|
|
28
28
|
},
|
29
29
|
"devDependencies": {
|
30
30
|
"@openui5/sap.ui.core": "1.101.0",
|
31
|
-
"@ui5/webcomponents-tools": "1.7.
|
31
|
+
"@ui5/webcomponents-tools": "1.7.4",
|
32
32
|
"chromedriver": "104.0.0",
|
33
33
|
"mkdirp": "^1.0.4",
|
34
34
|
"resolve": "^1.20.0"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
|
-
"@ui5/webcomponents-base": "1.7.
|
37
|
+
"@ui5/webcomponents-base": "1.7.4"
|
38
38
|
}
|
39
39
|
}
|
@@ -0,0 +1,231 @@
|
|
1
|
+
import UniversalDate from "../sap/ui/core/date/UniversalDate.js";
|
2
|
+
|
3
|
+
class CalendarDate {
|
4
|
+
_oUDate!: Date | UniversalDate;
|
5
|
+
|
6
|
+
constructor(year?: number, month?: number, date?: number, calendarType?: string) { // eslint-disable-line
|
7
|
+
let aArgs = arguments, // eslint-disable-line
|
8
|
+
oJSDate: Date,
|
9
|
+
oNow: Date,
|
10
|
+
sCalendarType!: string;
|
11
|
+
|
12
|
+
switch (aArgs.length) {
|
13
|
+
case 0: // defaults to the current date
|
14
|
+
oNow = new Date();
|
15
|
+
return (this.constructor(oNow.getFullYear(), oNow.getMonth(), oNow.getDate()) as CalendarDate);
|
16
|
+
|
17
|
+
case 1: // CalendarDate
|
18
|
+
case 2: // CalendarDate, sCalendarType
|
19
|
+
if (!(aArgs[0] instanceof CalendarDate)) {
|
20
|
+
throw new Error("Invalid arguments: the first argument must be of type sap.ui.unified.calendar.CalendarDate.");
|
21
|
+
}
|
22
|
+
sCalendarType = aArgs[1] ? aArgs[1] : (aArgs[0]._oUDate as UniversalDate).sCalendarType;
|
23
|
+
// Use source.valueOf() (returns the same point of time regardless calendar type) instead of
|
24
|
+
// source's getters to avoid non-gregorian Year, Month and Date may be used to construct a Gregorian date
|
25
|
+
oJSDate = new Date(aArgs[0].valueOf());
|
26
|
+
|
27
|
+
// Make this date really local. Now getters are safe.
|
28
|
+
oJSDate.setFullYear(oJSDate.getUTCFullYear(), oJSDate.getUTCMonth(), oJSDate.getUTCDate());
|
29
|
+
oJSDate.setHours(oJSDate.getUTCHours(), oJSDate.getUTCMinutes(), oJSDate.getUTCSeconds(), oJSDate.getUTCMilliseconds());
|
30
|
+
|
31
|
+
this._oUDate = createUniversalUTCDate(oJSDate, sCalendarType);
|
32
|
+
break;
|
33
|
+
|
34
|
+
case 3: // year, month, date
|
35
|
+
case 4: // year, month, date, sCalendarType
|
36
|
+
checkNumericLike(aArgs[0] as number, `Invalid year: ${aArgs[0] as number}`);
|
37
|
+
checkNumericLike(aArgs[1]as number, `Invalid month: ${aArgs[1] as number}`);
|
38
|
+
checkNumericLike(aArgs[2]as number, `Invalid date: ${aArgs[2] as number}`);
|
39
|
+
|
40
|
+
oJSDate = new Date(0, 0, 1);
|
41
|
+
oJSDate.setFullYear(aArgs[0] as number, aArgs[1] as number, aArgs[2] as number); // 2 digits year is not supported. If so, it is considered as full year as well.
|
42
|
+
|
43
|
+
if (aArgs[3]) {
|
44
|
+
sCalendarType = aArgs[3];
|
45
|
+
}
|
46
|
+
this._oUDate = createUniversalUTCDate(oJSDate, sCalendarType);
|
47
|
+
break;
|
48
|
+
|
49
|
+
default:
|
50
|
+
throw new Error(`${"Invalid arguments. Accepted arguments are: 1) oCalendarDate, (optional)calendarType"
|
51
|
+
+ "or 2) year, month, date, (optional) calendarType"}${aArgs as unknown as string}`);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
getYear() {
|
56
|
+
return this._oUDate.getUTCFullYear();
|
57
|
+
}
|
58
|
+
|
59
|
+
setYear(year: number) {
|
60
|
+
checkNumericLike(year, `Invalid year: ${year}`);
|
61
|
+
this._oUDate.setUTCFullYear(year);
|
62
|
+
return this;
|
63
|
+
}
|
64
|
+
|
65
|
+
getMonth() {
|
66
|
+
return this._oUDate.getUTCMonth();
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Sets the given month as ordinal month of the year.
|
71
|
+
* @param {int} month An integer between 0 and 11, representing the months January through December( or their
|
72
|
+
* equivalent month names for the given calendar).
|
73
|
+
* If the specified value is is outside of the expected range, this method attempts to update the date information
|
74
|
+
* accordingly. For example, if 12 is given as a month, the year will be incremented by 1, and 1 will be used for month.
|
75
|
+
* @param {int} [date] An integer between 1 and 31, representing the day of the month, but other values are allowed.
|
76
|
+
* 0 will result in the previous month's last day.
|
77
|
+
* -1 will result in the day before the previous month's last day.
|
78
|
+
* 32 will result in:
|
79
|
+
* - first day of the next month if the current month has 31 days.
|
80
|
+
* - second day of the next month if the current month has 30 days.
|
81
|
+
* Other value will result in adding or subtracting days according to the given value.
|
82
|
+
* @returns {sap.ui.unified.calendar.CalendarDate} <code>this</code> for method chaining.
|
83
|
+
*/
|
84
|
+
setMonth(month: number, date: number) {
|
85
|
+
checkNumericLike(month, `Invalid month: ${month}`);
|
86
|
+
if (date || date === 0) {
|
87
|
+
checkNumericLike(date, `Invalid date: ${date}`);
|
88
|
+
this._oUDate.setUTCMonth(month, date);
|
89
|
+
} else {
|
90
|
+
this._oUDate.setUTCMonth(month);
|
91
|
+
}
|
92
|
+
return this;
|
93
|
+
}
|
94
|
+
|
95
|
+
getDate() {
|
96
|
+
return this._oUDate.getUTCDate();
|
97
|
+
}
|
98
|
+
|
99
|
+
setDate(date: number) {
|
100
|
+
checkNumericLike(date, `Invalid date: ${date}`);
|
101
|
+
this._oUDate.setUTCDate(date);
|
102
|
+
return this;
|
103
|
+
}
|
104
|
+
|
105
|
+
getDay() {
|
106
|
+
return this._oUDate.getUTCDay();
|
107
|
+
}
|
108
|
+
|
109
|
+
getCalendarType() {
|
110
|
+
return (this._oUDate as UniversalDate).sCalendarType;
|
111
|
+
}
|
112
|
+
|
113
|
+
isBefore(oCalendarDate: CalendarDate) {
|
114
|
+
checkCalendarDate(oCalendarDate);
|
115
|
+
return this.valueOf() < oCalendarDate.valueOf();
|
116
|
+
}
|
117
|
+
|
118
|
+
isAfter(oCalendarDate: CalendarDate) {
|
119
|
+
checkCalendarDate(oCalendarDate);
|
120
|
+
return this.valueOf() > oCalendarDate.valueOf();
|
121
|
+
}
|
122
|
+
|
123
|
+
isSameOrBefore(oCalendarDate: CalendarDate) {
|
124
|
+
checkCalendarDate(oCalendarDate);
|
125
|
+
return this.valueOf() <= oCalendarDate.valueOf();
|
126
|
+
}
|
127
|
+
|
128
|
+
isSameOrAfter(oCalendarDate: CalendarDate) {
|
129
|
+
checkCalendarDate(oCalendarDate);
|
130
|
+
return this.valueOf() >= oCalendarDate.valueOf();
|
131
|
+
}
|
132
|
+
|
133
|
+
isSame(oCalendarDate: CalendarDate) {
|
134
|
+
checkCalendarDate(oCalendarDate);
|
135
|
+
return this.valueOf() === oCalendarDate.valueOf();
|
136
|
+
}
|
137
|
+
|
138
|
+
toLocalJSDate() {
|
139
|
+
// Use this._oUDate.getTime()(returns the same point of time regardless calendar type) instead of
|
140
|
+
// this._oUDate's getters to avoid non-gregorian Year, Month and Date to be used to construct a Gregorian date
|
141
|
+
const oLocalDate = new Date(this._oUDate.getTime());
|
142
|
+
|
143
|
+
// Make this date really local. Now getters are safe.
|
144
|
+
oLocalDate.setFullYear(oLocalDate.getUTCFullYear(), oLocalDate.getUTCMonth(), oLocalDate.getUTCDate());
|
145
|
+
oLocalDate.setHours(0, 0, 0, 0);
|
146
|
+
|
147
|
+
return oLocalDate;
|
148
|
+
}
|
149
|
+
|
150
|
+
toUTCJSDate() {
|
151
|
+
// Use this._oUDate.getTime()(returns the same point of time regardless calendar type) instead of
|
152
|
+
// this._oUDate's getters to avoid non-gregorian Year, Month and Date to be used to construct a Gregorian date
|
153
|
+
const oUTCDate = new Date(this._oUDate.getTime());
|
154
|
+
oUTCDate.setUTCHours(0, 0, 0, 0);
|
155
|
+
|
156
|
+
return oUTCDate;
|
157
|
+
}
|
158
|
+
|
159
|
+
toString() {
|
160
|
+
return `${(this._oUDate as UniversalDate).sCalendarType}: ${this.getYear()}/${this.getMonth() + 1}/${this.getDate()}`;
|
161
|
+
}
|
162
|
+
|
163
|
+
valueOf() {
|
164
|
+
return this._oUDate.getTime();
|
165
|
+
}
|
166
|
+
|
167
|
+
static fromLocalJSDate(oJSDate: Date, sCalendarType: string) {
|
168
|
+
// Cross frame check for a date should be performed here otherwise setDateValue would fail in OPA tests
|
169
|
+
// because Date object in the test is different than the Date object in the application (due to the iframe).
|
170
|
+
// We can use jQuery.type or this method:
|
171
|
+
function isValidDate(date: Date) {
|
172
|
+
return date && Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date as unknown as number); // eslint-disable-line
|
173
|
+
}
|
174
|
+
if (!isValidDate(oJSDate)) {
|
175
|
+
throw new Error(`Date parameter must be a JavaScript Date object: [${oJSDate as unknown as string}].`);
|
176
|
+
}
|
177
|
+
return new CalendarDate(oJSDate.getFullYear(), oJSDate.getMonth(), oJSDate.getDate(), sCalendarType);
|
178
|
+
}
|
179
|
+
|
180
|
+
static fromTimestamp(iTimestamp: number, sCalendarType: string) {
|
181
|
+
const oCalDate = new CalendarDate(0, 0, 1);
|
182
|
+
let oUDate;
|
183
|
+
try {
|
184
|
+
oUDate = UniversalDate.getInstance(new Date(iTimestamp), sCalendarType);
|
185
|
+
} catch (e) {
|
186
|
+
oUDate = new Date(NaN); // UniversalDate.getInstance may now throw an Exception - keep the old behavior
|
187
|
+
}
|
188
|
+
oCalDate._oUDate = oUDate;
|
189
|
+
return oCalDate;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
function createUniversalUTCDate(oDate: Date, sCalendarType: string) {
|
194
|
+
if (sCalendarType) {
|
195
|
+
return UniversalDate.getInstance(createUTCDate(oDate), sCalendarType);
|
196
|
+
}
|
197
|
+
return new UniversalDate(createUTCDate(oDate).getTime());
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Creates a JavaScript UTC Date corresponding to the given JavaScript Date.
|
202
|
+
* @param {Date} oDate JavaScript date object. Time related information is cut.
|
203
|
+
* @returns {Date} JavaScript date created from the date object, but this time considered as UTC date information.
|
204
|
+
*/
|
205
|
+
function createUTCDate(oDate: Date) {
|
206
|
+
const oUTCDate = new Date(Date.UTC(0, 0, 1));
|
207
|
+
|
208
|
+
oUTCDate.setUTCFullYear(oDate.getFullYear(), oDate.getMonth(), oDate.getDate());
|
209
|
+
|
210
|
+
return oUTCDate;
|
211
|
+
}
|
212
|
+
|
213
|
+
function checkCalendarDate(oCalendarDate: CalendarDate) {
|
214
|
+
if (!(oCalendarDate instanceof CalendarDate)) {
|
215
|
+
throw new Error(`Invalid calendar date: [${oCalendarDate as unknown as string}]. Expected: sap.ui.unified.calendar.CalendarDate`);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
/**
|
220
|
+
* Verifies the given value is numeric like, i.e. 3, "3" and throws an error if it is not.
|
221
|
+
* @param {any} value The value of any type to check. If null or undefined, this method throws an error.
|
222
|
+
* @param {string} message The message to be used if an error is to be thrown
|
223
|
+
* @throws will throw an error if the value is null or undefined or is not like a number
|
224
|
+
*/
|
225
|
+
function checkNumericLike(value: any, message: string) {
|
226
|
+
if (value === undefined || value === Infinity || isNaN(value)) { // eslint-disable-line
|
227
|
+
throw message;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
export default CalendarDate;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import LocaleData from "./LocaleData.js";
|
2
|
+
import Locale from "sap/ui/core/Locale";
|
3
|
+
|
4
|
+
const cache = new Map<Locale, LocaleData>();
|
5
|
+
|
6
|
+
const getCachedLocaleDataInstance = (locale: Locale) => {
|
7
|
+
if (!cache.has(locale)) {
|
8
|
+
cache.set(locale, LocaleData.getInstance(locale));
|
9
|
+
}
|
10
|
+
|
11
|
+
return cache.get(locale)!;
|
12
|
+
};
|
13
|
+
|
14
|
+
export default getCachedLocaleDataInstance;
|