Browse files

first run at .format

  • Loading branch information...
1 parent 10152ec commit 3f7de0fd2b96660108f740fbac98b897a09308b3 @chadbaudoin committed Apr 1, 2012
Showing with 30 additions and 68 deletions.
  1. +29 −67 dotformat.js
  2. +1 −1 test/tests.js
View
96 dotformat.js
@@ -11,79 +11,41 @@
//first off, we don't want to be stepping on anyone else's format function
if( !String.prototype.format ){
- String.prototype.format = function(){
+ String.prototype.format = function( obj ){
var index = 0,
formatArgs = arguments,
returnString,
replacement,
- curArg;
+ curArg,
+ delimiter = "%",
+ regex,
+ key;
- returnString = this.replace(/\%s/g, function(str, offset){
- replacement = formatArgs[index];
- index = index + 1;
- if( replacement !== undefined && replacement !== null ){
- console.log( );
- if(toString.call(replacement) == '[object String]' || toString.call(replacement) == '[object Number]' ){
- return replacement;
- }else if(typeof replacement == 'function'){
- return replacement();
+ if( Object.prototype.toString.call(obj) === '[object Object]' ){
+ regex = new RegExp(delimiter + "[^ ]*" + delimiter, "g");
+ returnString = this.replace(regex, function(str, offset){
+ key = str.replace(new RegExp(delimiter, "g"), "");
+ if( obj[key] ){
+ return obj[key];
}
- }
- return "";
- });
+ return "";
+ });
+ }else{
+ regex = new RegExp(delimiter + "s", "g");
+ returnString = this.replace(regex, function(str, offset){
+ replacement = formatArgs[index];
+ index = index + 1;
+ if( replacement !== undefined && replacement !== null ){
+ if(Object.prototype.toString.call(replacement) == '[object String]' || Object.prototype.toString.call(replacement) == '[object Number]' ){
+ return replacement;
+ }else if(Object.prototype.toString.call(replacement) == '[object Function]'){
+ return replacement();
+ }
+ }
+ return "";
+ });
+ }
return returnString;
};
-}
-
-/*
-Reference
-that.url = function( message, replacementVariables ){
- var messageString = LI.Data.urls[ message ];
-
- //make sure replacementVariables is an object
- replacementVariables = replacementVariables || {};
-
- if( message && messageString ){
- //replace message strings with coorisponding replacement variables
- messageString = messageString.replace( /(\:\w*)/g, function(){
- var match = arguments[0].substring( 1 ),
- returnString = replacementVariables[ match ] || "";
- return encodeURIComponent(returnString);
- });
- return messageString;
- }
- return message;
-};
-
-
-//replacement vars are an array
-/*
- if
- message: "Hello %2s, %1s"
-
- array would look like:
- ["world", "how's it going?"]
-
- and the result of the url would be:
- "Hello world, how's it going?"
-*
-that.message = function( message, replacementVariables ){
- var messageString = LI.Data.messages[ message ],
- index = 0;
-
- //make sure replacementVariables is an array
- replacementVariables = replacementVariables || [];
-
- if( message && messageString ){
- //replace message strings with coorisponding replacement variables
- messageString = messageString.replace( /(\%[^ ]*[\w])/g, function(){
- var returnString = replacementVariables[ index ] || "";
- index = index + 1;
- return returnString;
- });
- return messageString;
- }
- return message;
-};
-*/
+}
View
2 test/tests.js
@@ -25,5 +25,5 @@ test("Test functional argument formatting", function() {
test("Test object argument formatting", function() {
ok("Hello %name%".format({name:"Emmitt"}) === "Hello Emmitt", "Single object argument works correclty");
- ok("Hello %name% i am %age% years old".format({name:"Emmitt", age:3}) === "Hello Emmitt", "Single object with multiple arguments works correclty");
+ ok("Hello %name% i am %age% years old".format({name:"Emmitt", age:3}) === "Hello Emmitt i am 3 years old", "Single object with multiple arguments works correclty");
});

0 comments on commit 3f7de0f

Please sign in to comment.