Changeset 7529

Show
Ignore:
Timestamp:
03/06/07 07:11:03 (22 months ago)
Author:
peller
Message:

add a transform to substitute() for things like encoding (dijit parser)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/string/extras.js

    r7503 r7529  
    55dojo.require("dojo.lang.array"); 
    66 
    7 dojo.string.substitute = function(/*String*/template, /*Object or Array*/map, /*Object?*/thisObject){ 
     7dojo.string.substitute = function(/*String*/template, /*Object or Array*/map, /*Function?*/transform, /*Object?*/thisObject){ 
    88// summary: 
    99//      Performs parameterized substitutions on a string. Throws an exception if any parameter is unmatched. 
     
    1818// template: a string with expressions in the form ${key} to be replaced or ${key:format} which specifies a format function.  NOTE syntax has changed from %{key} 
    1919// map: where to look for substitutions 
    20 // thisObject: where to look for optional format function 
     20// transform: a function to process all parameters before substitution takes place, e.g. dojo.string.encodeXML 
     21// thisObject: where to look for optional format function; default to the global namespace 
    2122 
    2223        return template.replace(/\$\{([^\s\:]+)(?:\:(\S+))?\}/g, function(match, key, format){ 
    23                 var value = dojo.getObject(key,false,map); 
    24                 if(typeof(value) == "undefined"){ 
    25                         dojo.raise("Missing key: " + key); 
    26                 } 
    27                 return format ? dojo.getObject(format,false,thisObject)(value) : value; 
     24                var value = dojo.getObject(key,false,map).toString(); 
     25                if(format){ value = dojo.getObject(format,false,thisObject)(value);} 
     26                if(transform){ value = transform(value); } 
     27                return value; 
    2828        }); // string 
    2929};