1 |
efrain |
1 |
YUI.add('substitute', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* String variable substitution and string formatting.
|
|
|
5 |
* If included, the substitute method is added to the YUI instance.
|
|
|
6 |
*
|
|
|
7 |
* @module substitute
|
|
|
8 |
* @deprecated
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
var L = Y.Lang, DUMP = 'dump', SPACE = ' ', LBRACE = '{', RBRACE = '}',
|
|
|
12 |
savedRegExp = /(~-(\d+)-~)/g, lBraceRegExp = /\{LBRACE\}/g, rBraceRegExp = /\{RBRACE\}/g,
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* The following methods are added to the YUI instance
|
|
|
16 |
*
|
|
|
17 |
* <strong>Use `Y.Lang.sub` or `Y.Template` instead.</strong>
|
|
|
18 |
* @class YUI~substitute
|
|
|
19 |
* @deprecated
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
<strong>Use `Y.Lang.sub` or `Y.Template` instead.</strong>
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
Does `{placeholder}` substitution on a string. The object passed as the
|
|
|
28 |
second parameter provides values to replace the `{placeholder}`s.
|
|
|
29 |
{placeholder} token names must match property names of the object. For
|
|
|
30 |
example
|
|
|
31 |
|
|
|
32 |
`var greeting = Y.substitute("Hello, {who}!", { who: "World" });`
|
|
|
33 |
|
|
|
34 |
`{placeholder}` tokens that are undefined on the object map will be left in
|
|
|
35 |
tact (leaving unsightly "{placeholder}"s in the output string). If your
|
|
|
36 |
replacement strings *should* include curly braces, use `{LBRACE}` and
|
|
|
37 |
`{RBRACE}` in your object map string value.
|
|
|
38 |
|
|
|
39 |
If a function is passed as a third argument, it will be called for each
|
|
|
40 |
{placeholder} found. The {placeholder} name is passed as the first value
|
|
|
41 |
and the value from the object map is passed as the second. If the
|
|
|
42 |
{placeholder} contains a space, the first token will be used to identify
|
|
|
43 |
the object map property and the remainder will be passed as a third
|
|
|
44 |
argument to the function. See below for an example.
|
|
|
45 |
|
|
|
46 |
If the value in the object map for a given {placeholder} is an object and
|
|
|
47 |
the `dump` module is loaded, the replacement value will be the string
|
|
|
48 |
result of calling `Y.dump(...)` with the object as input. Include a
|
|
|
49 |
numeric second token in the {placeholder} to configure the depth of the call
|
|
|
50 |
to `Y.dump(...)`, e.g. "{someObject 2}". See the
|
|
|
51 |
<a href="../classes/YUI.html#method_dump">`dump`</a> method for details.
|
|
|
52 |
|
|
|
53 |
@method substitute
|
|
|
54 |
@deprecated
|
|
|
55 |
@param {string} s The string that will be modified.
|
|
|
56 |
@param {object} o An object containing the replacement values.
|
|
|
57 |
@param {function} f An optional function that can be used to
|
|
|
58 |
process each match. It receives the key,
|
|
|
59 |
value, and any extra metadata included with
|
|
|
60 |
the key inside of the braces.
|
|
|
61 |
@param {boolean} recurse if true, the replacement will be recursive,
|
|
|
62 |
letting you have replacement tokens in replacement text.
|
|
|
63 |
The default is false.
|
|
|
64 |
@return {string} the substituted string.
|
|
|
65 |
|
|
|
66 |
@example
|
|
|
67 |
|
|
|
68 |
function getAttrVal(key, value, name) {
|
|
|
69 |
// Return a string describing the named attribute and its value if
|
|
|
70 |
// the first token is @. Otherwise, return the value from the
|
|
|
71 |
// replacement object.
|
|
|
72 |
if (key === "@") {
|
|
|
73 |
value += name + " Value: " + myObject.get(name);
|
|
|
74 |
}
|
|
|
75 |
return value;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Assuming myObject.set('foo', 'flowers'),
|
|
|
79 |
// => "Attr: foo Value: flowers"
|
|
|
80 |
var attrVal = Y.substitute("{@ foo}", { "@": "Attr: " }, getAttrVal);
|
|
|
81 |
**/
|
|
|
82 |
|
|
|
83 |
substitute = function(s, o, f, recurse) {
|
|
|
84 |
var i, j, k, key, v, meta, saved = [], token, dump,
|
|
|
85 |
lidx = s.length;
|
|
|
86 |
|
|
|
87 |
for (;;) {
|
|
|
88 |
i = s.lastIndexOf(LBRACE, lidx);
|
|
|
89 |
if (i < 0) {
|
|
|
90 |
break;
|
|
|
91 |
}
|
|
|
92 |
j = s.indexOf(RBRACE, i);
|
|
|
93 |
if (i + 1 >= j) {
|
|
|
94 |
break;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
//Extract key and meta info
|
|
|
98 |
token = s.substring(i + 1, j);
|
|
|
99 |
key = token;
|
|
|
100 |
meta = null;
|
|
|
101 |
k = key.indexOf(SPACE);
|
|
|
102 |
if (k > -1) {
|
|
|
103 |
meta = key.substring(k + 1);
|
|
|
104 |
key = key.substring(0, k);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// lookup the value
|
|
|
108 |
v = o[key];
|
|
|
109 |
|
|
|
110 |
// if a substitution function was provided, execute it
|
|
|
111 |
if (f) {
|
|
|
112 |
v = f(key, v, meta);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (L.isObject(v)) {
|
|
|
116 |
if (!Y.dump) {
|
|
|
117 |
v = v.toString();
|
|
|
118 |
} else {
|
|
|
119 |
if (L.isArray(v)) {
|
|
|
120 |
v = Y.dump(v, parseInt(meta, 10));
|
|
|
121 |
} else {
|
|
|
122 |
meta = meta || '';
|
|
|
123 |
|
|
|
124 |
// look for the keyword 'dump', if found force obj dump
|
|
|
125 |
dump = meta.indexOf(DUMP);
|
|
|
126 |
if (dump > -1) {
|
|
|
127 |
meta = meta.substring(4);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
// use the toString if it is not the Object toString
|
|
|
131 |
// and the 'dump' meta info was not found
|
|
|
132 |
if (v.toString === Object.prototype.toString ||
|
|
|
133 |
dump > -1) {
|
|
|
134 |
v = Y.dump(v, parseInt(meta, 10));
|
|
|
135 |
} else {
|
|
|
136 |
v = v.toString();
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
} else if (L.isUndefined(v)) {
|
|
|
141 |
// This {block} has no replace string. Save it for later.
|
|
|
142 |
v = '~-' + saved.length + '-~';
|
|
|
143 |
saved.push(token);
|
|
|
144 |
|
|
|
145 |
// break;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
s = s.substring(0, i) + v + s.substring(j + 1);
|
|
|
149 |
|
|
|
150 |
if (!recurse) {
|
|
|
151 |
lidx = i - 1;
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
// restore saved {block}s and escaped braces
|
|
|
155 |
|
|
|
156 |
return s
|
|
|
157 |
.replace(savedRegExp, function (str, p1, p2) {
|
|
|
158 |
return LBRACE + saved[parseInt(p2,10)] + RBRACE;
|
|
|
159 |
})
|
|
|
160 |
.replace(lBraceRegExp, LBRACE)
|
|
|
161 |
.replace(rBraceRegExp, RBRACE)
|
|
|
162 |
;
|
|
|
163 |
};
|
|
|
164 |
|
|
|
165 |
Y.substitute = substitute;
|
|
|
166 |
L.substitute = substitute;
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
}, '3.18.1', {"requires": ["yui-base"], "optional": ["dump"]});
|