substitute-debug.js revision ccd0669fac8c19da541bbcecf8b2673ba1ce70a4
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff// requires lang, dump
736745cee753d967cf5fb1063c21578ecca61b4aJeff ConniffYUI.add("substitute", function(Y) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff var L = Y.Lang, DUMP='dump', SPACE=' ', LBRACE='{', RBRACE='}',
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff /**
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 * is used).
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff *
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * This method is included in the 'i18n' module. It is not included
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff * in the YUI module.
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff *
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
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff */
f47f024e093853ad3957be898687fc8317a835daJeff Conniff substitute = function (s, o, f) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff var i, j, k, key, v, meta, saved=[], token;
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff for (;;) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff i = s.lastIndexOf(LBRACE);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (i < 0) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff break;
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff j = s.indexOf(RBRACE, i);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (i + 1 >= j) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff break;
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff }
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff //Extract key and meta info
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff token = s.substring(i + 1, j);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff key = token;
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff meta = null;
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff k = key.indexOf(SPACE);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (k > -1) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff meta = key.substring(k + 1);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff key = key.substring(0, k);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff // lookup the value
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff v = o[key];
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // if a substitution function was provided, execute it
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (f) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff v = f(key, v, meta);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (L.isObject(v)) {
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff if (!Y.dump) {
24ef71067b8f34f90df1fc636a73424647c97f4bJeff Conniff v = v.toString();
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff } else {
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff if (L.isArray(v)) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff v = Y.dump(v, parseInt(meta, 10));
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff } else {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff meta = meta || "";
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // look for the keyword 'dump', if found force obj dump
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff var dump = meta.indexOf(DUMP);
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff if (dump > -1) {
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff meta = meta.substring(4);
24ef71067b8f34f90df1fc636a73424647c97f4bJeff Conniff }
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff
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) {
04f7ef259d54aeab53258697e1e7f8856adcc8a6Jeff Conniff v = Y.dump(v, parseInt(meta, 10));
04f7ef259d54aeab53258697e1e7f8856adcc8a6Jeff Conniff } else {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff v = v.toString();
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff }
04f7ef259d54aeab53258697e1e7f8856adcc8a6Jeff Conniff } else if (!L.isString(v) && !L.isNumber(v)) {
736745cee753d967cf5fb1063c21578ecca61b4aJeff Conniff // This {block} has no replace string. Save it for later.
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff v = "~-" + saved.length + "-~";
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff saved[saved.length] = token;
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff
df71f95acec70ebddf0ab97cc1e0ff2b70048017Jeff Conniff // break;
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff }
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff s = s.substring(0, i) + v + s.substring(j + 1);
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff }
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff // restore saved {block}s
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff for (i=saved.length-1; i>=0; i=i-1) {
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff s = s.replace(new RegExp("~-" + i + "-~"), "{" + saved[i] + "}", "g");
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff }
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff return s;
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff };
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff Y.substitute = substitute;
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff L.substitute = substitute;
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff}, "@VERSION@", {
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff optional: ['dump']
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff});
691af6c84a00ad5f629ffcfa3b1e42c38da5da4fJeff Conniff