substitute-debug.js revision ccd0669fac8c19da541bbcecf8b2673ba1ce70a4
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff// requires lang, dump
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff var L = Y.Lang, DUMP='dump', SPACE=' ', LBRACE='{', RBRACE='}',
2aa0883432cf56a974eb9261afcf6ede4313384cJeff Conniff * Does variable substitution on a string. It scans through the string
2aa0883432cf56a974eb9261afcf6ede4313384cJeff Conniff * looking for expressions enclosed in { } braces. If an expression
2aa0883432cf56a974eb9261afcf6ede4313384cJeff Conniff * is found, it is used a key on the object. If there is a space in
2aa0883432cf56a974eb9261afcf6ede4313384cJeff Conniff * the key, the first word is used for the key and the rest is provided
2aa0883432cf56a974eb9261afcf6ede4313384cJeff Conniff * to an optional function to be used to programatically determine the
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * value (the extra information might be used for this decision). If
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * the value for the key in the object, or what is returned from the
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * function has a string value, number value, or object value, it is
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * substituted for the bracket expression and it repeats. If this
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * value is an object, it uses the Object's toString() if this has
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * been overridden, otherwise it does a shallow dump of the key/value
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * pairs if Y.dump is available (if dump isn't available, toString()
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * This method is included in the 'i18n' module. It is not included
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * in the YUI module.
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * @method substitute
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * @param s {String} The string that will be modified.
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * @param o {Object} An object containing the replacement values
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * @param f {Function} An optional function that can be used to
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * process each match. It receives the key,
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * value, and any extra metadata included with
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * the key inside of the braces.
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff * @return {String} the substituted string
f47f024e093853ad3957be898687fc8317a835daJeff Conniff substitute = function (s, o, f) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (i < 0) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (i + 1 >= j) {
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff //Extract key and meta info
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (k > -1) {
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff // lookup the value
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // if a substitution function was provided, execute it
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // look for the keyword 'dump', if found force obj dump
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff // use the toString if it is not the Object toString
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // and the 'dump' meta info was not found
04f7ef259d54aeab53258697e1e7f8856adcc8a6Jeff Conniff if (v.toString===Object.prototype.toString||dump>-1) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // This {block} has no replace string. Save it for later.
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff s = s.substring(0, i) + v + s.substring(j + 1);
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff // restore saved {block}s
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff s = s.replace(new RegExp("~-" + i + "-~"), "{" + saved[i] + "}", "g");
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff}, "@VERSION@", {