All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.bower_components.globalize.src.number.js Maven / Gradle / Ivy

define([
    "./core",
    "./common/create-error/unsupported-feature",
    "./common/runtime-bind",
    "./common/validate/cldr",
    "./common/validate/default-locale",
    "./common/validate/parameter-presence",
    "./common/validate/parameter-range",
    "./common/validate/parameter-type/number",
    "./common/validate/parameter-type/plain-object",
    "./common/validate/parameter-type/string",
    "./number/formatter-fn",
    "./number/format-properties",
    "./number/numbering-system",
    "./number/parser-fn",
    "./number/parse-properties",
    "./number/pattern",
    "./number/symbol",
    "./util/remove-literal-quotes",
    "./util/string/pad",

    "cldr/event",
    "cldr/supplemental"
], function (Globalize, createErrorUnsupportedFeature, runtimeBind, validateCldr,
             validateDefaultLocale, validateParameterPresence, validateParameterRange,
             validateParameterTypeNumber, validateParameterTypePlainObject, validateParameterTypeString,
             numberFormatterFn, numberFormatProperties, numberNumberingSystem, numberParserFn,
             numberParseProperties, numberPattern, numberSymbol, removeLiteralQuotes, stringPad) {

    function validateDigits(properties) {
        var minimumIntegerDigits = properties[2],
            minimumFractionDigits = properties[3],
            maximumFractionDigits = properties[4],
            minimumSignificantDigits = properties[5],
            maximumSignificantDigits = properties[6];

        // Validate significant digit format properties
        if (!isNaN(minimumSignificantDigits * maximumSignificantDigits)) {
            validateParameterRange(minimumSignificantDigits, "minimumSignificantDigits", 1, 21);
            validateParameterRange(maximumSignificantDigits, "maximumSignificantDigits",
                minimumSignificantDigits, 21);

        } else if (!isNaN(minimumSignificantDigits) || !isNaN(maximumSignificantDigits)) {
            throw new Error("Neither or both the minimum and maximum significant digits must be " +
                "present");

            // Validate integer and fractional format
        } else {
            validateParameterRange(minimumIntegerDigits, "minimumIntegerDigits", 1, 21);
            validateParameterRange(minimumFractionDigits, "minimumFractionDigits", 0, 20);
            validateParameterRange(maximumFractionDigits, "maximumFractionDigits",
                minimumFractionDigits, 20);
        }
    }

    /**
     * .numberFormatter( [options] )
     *
     * @options [Object]:
     * - style: [String] "decimal" (default) or "percent".
     * - see also number/format options.
     *
     * Return a function that formats a number according to the given options and default/instance
     * locale.
     */
    Globalize.numberFormatter =
        Globalize.prototype.numberFormatter = function (options) {
            var args, cldr, pattern, properties, returnFn;

            validateParameterTypePlainObject(options, "options");

            options = options || {};
            cldr = this.cldr;

            args = [options];

            validateDefaultLocale(cldr);

            cldr.on("get", validateCldr);

            if (options.raw) {
                pattern = options.raw;
            } else {
                pattern = numberPattern(options.style || "decimal", cldr);
            }

            properties = numberFormatProperties(pattern, cldr, options);

            cldr.off("get", validateCldr);

            validateDigits(properties);

            returnFn = numberFormatterFn(properties);

            runtimeBind(args, cldr, returnFn, [properties]);

            return returnFn;
        };

    /**
     * .numberParser( [options] )
     *
     * @options [Object]:
     * - style: [String] "decimal" (default) or "percent".
     *
     * Return the number parser according to the default/instance locale.
     */
    Globalize.numberParser =
        Globalize.prototype.numberParser = function (options) {
            var args, cldr, pattern, properties, returnFn;

            validateParameterTypePlainObject(options, "options");

            options = options || {};
            cldr = this.cldr;

            args = [options];

            validateDefaultLocale(cldr);

            cldr.on("get", validateCldr);

            if (options.raw) {
                pattern = options.raw;
            } else {
                pattern = numberPattern(options.style || "decimal", cldr);
            }

            properties = numberParseProperties(pattern, cldr, options);

            cldr.off("get", validateCldr);

            returnFn = numberParserFn(properties);

            runtimeBind(args, cldr, returnFn, [properties]);

            return returnFn;
        };

    /**
     * .formatNumber( value [, options] )
     *
     * @value [Number] number to be formatted.
     *
     * @options [Object]: see number/format-properties.
     *
     * Format a number according to the given options and default/instance locale.
     */
    Globalize.formatNumber =
        Globalize.prototype.formatNumber = function (value, options) {
            validateParameterPresence(value, "value");
            validateParameterTypeNumber(value, "value");

            return this.numberFormatter(options)(value);
        };

    /**
     * .parseNumber( value [, options] )
     *
     * @value [String]
     *
     * @options [Object]: See numberParser().
     *
     * Return the parsed Number (including Infinity) or NaN when value is invalid.
     */
    Globalize.parseNumber =
        Globalize.prototype.parseNumber = function (value, options) {
            validateParameterPresence(value, "value");
            validateParameterTypeString(value, "value");

            return this.numberParser(options)(value);
        };

    /**
     * Optimization to avoid duplicating some internal functions across modules.
     */
    Globalize._createErrorUnsupportedFeature = createErrorUnsupportedFeature;
    Globalize._numberNumberingSystem = numberNumberingSystem;
    Globalize._numberPattern = numberPattern;
    Globalize._numberSymbol = numberSymbol;
    Globalize._removeLiteralQuotes = removeLiteralQuotes;
    Globalize._stringPad = stringPad;
    Globalize._validateParameterTypeNumber = validateParameterTypeNumber;
    Globalize._validateParameterTypeString = validateParameterTypeString;

    return Globalize;

});




© 2015 - 2025 Weber Informatics LLC | Privacy Policy