2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('substitute', function (Y, NAME) {
11 * String variable substitution and string formatting.
12 * If included, the substitute method is added to the YUI instance.
18 var L = Y.Lang, DUMP = 'dump', SPACE = ' ', LBRACE = '{', RBRACE = '}',
19 savedRegExp = /(~-(\d+)-~)/g, lBraceRegExp = /\{LBRACE\}/g, rBraceRegExp = /\{RBRACE\}/g,
22 * The following methods are added to the YUI instance
24 * <strong>Use `Y.Lang.sub` or `Y.Template` instead.</strong>
25 * @class YUI~substitute
30 <strong>Use `Y.Lang.sub` or `Y.Template` instead.</strong>
34 Does `{placeholder}` substitution on a string. The object passed as the
35 second parameter provides values to replace the `{placeholder}`s.
36 {placeholder} token names must match property names of the object. For
39 `var greeting = Y.substitute("Hello, {who}!", { who: "World" });`
41 `{placeholder}` tokens that are undefined on the object map will be left in
42 tact (leaving unsightly "{placeholder}"s in the output string). If your
43 replacement strings *should* include curly braces, use `{LBRACE}` and
44 `{RBRACE}` in your object map string value.
46 If a function is passed as a third argument, it will be called for each
47 {placeholder} found. The {placeholder} name is passed as the first value
48 and the value from the object map is passed as the second. If the
49 {placeholder} contains a space, the first token will be used to identify
50 the object map property and the remainder will be passed as a third
51 argument to the function. See below for an example.
53 If the value in the object map for a given {placeholder} is an object and
54 the `dump` module is loaded, the replacement value will be the string
55 result of calling `Y.dump(...)` with the object as input. Include a
56 numeric second token in the {placeholder} to configure the depth of the call
57 to `Y.dump(...)`, e.g. "{someObject 2}". See the
58 <a href="../classes/YUI.html#method_dump">`dump`</a> method for details.
62 @param {string} s The string that will be modified.
63 @param {object} o An object containing the replacement values.
64 @param {function} f An optional function that can be used to
65 process each match. It receives the key,
66 value, and any extra metadata included with
67 the key inside of the braces.
68 @param {boolean} recurse if true, the replacement will be recursive,
69 letting you have replacement tokens in replacement text.
71 @return {string} the substituted string.
75 function getAttrVal(key, value, name) {
76 // Return a string describing the named attribute and its value if
77 // the first token is @. Otherwise, return the value from the
78 // replacement object.
80 value += name + " Value: " + myObject.get(name);
85 // Assuming myObject.set('foo', 'flowers'),
86 // => "Attr: foo Value: flowers"
87 var attrVal = Y.substitute("{@ foo}", { "@": "Attr: " }, getAttrVal);
90 substitute = function(s, o, f, recurse) {
91 var i, j, k, key, v, meta, saved = [], token, dump,
95 i = s.lastIndexOf(LBRACE, lidx);
99 j = s.indexOf(RBRACE, i);
104 //Extract key and meta info
105 token = s.substring(i + 1, j);
108 k = key.indexOf(SPACE);
110 meta = key.substring(k + 1);
111 key = key.substring(0, k);
117 // if a substitution function was provided, execute it
127 v = Y.dump(v, parseInt(meta, 10));
131 // look for the keyword 'dump', if found force obj dump
132 dump = meta.indexOf(DUMP);
134 meta = meta.substring(4);
137 // use the toString if it is not the Object toString
138 // and the 'dump' meta info was not found
139 if (v.toString === Object.prototype.toString ||
141 v = Y.dump(v, parseInt(meta, 10));
147 } else if (L.isUndefined(v)) {
148 // This {block} has no replace string. Save it for later.
149 v = '~-' + saved.length + '-~';
155 s = s.substring(0, i) + v + s.substring(j + 1);
161 // restore saved {block}s and escaped braces
164 .replace(savedRegExp, function (str, p1, p2) {
165 return LBRACE + saved[parseInt(p2,10)] + RBRACE;
167 .replace(lBraceRegExp, LBRACE)
168 .replace(rBraceRegExp, RBRACE)
172 Y.substitute = substitute;
173 L.substitute = substitute;
177 }, '3.13.0', {"requires": ["yui-base"], "optional": ["dump"]});