102 | | if (named) { |
103 | | return fmt.replace(/%\(\w+\)s/, function(match){return String(obj[match.slice(2,-2)])}); |
104 | | } else { |
105 | | return fmt.replace(/%s/, function(match){return String(obj.shift())}); |
106 | | } |
| 102 | var result = []; |
| 103 | var splitter = (named ? /%\(\w+\)s/g : /%s/g); |
| 104 | var parts = fmt.split(splitter); |
| 105 | var names = fmt.match(splitter); |
| 106 | if(named) { |
| 107 | for(int i = 0; i < parts.length; ++i) { |
| 108 | result[result.length] = parts[i]; |
| 109 | result[result.length] = String(obj[names[i].slice(2,-2)]); |
| 110 | } |
| 111 | } else { |
| 112 | for(int i = 0; i < parts.length; ++i) { |
| 113 | result[result.length] = parts[i]; |
| 114 | result[result.length] = String(obj[i]); |
| 115 | } |
| 116 | } |
| 117 | //if(parts.length > names.length) { // you may not need it |
| 118 | result[result.length] = parts[names.length]; |
| 119 | //} |
| 120 | return result.join(""); |