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

META-INF.resources.bower_components.globalize.src.common.format-message.js Maven / Gradle / Ivy

There is a newer version: 1.2.2.1-jre17
Show newest version
define([
    "../util/to-string"
], function (toString) {

    /**
     * formatMessage( message, data )
     *
     * @message [String] A message with optional {vars} to be replaced.
     *
     * @data [Array or JSON] Object with replacing-variables content.
     *
     * Return the formatted message. For example:
     *
     * - formatMessage( "{0} second", [ 1 ] ); // 1 second
     *
     * - formatMessage( "{0}/{1}", ["m", "s"] ); // m/s
     *
     * - formatMessage( "{name} <{email}>", {
 *     name: "Foo",
 *     email: "[email protected]"
 *   }); // Foo 
     */
    return function (message, data) {

        // Replace {attribute}'s
        message = message.replace(/{[0-9a-zA-Z-_. ]+}/g, function (name) {
            name = name.replace(/^{([^}]*)}$/, "$1");
            return toString(data[name]);
        });

        return message;
    };

});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy