| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var nargs = /\{([0-9a-zA-Z]+)\}/g |
| 2 | var slice = Array.prototype.slice |
| 3 | |
| 4 | module.exports = template |
| 5 | |
| 6 | function template(string) { |
| 7 | var args |
| 8 | |
| 9 | if (arguments.length === 2 && typeof arguments[1] === "object") { |
| 10 | args = arguments[1] |
| 11 | } else { |
| 12 | args = slice.call(arguments, 1) |
| 13 | } |
| 14 | |
| 15 | if (!args || !args.hasOwnProperty) { |
| 16 | args = {} |
| 17 | } |
| 18 | |
| 19 | return string.replace(nargs, function replaceArg(match, i, index) { |
| 20 | var result |
| 21 | |
| 22 | if (string[index - 1] === "{" && |
| 23 | string[index + match.length] === "}") { |
| 24 | return i |
| 25 | } else { |
| 26 | result = args.hasOwnProperty(i) ? args[i] : null |
| 27 | if (result === null || result === undefined) { |
| 28 | return "" |
| 29 | } |
| 30 | |
| 31 | return result |
| 32 | } |
| 33 | }) |
| 34 | } |