-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-utils.js
56 lines (45 loc) · 1.94 KB
/
string-utils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*global window:true,module:true*/
(function () {
"use strict";
var StringUtils = function () {};
/*
StringUtils.format works similar to the String.Format found in .Net.
Parameters in format string like {0}, {1}, etc. are replaced with their respective arguments in the returned string
Examples:
StringUtils.format("The {0} is against the {1}.", "chair", "wall"); // "The chair is against the wall."
StringUtils.format("{0}: Getting ready to publish", new Date()) // "Mon Aug 05 2013 21:21:11 GMT-0500 (CDT): Getting ready to publish"
*/
StringUtils.format = function (formatString) {
var formattedString = formatString,
pattern,
regex,
i;
for (i = 1; i < arguments.length; i += 1) {
pattern = "(^|[^{])\\{" + (i - 1).toString() + "\\}($|[^}])";
regex = new RegExp(pattern, "gi");
formattedString = formattedString.replace(regex, "$1" + arguments[i] + "$2");
}
pattern = "(^|[^{])\\{[0-9]+\\}($|[^}])";
regex = new RegExp(pattern, "gi");
if (regex.test(formattedString)) {
throw "Index (zero based) must be greater than or equal to zero and less than the size of the argument list.";
}
pattern = "(^|[^{])\\{[^}]*\\}($|[^}])";
regex = new RegExp(pattern, "gi");
if (regex.test(formattedString)) {
throw "Input string was not in a correct format.";
}
pattern = "\\{{2}";
regex = new RegExp(pattern, "gi");
formattedString = formattedString.replace(regex, "{");
pattern = "\\}{2}";
regex = new RegExp(pattern, "gi");
formattedString = formattedString.replace(regex, "}");
return formattedString;
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.StringUtils = StringUtils;
} else {
window.StringUtils = StringUtils;
}
}());