New OpenLayers.String.format function to Format a string given a string
template and some context -- to be used within the SLD framework. Developed by Andreas and Sr. Schaub. Thanks, guys! git-svn-id: http://svn.openlayers.org/trunk/openlayers@5317 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -87,7 +87,44 @@ OpenLayers.String = {
|
||||
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
|
||||
}
|
||||
return camelizedString;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIFunction: OpenLayers.String.format
|
||||
* Given a string with tokens in the form ${token}, return a string
|
||||
* with tokens replaced with properties from the given context
|
||||
* object. Represent a literal "${" by doubling it, e.g. "${${".
|
||||
*
|
||||
* Parameters:
|
||||
* template - {String} A string with tokens to be replaced. A template
|
||||
* has the form "literal ${token}" where the token will be replaced
|
||||
* by the value of context["token"].
|
||||
* context - {Object} An optional object with properties corresponding
|
||||
* to the tokens in the format string. If no context is sent, the
|
||||
* window object will be used.
|
||||
*
|
||||
* Returns:
|
||||
* {String} A string with tokens replaced from the context object.
|
||||
*/
|
||||
format: function(template, context) {
|
||||
if(!context) {
|
||||
context = window;
|
||||
}
|
||||
var tokens = template.split("${");
|
||||
var item, last;
|
||||
for(var i=1; i<tokens.length; i++) {
|
||||
item = tokens[i];
|
||||
last = item.indexOf("}");
|
||||
if(last > 0) {
|
||||
tokens[i] = context[item.substring(0, last)] +
|
||||
item.substring(++last);
|
||||
} else {
|
||||
tokens[i] = "${" + item;
|
||||
}
|
||||
}
|
||||
return tokens.join("");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if (!String.prototype.startsWith) {
|
||||
|
||||
@@ -82,7 +82,41 @@
|
||||
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function test_String_format(t) {
|
||||
var unchanged = [
|
||||
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
|
||||
"}", "${${} }"
|
||||
]
|
||||
t.plan(4 + unchanged.length);
|
||||
|
||||
var format = OpenLayers.String.format;
|
||||
|
||||
var expected;
|
||||
for(var i=0; i<unchanged.length; ++i) {
|
||||
expected = unchanged[i];
|
||||
t.eq(format(expected), expected,
|
||||
"'" + expected + "' left unchanged");
|
||||
}
|
||||
|
||||
t.eq(format("${foo} none"),
|
||||
"undefined none", "undefined properties don't bomb");
|
||||
|
||||
window.foo = "bar";
|
||||
t.eq(format("${foo} none"),
|
||||
"bar none", "window context used if none passed");
|
||||
|
||||
var context = {bar: "foo"};
|
||||
t.eq(format("${bar} foo", context), "foo foo",
|
||||
"properties accessed from context");
|
||||
|
||||
var context = {bar: "foo", foo: "bar"};
|
||||
t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
|
||||
"multiple properties replaced correctly");
|
||||
|
||||
}
|
||||
|
||||
|
||||
function test_06_Number_limitSigDigs(t) {
|
||||
t.plan(9);
|
||||
|
||||
Reference in New Issue
Block a user