blob: 012cec21b2d05c94745d6aa714dfaa72b1fce390 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var nargs = /\{([0-9a-zA-Z]+)\}/g
2var slice = Array.prototype.slice
3
4module.exports = template
5
6function 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}