1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/********** Intl.RelativeTimeFormat **********/
/**
* RelativeTimeFormat internal properties.
*
* Spec: ECMAScript 402 API, RelativeTimeFormat, 1.3.3.
*/
var relativeTimeFormatInternalProperties = {
localeData: relativeTimeFormatLocaleData,
relevantExtensionKeys: [],
};
function relativeTimeFormatLocaleData() {
// RelativeTimeFormat doesn't support any extension keys.
return {};
}
/**
* Compute an internal properties object from |lazyRelativeTimeFormatData|.
*/
function resolveRelativeTimeFormatInternals(lazyRelativeTimeFormatData) {
assert(IsObject(lazyRelativeTimeFormatData), "lazy data not an object?");
var internalProps = std_Object_create(null);
var RelativeTimeFormat = relativeTimeFormatInternalProperties;
// Steps 7-8.
const r = ResolveLocale("RelativeTimeFormat",
lazyRelativeTimeFormatData.requestedLocales,
lazyRelativeTimeFormatData.opt,
RelativeTimeFormat.relevantExtensionKeys,
RelativeTimeFormat.localeData);
// Step 9-10.
internalProps.locale = r.locale;
// Step 11.
assert(r.locale === r.dataLocale,
"resolved locale matches the resolved data-locale when no extension-keys are present");
// Step 13.
internalProps.style = lazyRelativeTimeFormatData.style;
// Step 15.
internalProps.numeric = lazyRelativeTimeFormatData.numeric;
// Steps 16-20 (Not relevant in our implementation).
return internalProps;
}
/**
* Returns an object containing the RelativeTimeFormat internal properties of |obj|,
* or throws a TypeError if |obj| isn't RelativeTimeFormat-initialized.
*/
function getRelativeTimeFormatInternals(obj) {
assert(IsObject(obj), "getRelativeTimeFormatInternals called with non-object");
assert(IsRelativeTimeFormat(obj), "getRelativeTimeFormatInternals called with non-RelativeTimeFormat");
var internals = getIntlObjectInternals(obj);
assert(internals.type === "RelativeTimeFormat", "bad type escaped getIntlObjectInternals");
var internalProps = maybeInternalProperties(internals);
if (internalProps)
return internalProps;
internalProps = resolveRelativeTimeFormatInternals(internals.lazyData);
setInternalProperties(internals, internalProps);
return internalProps;
}
/**
* Initializes an object as a RelativeTimeFormat.
*
* This method is complicated a moderate bit by its implementing initialization
* as a *lazy* concept. Everything that must happen now, does -- but we defer
* all the work we can until the object is actually used as a RelativeTimeFormat.
* This later work occurs in |resolveRelativeTimeFormatInternals|; steps not noted
* here occur there.
*
* Spec: ECMAScript 402 API, RelativeTimeFormat, 1.1.1.
*/
function InitializeRelativeTimeFormat(relativeTimeFormat, locales, options) {
assert(IsObject(relativeTimeFormat), "InitializeRelativeTimeFormat called with non-object");
assert(IsRelativeTimeFormat(relativeTimeFormat), "InitializeRelativeTimeFormat called with non-RelativeTimeFormat");
// Lazy RelativeTimeFormat data has the following structure:
//
// {
// requestedLocales: List of locales,
// style: "long" / "short" / "narrow",
// numeric: "always" / "auto",
//
// opt: // opt object computed in InitializeRelativeTimeFormat
// {
// localeMatcher: "lookup" / "best fit",
// }
// }
//
// Note that lazy data is only installed as a final step of initialization,
// so every RelativeTimeFormat lazy data object has *all* these properties, never a
// subset of them.
const lazyRelativeTimeFormatData = std_Object_create(null);
// Step 1.
let requestedLocales = CanonicalizeLocaleList(locales);
lazyRelativeTimeFormatData.requestedLocales = requestedLocales;
// Steps 2-3.
if (options === undefined)
options = std_Object_create(null);
else
options = ToObject(options);
// Step 4.
let opt = new Record();
// Steps 5-6.
let matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
opt.localeMatcher = matcher;
lazyRelativeTimeFormatData.opt = opt;
// Steps 12-13.
const style = GetOption(options, "style", "string", ["long", "short", "narrow"], "long");
lazyRelativeTimeFormatData.style = style;
// Steps 14-15.
const numeric = GetOption(options, "numeric", "string", ["always", "auto"], "always");
lazyRelativeTimeFormatData.numeric = numeric;
initializeIntlObject(relativeTimeFormat, "RelativeTimeFormat", lazyRelativeTimeFormatData)
}
/**
* Returns the subset of the given locale list for which this locale list has a
* matching (possibly fallback) locale. Locales appear in the same order in the
* returned list as in the input list.
*
* Spec: ECMAScript 402 API, RelativeTimeFormat, 1.3.2.
*/
function Intl_RelativeTimeFormat_supportedLocalesOf(locales /*, options*/) {
var options = arguments.length > 1 ? arguments[1] : undefined;
// Step 1.
var availableLocales = "RelativeTimeFormat";
// Step 2.
let requestedLocales = CanonicalizeLocaleList(locales);
// Step 3.
return SupportedLocales(availableLocales, requestedLocales, options);
}
/**
* Returns a String value representing the written form of a relative date
* formatted according to the effective locale and the formatting options
* of this RelativeTimeFormat object.
*
* Spec: ECMAScript 402 API, RelativeTImeFormat, 1.4.3.
*/
function Intl_RelativeTimeFormat_format(value, unit) {
// Step 1.
let relativeTimeFormat = this;
// Step 2.
if (!IsObject(relativeTimeFormat) || !IsRelativeTimeFormat(relativeTimeFormat))
ThrowTypeError(JSMSG_INTL_OBJECT_NOT_INITED, "RelativeTimeFormat", "format", "RelativeTimeFormat");
// Ensure the RelativeTimeFormat internals are resolved.
let internals = getRelativeTimeFormatInternals(relativeTimeFormat);
// Step 3.
let t = ToNumber(value);
// Step 4.
let u = ToString(unit);
// PartitionRelativeTimePattern, step 4.
if (!Number_isFinite(t)) {
ThrowRangeError(JSMSG_DATE_NOT_FINITE, "RelativeTimeFormat", "format");
}
// PartitionRelativeTimePattern, step 5.
switch (u) {
case "second":
case "seconds":
case "minute":
case "minutes":
case "hour":
case "hours":
case "day":
case "days":
case "week":
case "weeks":
case "month":
case "months":
case "quarter":
case "quarters":
case "year":
case "years":
break;
default:
ThrowRangeError(JSMSG_INVALID_OPTION_VALUE, "unit", u);
}
// Step 5.
return intl_FormatRelativeTime(relativeTimeFormat, t, u, internals.numeric);
}
/**
* Returns the resolved options for a PluralRules object.
*
* Spec: ECMAScript 402 API, RelativeTimeFormat, 1.4.4.
*/
function Intl_RelativeTimeFormat_resolvedOptions() {
// Check "this RelativeTimeFormat object" per introduction of section 1.4.
if (!IsObject(this) || !IsRelativeTimeFormat(this)) {
ThrowTypeError(JSMSG_INTL_OBJECT_NOT_INITED, "RelativeTimeFormat", "resolvedOptions",
"RelativeTimeFormat");
}
var internals = getRelativeTimeFormatInternals(this);
// Steps 4-5.
var result = {
locale: internals.locale,
style: internals.style,
numeric: internals.numeric,
};
// Step 6.
return result;
}
|