Debian Codegraph @

js/src/builtin/intl/RelativeTimeFormat.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /**
6 * RelativeTimeFormat internal properties.
7 *
8 * Spec: ECMAScript 402 API, RelativeTimeFormat, 1.3.3.
9 */
10 var relativeTimeFormatInternalProperties = {
11 localeData: relativeTimeFormatLocaleData,
12 relevantExtensionKeys: ["nu"],
13 };
14
15 function relativeTimeFormatLocaleData() {
16 return {
17 nu: getNumberingSystems,
18 default: {
19 nu: intl_numberingSystem,
20 },
21 };
22 }
23
24 /**
25 * Compute an internal properties object from |lazyRelativeTimeFormatData|.
26 */
27 function resolveRelativeTimeFormatInternals(lazyRelativeTimeFormatData) {
28 assert(IsObject(lazyRelativeTimeFormatData), "lazy data not an object?");
29
30 var internalProps = std_Object_create(null);
31
32 var RelativeTimeFormat = relativeTimeFormatInternalProperties;
33
34 // Steps 10-11.
35 var r = ResolveLocale(
36 "RelativeTimeFormat",
37 lazyRelativeTimeFormatData.requestedLocales,
38 lazyRelativeTimeFormatData.opt,
39 RelativeTimeFormat.relevantExtensionKeys,
40 RelativeTimeFormat.localeData
41 );
42
43 // Steps 12-13.
44 internalProps.locale = r.locale;
45
46 // Step 14.
47 internalProps.numberingSystem = r.nu;
48
49 // Step 15 (Not relevant in our implementation).
50
51 // Step 17.
52 internalProps.style = lazyRelativeTimeFormatData.style;
53
54 // Step 19.
55 internalProps.numeric = lazyRelativeTimeFormatData.numeric;
56
57 // Steps 20-24 (Not relevant in our implementation).
58
59 return internalProps;
60 }
61
62 /**
63 * Returns an object containing the RelativeTimeFormat internal properties of |obj|.
64 */
65 function getRelativeTimeFormatInternals(obj) {
66 assert(
67 IsObject(obj),
68 "getRelativeTimeFormatInternals called with non-object"
69 );
70 assert(
71 intl_GuardToRelativeTimeFormat(obj) !== null,
72 "getRelativeTimeFormatInternals called with non-RelativeTimeFormat"
73 );
74
75 var internals = getIntlObjectInternals(obj);
76 assert(
77 internals.type === "RelativeTimeFormat",
78 "bad type escaped getIntlObjectInternals"
79 );
80
81 var internalProps = maybeInternalProperties(internals);
82 if (internalProps) {
83 return internalProps;
84 }
85
86 internalProps = resolveRelativeTimeFormatInternals(internals.lazyData);
87 setInternalProperties(internals, internalProps);
88 return internalProps;
89 }
90
91 /**
92 * Initializes an object as a RelativeTimeFormat.
93 *
94 * This method is complicated a moderate bit by its implementing initialization
95 * as a *lazy* concept. Everything that must happen now, does -- but we defer
96 * all the work we can until the object is actually used as a RelativeTimeFormat.
97 * This later work occurs in |resolveRelativeTimeFormatInternals|; steps not noted
98 * here occur there.
99 *
100 * Spec: ECMAScript 402 API, RelativeTimeFormat, 1.1.1.
101 */
102 function InitializeRelativeTimeFormat(relativeTimeFormat, locales, options) {
103 assert(
104 IsObject(relativeTimeFormat),
105 "InitializeRelativeimeFormat called with non-object"
106 );
107 assert(
108 intl_GuardToRelativeTimeFormat(relativeTimeFormat) !== null,
109 "InitializeRelativeTimeFormat called with non-RelativeTimeFormat"
110 );
111
112 // Lazy RelativeTimeFormat data has the following structure:
113 //
114 // {
115 // requestedLocales: List of locales,
116 // style: "long" / "short" / "narrow",
117 // numeric: "always" / "auto",
118 //
119 // opt: // opt object computed in InitializeRelativeTimeFormat
120 // {
121 // localeMatcher: "lookup" / "best fit",
122 // }
123 // }
124 //
125 // Note that lazy data is only installed as a final step of initialization,
126 // so every RelativeTimeFormat lazy data object has *all* these properties, never a
127 // subset of them.
128 var lazyRelativeTimeFormatData = std_Object_create(null);
129
130 // Step 1.
131 var requestedLocales = CanonicalizeLocaleList(locales);
132 lazyRelativeTimeFormatData.requestedLocales = requestedLocales;
133
134 // Steps 2-3.
135 if (options === undefined) {
136 options = std_Object_create(null);
137 } else {
138 options = ToObject(options);
139 }
140
141 // Step 4.
142 var opt = new_Record();
143
144 // Steps 5-6.
145 var matcher = GetOption(
146 options,
147 "localeMatcher",
148 "string",
149 ["lookup", "best fit"],
150 "best fit"
151 );
152 opt.localeMatcher = matcher;
153
154 // Steps 7-9.
155 var numberingSystem = GetOption(
156 options,
157 "numberingSystem",
158 "string",
159 undefined,
160 undefined
161 );
162 if (numberingSystem !== undefined) {
163 numberingSystem = intl_ValidateAndCanonicalizeUnicodeExtensionType(
164 numberingSystem,
165 "numberingSystem",
166 "nu"
167 );
168 }
169 opt.nu = numberingSystem;
170
171 lazyRelativeTimeFormatData.opt = opt;
172
173 // Steps 16-17.
174 var style = GetOption(
175 options,
176 "style",
177 "string",
178 ["long", "short", "narrow"],
179 "long"
180 );
181 lazyRelativeTimeFormatData.style = style;
182
183 // Steps 18-19.
184 var numeric = GetOption(
185 options,
186 "numeric",
187 "string",
188 ["always", "auto"],
189 "always"
190 );
191 lazyRelativeTimeFormatData.numeric = numeric;
192
193 initializeIntlObject(
194 relativeTimeFormat,
195 "RelativeTimeFormat",
196 lazyRelativeTimeFormatData
197 );
198 }
199
200 /**
201 * Returns the subset of the given locale list for which this locale list has a
202 * matching (possibly fallback) locale. Locales appear in the same order in the
203 * returned list as in the input list.
204 *
205 * Spec: ECMAScript 402 API, RelativeTimeFormat, 1.3.2.
206 */
207 function Intl_RelativeTimeFormat_supportedLocalesOf(locales /*, options*/) {
208 var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
209
210 // Step 1.
211 var availableLocales = "RelativeTimeFormat";
212
213 // Step 2.
214 var requestedLocales = CanonicalizeLocaleList(locales);
215
216 // Step 3.
217 return SupportedLocales(availableLocales, requestedLocales, options);
218 }
219
220 /**
221 * Returns a String value representing the written form of a relative date
222 * formatted according to the effective locale and the formatting options
223 * of this RelativeTimeFormat object.
224 *
225 * Spec: ECMAScript 402 API, RelativeTImeFormat, 1.4.3.
226 */
227 function Intl_RelativeTimeFormat_format(value, unit) {
228 // Step 1.
229 var relativeTimeFormat = this;
230
231 // Step 2.
232 if (
233 !IsObject(relativeTimeFormat) ||
234 (relativeTimeFormat = intl_GuardToRelativeTimeFormat(
235 relativeTimeFormat
236 )) === null
237 ) {
238 return callFunction(
239 intl_CallRelativeTimeFormatMethodIfWrapped,
240 this,
241 value,
242 unit,
243 "Intl_RelativeTimeFormat_format"
244 );
245 }
246
247 // Step 3.
248 var t = ToNumber(value);
249
250 // Step 4.
251 var u = ToString(unit);
252
253 // Step 5.
254 return intl_FormatRelativeTime(relativeTimeFormat, t, u, false);
255 }
256
257 /**
258 * Returns an Array composed of the components of a relative date formatted
259 * according to the effective locale and the formatting options of this
260 * RelativeTimeFormat object.
261 *
262 * Spec: ECMAScript 402 API, RelativeTImeFormat, 1.4.4.
263 */
264 function Intl_RelativeTimeFormat_formatToParts(value, unit) {
265 // Step 1.
266 var relativeTimeFormat = this;
267
268 // Step 2.
269 if (
270 !IsObject(relativeTimeFormat) ||
271 (relativeTimeFormat = intl_GuardToRelativeTimeFormat(
272 relativeTimeFormat
273 )) === null
274 ) {
275 return callFunction(
276 intl_CallRelativeTimeFormatMethodIfWrapped,
277 this,
278 value,
279 unit,
280 "Intl_RelativeTimeFormat_formatToParts"
281 );
282 }
283
284 // Step 3.
285 var t = ToNumber(value);
286
287 // Step 4.
288 var u = ToString(unit);
289
290 // Step 5.
291 return intl_FormatRelativeTime(relativeTimeFormat, t, u, true);
292 }
293
294 /**
295 * Returns the resolved options for a RelativeTimeFormat object.
296 *
297 * Spec: ECMAScript 402 API, RelativeTimeFormat, 1.4.5.
298 */
299 function Intl_RelativeTimeFormat_resolvedOptions() {
300 // Step 1.
301 var relativeTimeFormat = this;
302
303 // Steps 2-3.
304 if (
305 !IsObject(relativeTimeFormat) ||
306 (relativeTimeFormat = intl_GuardToRelativeTimeFormat(
307 relativeTimeFormat
308 )) === null
309 ) {
310 return callFunction(
311 intl_CallRelativeTimeFormatMethodIfWrapped,
312 this,
313 "Intl_RelativeTimeFormat_resolvedOptions"
314 );
315 }
316
317 var internals = getRelativeTimeFormatInternals(relativeTimeFormat);
318
319 // Steps 4-5.
320 var result = {
321 locale: internals.locale,
322 style: internals.style,
323 numeric: internals.numeric,
324 numberingSystem: internals.numberingSystem,
325 };
326
327 // Step 6.
328 return result;
329 }