Bug 1436841 - Update Intl.RelativeTimeFormat option type: numeric/text to numeric always/auto. r?anba draft
authorZibi Braniecki <zbraniecki@mozilla.com>
Thu, 08 Feb 2018 13:10:01 -0800
changeset 752780 6058a94ec0967f6aca02a19097b9765e47c8cead
parent 752350 8cc2427a322caa1e2c09ca3957335f88e573dc7a
push id98379
push userbmo:gandalf@aviary.pl
push dateThu, 08 Feb 2018 22:46:13 +0000
reviewersanba
bugs1436841
milestone60.0a1
Bug 1436841 - Update Intl.RelativeTimeFormat option type: numeric/text to numeric always/auto. r?anba MozReview-Commit-ID: AppMXKfQpG2
js/src/builtin/intl/RelativeTimeFormat.cpp
js/src/builtin/intl/RelativeTimeFormat.h
js/src/builtin/intl/RelativeTimeFormat.js
js/src/tests/non262/Intl/RelativeTimeFormat/format.js
js/src/tests/non262/Intl/RelativeTimeFormat/relativetimeformat.js
--- a/js/src/builtin/intl/RelativeTimeFormat.cpp
+++ b/js/src/builtin/intl/RelativeTimeFormat.cpp
@@ -253,27 +253,27 @@ NewURelativeDateTimeFormatter(JSContext*
                          UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status);
     if (U_FAILURE(status)) {
         intl::ReportInternalError(cx);
         return nullptr;
     }
     return rtf;
 }
 
-enum class RelativeTimeType
+enum class RelativeTimeNumeric
 {
     /**
      * Only strings with numeric components like `1 day ago`.
      */
-    Numeric,
+    Always,
     /**
      * Natural-language strings like `yesterday` when possible,
      * otherwise strings with numeric components as in `7 months ago`.
      */
-    Text,
+    Auto,
 };
 
 bool
 js::intl_FormatRelativeTime(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
     MOZ_ASSERT(args.length() == 4);
 
@@ -319,35 +319,35 @@ js::intl_FormatRelativeTime(JSContext* c
         } else if (StringEqualsAscii(unit, "quarter")) {
             relDateTimeUnit = UDAT_REL_UNIT_QUARTER;
         } else {
             MOZ_ASSERT(StringEqualsAscii(unit, "year"));
             relDateTimeUnit = UDAT_REL_UNIT_YEAR;
         }
     }
 
-    RelativeTimeType relDateTimeType;
+    RelativeTimeNumeric relDateTimeNumeric;
     {
-        JSLinearString* type = args[3].toString()->ensureLinear(cx);
-        if (!type)
+        JSLinearString* numeric = args[3].toString()->ensureLinear(cx);
+        if (!numeric)
             return false;
 
-        if (StringEqualsAscii(type, "text")) {
-            relDateTimeType = RelativeTimeType::Text;
+        if (StringEqualsAscii(numeric, "auto")) {
+            relDateTimeNumeric = RelativeTimeNumeric::Auto;
         } else {
-            MOZ_ASSERT(StringEqualsAscii(type, "numeric"));
-            relDateTimeType = RelativeTimeType::Numeric;
+            MOZ_ASSERT(StringEqualsAscii(numeric, "always"));
+            relDateTimeNumeric = RelativeTimeNumeric::Always;
         }
     }
 
     JSString* str =
-        CallICU(cx, [rtf, t, relDateTimeUnit, relDateTimeType](UChar* chars, int32_t size,
-                                                               UErrorCode* status)
+        CallICU(cx, [rtf, t, relDateTimeUnit, relDateTimeNumeric](UChar* chars, int32_t size,
+                                                                  UErrorCode* status)
         {
-            auto fmt = relDateTimeType == RelativeTimeType::Text
+            auto fmt = relDateTimeNumeric == RelativeTimeNumeric::Auto
                        ? ureldatefmt_format
                        : ureldatefmt_formatNumeric;
             return fmt(rtf, t, relDateTimeUnit, chars, size, status);
         });
     if (!str)
         return false;
 
     args.rval().setString(str);
--- a/js/src/builtin/intl/RelativeTimeFormat.h
+++ b/js/src/builtin/intl/RelativeTimeFormat.h
@@ -53,18 +53,18 @@ extern MOZ_MUST_USE bool
 intl_RelativeTimeFormat_availableLocales(JSContext* cx, unsigned argc, JS::Value* vp);
 
 /**
  * Returns a relative time as a string formatted according to the effective
  * locale and the formatting options of the given RelativeTimeFormat.
  *
  * t should be a number representing a number to be formatted.
  * unit should be "second", "minute", "hour", "day", "week", "month", "quarter", or "year".
- * type should be "text" or "numeric".
+ * numeric should be "always" or "auto".
  *
- * Usage: formatted = intl_FormatRelativeTime(relativeTimeFormat, t, unit, type)
+ * Usage: formatted = intl_FormatRelativeTime(relativeTimeFormat, t, unit, numeric)
  */
 extern MOZ_MUST_USE bool
 intl_FormatRelativeTime(JSContext* cx, unsigned argc, JS::Value* vp);
 
 } // namespace js
 
 #endif /* builtin_intl_RelativeTimeFormat_h */
--- a/js/src/builtin/intl/RelativeTimeFormat.js
+++ b/js/src/builtin/intl/RelativeTimeFormat.js
@@ -33,27 +33,31 @@ function relativeTimeFormatLocaleData() 
  */
 function resolveRelativeTimeFormatInternals(lazyRelativeTimeFormatData) {
     assert(IsObject(lazyRelativeTimeFormatData), "lazy data not an object?");
 
     var internalProps = std_Object_create(null);
 
     var RelativeTimeFormat = relativeTimeFormatInternalProperties;
 
-    // Step 16.
+    // Step 10.
     const r = ResolveLocale(callFunction(RelativeTimeFormat.availableLocales, RelativeTimeFormat),
                             lazyRelativeTimeFormatData.requestedLocales,
                             lazyRelativeTimeFormatData.opt,
                             RelativeTimeFormat.relevantExtensionKeys,
                             RelativeTimeFormat.localeData);
 
-    // Step 17.
+    // Step 11.
     internalProps.locale = r.locale;
+
+    // Step 14.
     internalProps.style = lazyRelativeTimeFormatData.style;
-    internalProps.type = lazyRelativeTimeFormatData.type;
+
+    // Step 16.
+    internalProps.numeric = lazyRelativeTimeFormatData.numeric;
 
     return internalProps;
 }
 
 /**
  * Returns an object containing the RelativeTimeFormat internal properties of |obj|,
  * or throws a TypeError if |obj| isn't RelativeTimeFormat-initialized.
  */
@@ -90,17 +94,17 @@ function InitializeRelativeTimeFormat(re
     assert(IsRelativeTimeFormat(relativeTimeFormat),
            "InitializeRelativeTimeFormat called with non-RelativeTimeFormat");
 
     // Lazy RelativeTimeFormat data has the following structure:
     //
     //   {
     //     requestedLocales: List of locales,
     //     style: "long" / "short" / "narrow",
-    //     type: "numeric" / "text",
+    //     numeric: "always" / "auto",
     //
     //     opt: // opt object computer in InitializeRelativeTimeFormat
     //       {
     //         localeMatcher: "lookup" / "best fit",
     //       }
     //   }
     //
     // Note that lazy data is only installed as a final step of initialization,
@@ -126,20 +130,19 @@ function InitializeRelativeTimeFormat(re
     opt.localeMatcher = matcher;
 
     lazyRelativeTimeFormatData.opt = opt;
 
     // Steps 13-14.
     const style = GetOption(options, "style", "string", ["long", "short", "narrow"], "long");
     lazyRelativeTimeFormatData.style = style;
 
-    // This option is in the process of being added to the spec.
-    // See: https://github.com/tc39/proposal-intl-relative-time/issues/9
-    const type = GetOption(options, "type", "string", ["numeric", "text"], "numeric");
-    lazyRelativeTimeFormatData.type = type;
+    // Steps 15-16.
+    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.
@@ -193,17 +196,17 @@ function Intl_RelativeTimeFormat_format(
       case "quarter":
       case "year":
         break;
       default:
         ThrowRangeError(JSMSG_INVALID_OPTION_VALUE, "unit", u);
     }
 
     // Step 5.
-    return intl_FormatRelativeTime(relativeTimeFormat, t, u, internals.type);
+    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() {
@@ -213,13 +216,13 @@ function Intl_RelativeTimeFormat_resolve
                        "RelativeTimeFormat");
     }
 
     var internals = getRelativeTimeFormatInternals(this, "resolvedOptions");
 
     var result = {
         locale: internals.locale,
         style: internals.style,
-        type: internals.type,
+        numeric: internals.numeric,
     };
 
     return result;
 }
--- a/js/src/tests/non262/Intl/RelativeTimeFormat/format.js
+++ b/js/src/tests/non262/Intl/RelativeTimeFormat/format.js
@@ -46,17 +46,17 @@ addIntlExtras(Intl);
   assertEq(rtf.format(-0, "year"), "in 0 years");
   assertEq(rtf.format(-1, "year"), "1 year ago");
   assertEq(rtf.format(1, "year"), "in 1 year");
 }
 
 {
   // Text format
   rtf = new Intl.RelativeTimeFormat("en-US", {
-    type: "text"
+    numeric: "auto"
   });
   assertEq(rtf.format(0, "second"), "now");
   assertEq(rtf.format(-0, "second"), "now");
   assertEq(rtf.format(-1, "second"), "1 second ago");
   assertEq(rtf.format(1, "second"), "in 1 second");
 
   assertEq(rtf.format(0, "minute"), "in 0 minutes");
   assertEq(rtf.format(-0, "minute"), "in 0 minutes");
@@ -84,21 +84,21 @@ addIntlExtras(Intl);
   assertEq(rtf.format(1, "month"), "next month");
 
   assertEq(rtf.format(0, "year"), "this year");
   assertEq(rtf.format(-0, "year"), "this year");
   assertEq(rtf.format(-1, "year"), "last year");
   assertEq(rtf.format(1, "year"), "next year");
 }
 
-rtf = new Intl.RelativeTimeFormat("de", {type: "text"});
+rtf = new Intl.RelativeTimeFormat("de", {numeric: "auto"});
 assertEq(rtf.format(-1, "day"), "gestern");
 assertEq(rtf.format(1, "day"), "morgen");
 
-rtf = new Intl.RelativeTimeFormat("ar", {type: "text"});
+rtf = new Intl.RelativeTimeFormat("ar", {numeric: "auto"});
 assertEq(rtf.format(-1, "day"), "أمس");
 assertEq(rtf.format(1, "day"), "غدًا");
 
 
 rtf = new Intl.RelativeTimeFormat("en-US");
 assertEq(rtf.format(Infinity, "year"), "in ∞ years");
 assertEq(rtf.format(-Infinity, "year"), "∞ years ago");
 
--- a/js/src/tests/non262/Intl/RelativeTimeFormat/relativetimeformat.js
+++ b/js/src/tests/non262/Intl/RelativeTimeFormat/relativetimeformat.js
@@ -7,10 +7,11 @@
 
 var rtf;
 
 addIntlExtras(Intl);
 
 rtf = new Intl.RelativeTimeFormat("en-us");
 assertEq(rtf.resolvedOptions().locale, "en-US");
 assertEq(rtf.resolvedOptions().style, "long");
+assertEq(rtf.resolvedOptions().numeric, "always");
 
 reportCompare(0, 0, 'ok');