-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-string-format.js
45 lines (36 loc) · 1.15 KB
/
angular-string-format.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
(function(window, angular, undefined) {'use strict';
/**
* @ngdoc service
* @name ngStringFormat
* @description
* # sformat
* Factory which provides some helper string formatting functions
*/
angular.module('ngStringFormat', [])
.factory('sformat', function () {
var sformatKwargs, sformatNumbers, sformat;
sformatKwargs = function(string, kwargs) {
return string.replace(/{([a-zA-Z0-9]+)}/g, function(match, varName) {
return typeof kwargs[varName] !== 'undefined' ? kwargs[varName] : match;
});
};
sformatNumbers = function(string) {
var args = Array.prototype.slice.call(arguments, 1);
return string.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
};
sformat = function(string, kwargs) {
var args = arguments,
replaced;
if (args.length === 2 && typeof args[1] === 'object') {
replaced = sformatKwargs(string, kwargs);
}
else {
replaced = sformatNumbers.apply(this, arguments);
}
return replaced;
};
return sformat;
});
})(window, window.angular);