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

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

There is a newer version: 0.66.0.1
Show newest version
define([
    "cldr",
    "./common/runtime-bind",
    "./common/validate/cldr",
    "./common/validate/default-locale",
    "./common/validate/parameter-presence",
    "./common/validate/parameter-type/date",
    "./common/validate/parameter-type/plain-object",
    "./common/validate/parameter-type/string",
    "./core",
    "./date/expand-pattern",
    "./date/formatter-fn",
    "./date/format-properties",
    "./date/parser-fn",
    "./date/parse-properties",
    "./date/tokenizer-properties",

    "cldr/event",
    "cldr/supplemental",
    "./number"
], function (Cldr, runtimeBind, validateCldr, validateDefaultLocale, validateParameterPresence,
             validateParameterTypeDate, validateParameterTypePlainObject, validateParameterTypeString,
             Globalize, dateExpandPattern, dateFormatterFn, dateFormatProperties, dateParserFn,
             dateParseProperties, dateTokenizerProperties) {

    function validateRequiredCldr(path, value) {
        validateCldr(path, value, {
            skip: [
                /dates\/calendars\/gregorian\/dateTimeFormats\/availableFormats/,
                /dates\/calendars\/gregorian\/days\/.*\/short/,
                /supplemental\/timeData\/(?!001)/,
                /supplemental\/weekData\/(?!001)/
            ]
        });
    }

    /**
     * .dateFormatter( options )
     *
     * @options [Object] see date/expand_pattern for more info.
     *
     * Return a date formatter function (of the form below) according to the given options and the
     * default/instance locale.
     *
     * fn( value )
     *
     * @value [Date]
     *
     * Return a function that formats a date according to the given `format` and the default/instance
     * locale.
     */
    Globalize.dateFormatter =
        Globalize.prototype.dateFormatter = function (options) {
            var args, cldr, numberFormatters, pad, pattern, properties, returnFn;

            validateParameterTypePlainObject(options, "options");

            cldr = this.cldr;
            options = options || {skeleton: "yMd"};

            args = [options];

            validateDefaultLocale(cldr);

            cldr.on("get", validateRequiredCldr);
            pattern = dateExpandPattern(options, cldr);
            properties = dateFormatProperties(pattern, cldr);
            cldr.off("get", validateRequiredCldr);

            // Create needed number formatters.
            numberFormatters = properties.numberFormatters;
            delete properties.numberFormatters;
            for (pad in numberFormatters) {
                numberFormatters[pad] = this.numberFormatter({
                    raw: numberFormatters[pad]
                });
            }

            returnFn = dateFormatterFn(numberFormatters, properties);

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

            return returnFn;
        };

    /**
     * .dateParser( options )
     *
     * @options [Object] see date/expand_pattern for more info.
     *
     * Return a function that parses a string date according to the given `formats` and the
     * default/instance locale.
     */
    Globalize.dateParser =
        Globalize.prototype.dateParser = function (options) {
            var args, cldr, numberParser, parseProperties, pattern, tokenizerProperties, returnFn;

            validateParameterTypePlainObject(options, "options");

            cldr = this.cldr;
            options = options || {skeleton: "yMd"};

            args = [options];

            validateDefaultLocale(cldr);

            cldr.on("get", validateRequiredCldr);
            pattern = dateExpandPattern(options, cldr);
            tokenizerProperties = dateTokenizerProperties(pattern, cldr);
            parseProperties = dateParseProperties(cldr);
            cldr.off("get", validateRequiredCldr);

            numberParser = this.numberParser({raw: "0"});

            returnFn = dateParserFn(numberParser, parseProperties, tokenizerProperties);

            runtimeBind(args, cldr, returnFn, [numberParser, parseProperties, tokenizerProperties]);

            return returnFn;
        };

    /**
     * .formatDate( value, options )
     *
     * @value [Date]
     *
     * @options [Object] see date/expand_pattern for more info.
     *
     * Formats a date or number according to the given options string and the default/instance locale.
     */
    Globalize.formatDate =
        Globalize.prototype.formatDate = function (value, options) {
            validateParameterPresence(value, "value");
            validateParameterTypeDate(value, "value");

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

    /**
     * .parseDate( value, options )
     *
     * @value [String]
     *
     * @options [Object] see date/expand_pattern for more info.
     *
     * Return a Date instance or null.
     */
    Globalize.parseDate =
        Globalize.prototype.parseDate = function (value, options) {
            validateParameterPresence(value, "value");
            validateParameterTypeString(value, "value");

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

    return Globalize;

});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy