Skip to content

Commit

Permalink
Committing dojo.replace(), !strict, refs #9494.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.dojotoolkit.org/src/dojo/trunk@20666 560b804f-0ae3-0310-86f3-f6aa0a117693
  • Loading branch information
uhop committed Oct 26, 2009
1 parent 2e17bfb commit f340aa7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
78 changes: 76 additions & 2 deletions _base/lang.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dojo.provide("dojo._base.lang");

(function(){
var d = dojo;
var d = dojo, opts = Object.prototype.toString;

// Crockford (ish) functions

Expand All @@ -18,7 +18,6 @@ dojo.provide("dojo._base.lang");
return it && (it instanceof Array || typeof it == "array"); // Boolean
}

var opts = Object.prototype.toString;
dojo.isFunction = function(/*anything*/ it){
// summary:
// Return true if it is a Function
Expand Down Expand Up @@ -310,4 +309,79 @@ dojo.provide("dojo._base.lang");
dojo.trim = String.prototype.trim ?
function(str){ return str.trim(); } :
function(str){ return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };

/*=====
dojo.replace = function(tmpl, map, pattern){
// summary:
// Performs parameterized substitutions on a string. Throws an
// exception if any parameter is unmatched.
// tmpl: String
// String to be used as a template.
// map: Object|Function
// If an object, it is used as a dictionary to look up substitutions.
// If a function, it is called for every substitution with following
// parameters: a whole match, a name, an offset, and the whole template
// string (see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/replace
// for more details).
// pattern: RegEx?
// Optional regular expression objects that overrides the default pattern.
// Must be global and match one item. The default is: /\{([^\}]+)\}/g,
// which matches patterns like that: "{xxx}", where "xxx" is any sequence
// of characters, which doesn't include "}".
// returns: String
// Returns the substituted string.
// example:
// | // uses a dictionary for substitutions:
// | dojo.replace("Hello, {name.first} {name.last} AKA {nick}!",
// | {
// | nick: "Bob",
// | name: {
// | first: "Robert",
// | middle: "X",
// | last: "Cringely"
// | }
// | });
// | // returns: Hello, Robert Cringely AKA Bob!
// example:
// | // uses an array for substitutions:
// | dojo.replace("Hello, {0} {2}!",
// | ["Robert", "X", "Cringely"]);
// | // returns: Hello, Robert Cringely!
// example:
// | // uses a function for substitutions:
// | function sum(a){
// | var t = 0;
// | dojo.forEach(a, function(x){ t += x; });
// | return t;
// | }
// | dojo.replace(
// | "{count} payments averaging {avg} USD per payment.",
// | dojo.hitch(
// | { payments: [11, 16, 12] },
// | function(_, key){
// | switch(key){
// | case "count": return this.payments.length;
// | case "min": return Math.min.apply(Math, this.payments);
// | case "max": return Math.max.apply(Math, this.payments);
// | case "sum": return sum(this.payments);
// | case "avg": return sum(this.payments) / this.payments.length;
// | }
// | }
// | ),
// | );
// | // prints: 3 payments averaging 13 USD per payment.
// example:
// | // uses an alternative PHP-like pattern for substitutions:
// | dojo.replace("Hello, ${0} ${2}!",
// | ["Robert", "X", "Cringely"], /\$\{([^\}]+)\}/g);
// | // returns: Hello, Robert Cringely!
return ""; // String
}
=====*/

var _pattern = /\{([^\}]+)\}/g;
dojo.replace = function(tmpl, map, pattern){
return tmpl.replace(pattern || _pattern, d.isFunction(map) ?
map : function(_, k){ return d.getObject(k, false, map); });
};
})();
43 changes: 42 additions & 1 deletion tests/_base/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,47 @@ tests.register("tests._base.lang",
t.is("le bark-s!", c.toLocaleString());
t.is(99, c.z1);
t.is(33, c.z2);
},

function replace(t){
var s1 = dojo.replace("Hello, {name.first} {name.last} AKA {nick}!",
{
nick: "Bob",
name: {
first: "Robert",
middle: "X",
last: "Cringely"
}
});
t.is("Hello, Robert Cringely AKA Bob!", s1);

var s2 = dojo.replace("Hello, {0} {2}!", ["Robert", "X", "Cringely"]);
t.is("Hello, Robert Cringely!", s2);

function sum(a){
var t = 0;
dojo.forEach(a, function(x){ t += x; });
return t;
}
var s3 = dojo.replace(
"{count} payments averaging {avg} USD per payment.",
dojo.hitch(
{ payments: [11, 16, 12] },
function(_, key){
switch(key){
case "count": return this.payments.length;
case "min": return Math.min.apply(Math, this.payments);
case "max": return Math.max.apply(Math, this.payments);
case "sum": return sum(this.payments);
case "avg": return sum(this.payments) / this.payments.length;
}
return "";
}
));
t.is("3 payments averaging 13 USD per payment.", s3);

var s4 = dojo.replace("Hello, ${0} ${2}!", ["Robert", "X", "Cringely"], /\$\{([^\}]+)\}/g);
t.is("Hello, Robert Cringely!", s4);
}
]
);
);

0 comments on commit f340aa7

Please sign in to comment.