Search in sources :

Example 1 with OptionsRecord

use of com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord in project es6draft by anba.

the class DateTimeFormatConstructor method InitializeDateTimeFormat.

/**
     * 12.1.1 InitializeDateTimeFormat (dateTimeFormat, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param dateTimeFormat
     *            the date format object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializeDateTimeFormat(ExecutionContext cx, DateTimeFormatObject dateTimeFormat, Object locales, Object opts) {
    /* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* step 4 */
    ScriptObject options = ToDateTimeOptions(cx, opts, "any", "date");
    /* step 6 */
    String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
    /* step 5, 7 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
    /* step 8 */
    DateTimeFormatLocaleData localeData = new DateTimeFormatLocaleData();
    /* step 9 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 10 */
    dateTimeFormat.setLocale(r.getLocale());
    /* step 11 */
    dateTimeFormat.setCalendar(r.getValue(ExtensionKey.ca));
    /* step 12 */
    dateTimeFormat.setNumberingSystem(r.getValue(ExtensionKey.nu));
    /* step 13 */
    String dataLocale = r.getDataLocale();
    /* step 14 */
    Object tz = Get(cx, options, "timeZone");
    /* steps 15-16 */
    String timeZone;
    if (!Type.isUndefined(tz)) {
        /* step 15.a */
        timeZone = ToFlatString(cx, tz);
        /* step 15.b */
        if (!IsValidTimeZoneName(timeZone)) {
            throw newRangeError(cx, Messages.Key.IntlInvalidOption, timeZone);
        }
        /* step 15.c */
        timeZone = CanonicalizeTimeZoneName(timeZone);
    } else {
        /* step 16.a */
        timeZone = DefaultTimeZone(cx.getRealm());
    }
    /* step 17 */
    dateTimeFormat.setTimeZone(timeZone);
    /* step 18 (moved) */
    /* step 19 */
    // FIXME: spec should propably define exact iteration order here
    String weekday = GetStringOption(cx, options, "weekday", set("narrow", "short", "long"), null);
    String era = GetStringOption(cx, options, "era", set("narrow", "short", "long"), null);
    String year = GetStringOption(cx, options, "year", set("2-digit", "numeric"), null);
    String month = GetStringOption(cx, options, "month", set("2-digit", "numeric", "narrow", "short", "long"), null);
    String day = GetStringOption(cx, options, "day", set("2-digit", "numeric"), null);
    String hour = GetStringOption(cx, options, "hour", set("2-digit", "numeric"), null);
    String minute = GetStringOption(cx, options, "minute", set("2-digit", "numeric"), null);
    String second = GetStringOption(cx, options, "second", set("2-digit", "numeric"), null);
    String timeZoneName = GetStringOption(cx, options, "timeZoneName", set("short", "long"), null);
    /* steps 20-21 (moved) */
    /* step 22 */
    String formatMatcher = GetStringOption(cx, options, "formatMatcher", set("basic", "best fit"), "best fit");
    /* steps 23-26 (moved) */
    /* step 27 */
    Boolean hour12 = GetBooleanOption(cx, options, "hour12", null);
    /* steps 18, 20-21, 23-26, 28-29 */
    FormatMatcherRecord formatRecord = new FormatMatcherRecord(weekday, era, year, month, day, hour, minute, second, timeZoneName, hour12);
    Lazy<String> pattern;
    if ("basic".equals(formatMatcher)) {
        pattern = new BasicFormatPattern(formatRecord, dataLocale);
    } else {
        pattern = new BestFitFormatPattern(formatRecord, dataLocale);
    }
    /* step 30 */
    dateTimeFormat.setPattern(pattern);
    /* step 31 */
    dateTimeFormat.setBoundFormat(null);
/* step 32 (FIXME: spec bug - unnecessary internal slot) */
/* step 33 (omitted) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 2 with OptionsRecord

use of com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord in project es6draft by anba.

the class NumberFormatConstructor method InitializeNumberFormat.

/**
     * 11.1.1 InitializeNumberFormat (numberFormat, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param numberFormat
     *            the number format object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializeNumberFormat(ExecutionContext cx, NumberFormatObject numberFormat, Object locales, Object opts) {
    /* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* steps 4-5 */
    ScriptObject options;
    if (Type.isUndefined(opts)) {
        options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
    } else {
        options = ToObject(cx, opts);
    }
    /* step 7 */
    String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
    /* step 6, 8 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
    /* step 9 */
    NumberFormatLocaleData localeData = new NumberFormatLocaleData();
    /* step 10 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 11 */
    numberFormat.setLocale(r.getLocale());
    /* step 12 */
    numberFormat.setNumberingSystem(r.getValue(ExtensionKey.nu));
    /* step 13 (not applicable) */
    /* step 14 */
    String s = GetStringOption(cx, options, "style", set("decimal", "percent", "currency"), "decimal");
    /* step 15 */
    numberFormat.setStyle(s);
    /* step 16 */
    String c = GetStringOption(cx, options, "currency", null, null);
    /* step 17 */
    if (c != null && !IsWellFormedCurrencyCode(c)) {
        throw newRangeError(cx, Messages.Key.IntlInvalidCurrency, c);
    }
    /* step 18 */
    if ("currency".equals(s) && c == null) {
        throw newTypeError(cx, Messages.Key.IntlInvalidCurrency, "null");
    }
    /* step 19 */
    int cDigits = -1;
    if ("currency".equals(s)) {
        c = ToUpperCase(c);
        numberFormat.setCurrency(c);
        cDigits = CurrencyDigits(c);
    }
    /* step 20 */
    String cd = GetStringOption(cx, options, "currencyDisplay", set("code", "symbol", "name"), "symbol");
    /* step 21 */
    if ("currency".equals(s)) {
        numberFormat.setCurrencyDisplay(cd);
    }
    /* step 22 */
    int mnid = GetNumberOption(cx, options, "minimumIntegerDigits", 1, 21, 1);
    /* step 23 */
    numberFormat.setMinimumIntegerDigits(mnid);
    /* step 24 */
    int mnfdDefault = "currency".equals(s) ? cDigits : 0;
    /* step 25 */
    int mnfd = GetNumberOption(cx, options, "minimumFractionDigits", 0, 20, mnfdDefault);
    /* step 26 */
    numberFormat.setMinimumFractionDigits(mnfd);
    /* step 27 */
    int mxfdDefault = "currency".equals(s) ? Math.max(mnfd, cDigits) : "percent".equals(s) ? Math.max(mnfd, 0) : Math.max(mnfd, 3);
    /* step 28 */
    int mxfd = GetNumberOption(cx, options, "maximumFractionDigits", mnfd, 20, mxfdDefault);
    /* step 29 */
    numberFormat.setMaximumFractionDigits(mxfd);
    /* step 30 */
    Object mnsd = Get(cx, options, "minimumSignificantDigits");
    /* step 31 */
    Object mxsd = Get(cx, options, "maximumSignificantDigits");
    /* step 32 */
    if (!Type.isUndefined(mnsd) || !Type.isUndefined(mxsd)) {
        int _mnsd = GetNumberOption(cx, options, "minimumSignificantDigits", 1, 21, 1);
        int _mxsd = GetNumberOption(cx, options, "maximumSignificantDigits", _mnsd, 21, 21);
        numberFormat.setMinimumSignificantDigits(_mnsd);
        numberFormat.setMaximumSignificantDigits(_mxsd);
    }
    /* step 33 */
    boolean g = GetBooleanOption(cx, options, "useGrouping", true);
    /* step 34 */
    numberFormat.setUseGrouping(g);
    /* steps 35-40 (not applicable) */
    /* step 41 */
    numberFormat.setBoundFormat(null);
/* step 42 (FIXME: spec bug - unnecessary internal slot) */
/* step 43 (omitted) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Example 3 with OptionsRecord

use of com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord in project es6draft by anba.

the class PluralRulesConstructor method InitializePluralRules.

/**
     * InitializePluralRules (pluralRules, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param pluralRules
     *            the pluralRules object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializePluralRules(ExecutionContext cx, PluralRulesObject pluralRules, Object locales, Object opts) {
    /* steps 1-2 (not applicable) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* steps 4-5 */
    ScriptObject options;
    if (Type.isUndefined(opts)) {
        options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
    } else {
        options = ToObject(cx, opts);
    }
    /* step 6 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName("lookup"));
    /* step 7 */
    String s = GetStringOption(cx, options, "type", set("cardinal", "ordinal"), "cardinal");
    /* step 8 */
    pluralRules.setType(s);
    /* step 9 */
    PluralRulesLocaleData localeData = new PluralRulesLocaleData();
    /* step 10 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 11 */
    pluralRules.setLocale(r.getLocale());
    /* steps 12-16 (not applicable) */
    /* step 17 */
    pluralRules.setBoundResolve(null);
/* step 18 (not applicable) */
/* step 19 (return) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale)

Example 4 with OptionsRecord

use of com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord in project es6draft by anba.

the class CollatorConstructor method InitializeCollator.

/**
     * 10.1.1 InitializeCollator (collator, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param collator
     *            the collator object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializeCollator(ExecutionContext cx, CollatorObject collator, Object locales, Object opts) {
    /* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
    /* step 3 */
    Set<String> requestedLocals = CanonicalizeLocaleList(cx, locales);
    /* steps 4-5 */
    ScriptObject options;
    if (Type.isUndefined(opts)) {
        options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
    } else {
        options = ToObject(cx, opts);
    }
    /* step 6 */
    String u = GetStringOption(cx, options, "usage", set("sort", "search"), "sort");
    /* step 7 */
    collator.setUsage(u);
    /* steps 8-9 */
    CollatorLocaleData localeData = new CollatorLocaleData(u);
    /* step 11 */
    String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
    /* steps 10, 12 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
    // FIXME: spec should propably define exact iteration order here
    /* step 13 (kn-numeric) */
    Boolean numeric = GetBooleanOption(cx, options, "numeric", null);
    if (numeric != null) {
        opt.set(ExtensionKey.kn, numeric.toString());
    }
    /* step 13 (kf-caseFirst) */
    String caseFirst = GetStringOption(cx, options, "caseFirst", set("upper", "lower", "false"), null);
    if (caseFirst != null) {
        opt.set(ExtensionKey.kf, caseFirst);
    }
    /* steps 14-15 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocals, opt, relevantExtensionKeys, localeData);
    /* step 16 */
    collator.setLocale(r.getLocale());
    /* steps 17-20 (co-collation) */
    String collation = r.getValue(ExtensionKey.co);
    collator.setCollation(collation != null ? collation : "default");
    /* steps 17-20 (kn-numeric) */
    collator.setNumeric("true".equals(r.getValue(ExtensionKey.kn)));
    /* steps 17-20 (kf-caseFirst) */
    collator.setCaseFirst(r.getValue(ExtensionKey.kf));
    /* step 21 */
    String s = GetStringOption(cx, options, "sensitivity", set("base", "accent", "case", "variant"), null);
    /* step 22 */
    if (s == null) {
        // The specification differentiates between "sort" and "search" usage, but effectively
        // you'll end up with "variant" in both cases, so take the short path here.
        s = "variant";
    }
    /* step 23 */
    collator.setSensitivity(s);
    /* step 24 */
    boolean ip = GetBooleanOption(cx, options, "ignorePunctuation", false);
    /* step 25 */
    collator.setIgnorePunctuation(ip);
    /* step 26 */
    collator.setBoundCompare(null);
/* step 27 (FIXME: spec bug - unnecessary internal slot) */
/* step 28 (omitted) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale)

Aggregations

OptionsRecord (com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord)4 ResolvedLocale (com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 ToObject (com.github.anba.es6draft.runtime.AbstractOperations.ToObject)1 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1