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 names = []; |
| 104 | var str = fmt; |
| 105 | if(named) { |
| 106 | str = fmt.replace(/%\(\w+\)s/, "%s"); |
| 107 | names = fmt.match(/%\(\w+\)s/g); |
| 108 | } |
| 109 | var parts = str.split("%s"); |
| 110 | if(parts.length < 2) return fmt; |
| 111 | if(named) { |
| 112 | for(int i = 0; i < parts.length - 1; ++i) { |
| 113 | result[result.length] = parts[i]; |
| 114 | result[result.length] = String(obj[names[i].slice(2,-2)]); |
| 115 | } |
| 116 | } else { |
| 117 | for(int i = 0; i < parts.length - 1; ++i) { |
| 118 | result[result.length] = parts[i]; |
| 119 | result[result.length] = String(obj[i]); |
| 120 | } |
| 121 | } |
| 122 | result[result.length] = parts[names.length]; |
| 123 | return result.join(""); |