substitute.js revision 637be785ab98741335d5cd6634fddc5f6ecdb41b
/**
* String variable substitution and string formatting.
* If included, the substitute method is added to the YUI instance.
*
* @module substitute
*/
/**
* The following methods are added to the YUI instance
* @class YUI~substitute
*/
/**
* Does variable substitution on a string. It scans through the string
* looking for expressions enclosed in { } braces. If an expression
* is found, it is used a key on the object. If there is a space in
* the key, the first word is used for the key and the rest is provided
* to an optional function to be used to programatically determine the
* value (the extra information might be used for this decision). If
* the value for the key in the object, or what is returned from the
* function has a string value, number value, or object value, it is
* substituted for the bracket expression and it repeats. If this
* value is an object, it uses the Object's toString() if this has
* pairs if Y.dump is available (if dump isn't available, toString()
* is used).
*
* This method is included in the 'substitute' module. It is not included
* in the YUI module.
*
* @method substitute
* @for YUI
* @param s {string} The string that will be modified.
* @param o An object containing the replacement values
* @param f {function} An optional function that can be used to
* process each match. It receives the key,
* value, and any extra metadata included with
* the key inside of the braces.
* @param ldelim {string} optional left delimiter for the replacement token (default: {)
* @param rdelim {string} optional right delimiter for the replacement token (default: })
* @return {string} the substituted string
*/
for (;;) {
i = s.lastIndexOf(ldelim);
if (i < 0) {
break;
}
if (i + 1 >= j) {
break;
}
//Extract key and meta info
meta = null;
if (k > -1) {
}
// lookup the value
v = o[key];
// if a substitution function was provided, execute it
if (f) {
}
if (L.isObject(v)) {
if (!Y.dump) {
v = v.toString();
} else {
if (L.isArray(v)) {
} else {
// look for the keyword 'dump', if found force obj dump
if (dump > -1) {
}
// use the toString if it is not the Object toString
// and the 'dump' meta info was not found
} else {
v = v.toString();
}
}
}
// This {block} has no replace string. Save it for later.
// break;
}
}
// restore saved {block}s
}
return s;
};
(function() {
// Static regex for matching {key} {key|input} patterns
var pattern = /\{\s*([^|{}]+?)\s*(?:\|([^\{}]*))?\s*\}/g,
// Recursion limit
MAX_RECURSION = 100,
// Replacer function factory
return function (m, k, x) {
var v = o[k];
};
},
// Wrapper to execute substitute until no more substitutions can be done
var r = s, i = 0;
do {
s = r;
r = substitute(s, o, opts);
} while (r !== s && ++i < MAX_RECURSION);
return r;
},
LDELIM = /\{/g,
RDELIM = /\}/g,
UNSPECIFIED = {},
substitute = function (s, o, opts) {
}
}
};
Y.substitute = substitute;
L.substitute = substitute;
})();