forked from kvdmolen/vue-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.js
39 lines (37 loc) · 1.12 KB
/
replace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Replace {0} in message. Credits: @Haixing-Hu
*
* @param template
* the message template, which contains zero or more placeholders, e.g.,
* "{0}", "{1}", ...
* @param arg1, arg2, ...
* zero or more arguments used to replace the corresponding placeholders
* in the message template.
* @return
* the formatted message.
* @author Haixing Hu
*/
var PLACEHOLDER_REGEXP = /\{([0-9a-zA-Z]+)\}/g;
module.exports = function() {
if(arguments.length === 0) {
return ""
}else if(arguments.length === 1) {
return arguments[0]
}else{
var args = arguments
var message = args[0]
return message.replace(PLACEHOLDER_REGEXP, function(match, placeholder, index) {
if (message[index - 1] === "{" && message[index + match.length] === "}") {
return placeholder;
}else{
var i = parseInt(placeholder)
var result = args[i + 1]
if (result === null || result === undefined) {
return ""
}else{
return result
}
}
})
}
}